I have been trying to make some code in Shiny that allows multiple files to be uploaded and combined into a single dataframe, but the only way I can figure out to make this work is using a reactive() command. However, I need my dataframe to be subsettable because I have lots of further calculations to do on it, and a reactive dataframe keeps giving me the "object of type 'closure' is not subsettable" error.
Is there any way I can either:
a) Read in multiple files to a static dataframe without using reactive() (ie the same way as I would do it for a single file), or
b) Convert a reactive dataframe to a static one?
I am using the fileInput(...multiple = TRUE) command in the UI.
This is the relevant part of my server code (works for single file upload but not multiple):
server <- function(input, output) {
output$contents <- renderTable({
req(input$file1)
tryCatch(
{
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
#create a subset of all the rows where pred > threshold
rows_above <- df[rowSums(df[6] > input$predthr) > 0, ]
#......my code goes on to do more analysis, subsetting and graphing
return(rows_above)
})
}
I have tried this instead of the read.csv, but it gives me an error if I try any kind of subsetting, and I can't figure out how to convert it to a static dataframe:
df<-reactive({
rbindlist(lapply(input$file1$datapath, fread),
use.names = TRUE, fill = TRUE)
})