【发布时间】:2017-07-28 14:39:41
【问题描述】:
如果我把一些元素是反应函数,我需要它们在代码的后面我怎么能回忆起它们,示例:
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)
现在我有reac (),作为反应函数。如果在另一个反应或渲染函数中我只需要 T 值怎么办?我可以像reac()$T 一样通过$ 访问它吗?
上面提到的代码返回错误:
Warning: Error in data.table: column or argument 1 is NULL
如果我不使用响应函数而只使用渲染函数,代码就可以工作!我这样做是因为我最后需要将结果下载为 pdf 文件。 (AEP 表) 另外,如果我想在 R markdown 中使用 AEP 表该怎么办?
【问题讨论】:
标签: r shiny reactive-programming