r - access to elements inside a reactive function -
if put element reactive function need them further down code how recall them, example :
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() (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)
now have reac ()
, reactive function. if in reactive or render function need t
value ? can access $
reac()$
t ?? above mentioned code returns error :
warning: error in data.table: column or argument 1 null
code working if not use reactive function , put render function instead ! because need @ end download result pdf file. (the aep table) should if want use aep table in r markdown ?
this reproducible example shows how reactive data frame can accessed other data frame in shiny app. without being able reproduce question, it's hard what's going on app.
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)
Comments
Post a Comment