-
My Shiny app inputs multiple files. Based on which file the user selects, the app will do some operations (display the data as a table, perform several math functions, plot the result).
-
This plotting happens instantly in my minmal reprex (below) - because I'm just log-transforming the data as an example - but in my real-world app this is a complex and time-consuming process (because several functions are run, not just simple log transform and plot).
-
How can I make the app do everything it needs to do at once, in the background, then only use the dropdown menu to display them, not actually do the plotting?
Extremely simple test files (just 3 files each containing 1 row of numbers):
https://easyupload.io/m/ufgjwk
Self-contained, minimal reproducible example
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Multiple Files testing"),
sidebarLayout(
sidebarPanel(
fileInput("file",
"Upload the file",
multiple = TRUE,
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')), # fileinput() function is used to get the file upload contorl option
uiOutput("selectfile"),
plotOutput("myplot")),
mainPanel(
uiOutput("tb")
))))
server <- shinyServer(function(input,output) {
# Extract the file path for file
output$filedf2 <- renderTable({
if(is.null(input$file)){return ()}
input$file$datapath # the file input data frame object that contains the file attributes
})
## Side bar select input widget coming through renderUI()
# Following code displays the select input widget with the list of file loaded by the user
output$selectfile <- renderUI({
if(is.null(input$file)) {return()}
list(hr(),
helpText("Select the files for which you need to see data and summary stats"),
selectInput("Select", "Select", choices=input$file$name)
)
})
## Dataset code ##
# This reactive output contains the dataset and display the dataset in table format
output$table <- renderTable({
if(is.null(input$file)){return()}
read.table(file=input$file$datapath[input$file$name==input$Select],)
})
## MainPanel tabset renderUI code ##
# the following renderUI is used to dynamically generate the tabsets when the file is loaded.
# Until the file is loaded, app will not show the tabset.
output$tb <- renderUI({
if(is.null(input$file)) {return()}
else
tabsetPanel(
tabPanel("Dataset", tableOutput("table"))
)
})
output$myplot <- renderPlot({
req(input$Select)
req(input$file)
mydataframe <- as.data.frame(read.table(file=input$file$datapath[input$file$name==input$Select],))
mydataframe <- log10(mydataframe) #Real app does something much more complex
plot(mydataframe)
})
})
shinyApp(ui, server)
shinyApp(ui, server)