R 访问被动功能内部的元素
如果我把一些元素放在反应函数中,我需要它们在代码的下面,我怎么能回忆起它们,例如:R 访问被动功能内部的元素,r,shiny,reactive-programming,R,Shiny,Reactive Programming,如果我把一些元素放在反应函数中,我需要它们在代码的下面,我怎么能回忆起它们,例如: library(shiny) library(data.table) library(formattable) library(tables) ui <- fluidPage( fileInput(inputId = "File",label = "Upload file...",accept=c("zip","text")), tableOutput("AEP"
library(shiny)
library(data.table)
library(formattable)
library(tables)
ui <- fluidPage(
fileInput(inputId = "File",label = "Upload file...",accept=c("zip","text")),
tableOutput("AEP")
)
server <- function(input, output){
options(shiny.maxRequestSize=50*1024^2)
#output$AEP <- renderTable({
data1 <- reactive({
infile=input$File
if (is.null(infile))
return(NULL)
report_list <- c("Park result.txt",
"Park result minus",
"Park result plus")
temp_files <- unzip(infile$datapath)
temp_files <- temp_files[grepl(paste(report_list, collapse = "|"), temp_files)]
T=length(temp_files)
t1=3*c(1:(T/3))
t2=c(1:T)
t2=t2[-t1]
p=c();for(i in 1:T){p[[i]]=c()}
for(i in 1:(length(t1))){p[[t1[i]]]=read.table(temp_files[t1[i]],skip=1,sep=";")}
for(i in 1:(length(t2))){p[[t2[i]]]=read.table(temp_files[t2[i]],skip=2,sep=";")}
Installed_Power=v=park=c();for(i in 1:T/3){park[[i]]=v[[i]]=c()}
for(i in 1:(T/3)){
park[[i]]=as.matrix(cbind(p[[1+(i-1)*3]],p[[2+(i-1)*3]],p[[3+(i-1)*3]]))
}
#Power output :
y=c();for(i in 1:T/3){y[[i]]=c()}
power=Ave.A=Ave.k=AD=c()
for (i in 1:(T/3)){
y[[i]]=park[[i]][,153][3:length(park[[i]][,153])]
y[[i]]=gsub("\\,","",y[[i]])
y[[i]]=as.numeric(y[[i]])
power[i]=sum(y[[i]])/1000
Ave.A[i]=mean(as.numeric(park[[i]][3:length(park[[i]][,162]),162]))
Ave.k[i]=mean(as.numeric(park[[i]][3:length(park[[i]][,163]),163]))
AD[i]=mean(as.numeric(park[[i]][3:length(park[[i]][,200]),200]))
}
})
output$AEP <- renderTable({
df <- data1()
df <-as.data.frame(df)
AEP=data.table(df$power,df$Ave.k,df$Ave.A,df$AD)
})
}
shinyApp(ui=ui, server=server)
如果我不使用reactive函数,而只是使用render函数,代码就可以运行了!我这样做是因为我需要在最后下载结果作为pdf文件。AEP表
另外,如果我想在R markdown中使用AEP表,我该怎么办?您总是可以在另一个被动函数或渲染函数中将reac定义为数据帧 尝试:
这个可重现的示例展示了如何像闪亮应用程序中的任何其他数据框一样访问反应性数据框。如果不能够重现这个问题,很难说你的应用程序到底发生了什么
library(shiny)
ui <- fluidPage(
titlePanel("Test Reactive Dataframe"),
mainPanel(textInput("test", "EnterTextHere"),
tableOutput("test1"),
tableOutput("test2")
)
)
server <- function(input, output) {
df1 <- reactive({
data.frame(id = 1:length(input$test), input = input$test)
})
output$test1 <- renderTable({
df1()$input
})
output$test2 <- renderTable({
df1()[,1:2]
})
}
shinyApp(ui = ui, server = server)
无效:警告:数据中有错误。表:列或参数1为空
output$table <- renderTable({
df <- reac()
df <- as.data.frame(df)
df$T
})
library(shiny)
ui <- fluidPage(
titlePanel("Test Reactive Dataframe"),
mainPanel(textInput("test", "EnterTextHere"),
tableOutput("test1"),
tableOutput("test2")
)
)
server <- function(input, output) {
df1 <- reactive({
data.frame(id = 1:length(input$test), input = input$test)
})
output$test1 <- renderTable({
df1()$input
})
output$test2 <- renderTable({
df1()[,1:2]
})
}
shinyApp(ui = ui, server = server)