We are working in a complex shiny app with several functions/modules where the data uploaded is observed as tables, it can be modified (for example, applying cut-offs) and plots are generated. Users can spend hours analyzing their data, and we would like to offer them a possibility to save what has been done and their progress.
We wrote code that allows to save and download an .RData file, in which we hoped the working environment including the analysis was stored. Unfortunately, once uploaded into Rstudio, the only thing seen in the environment are the app functions, so nothing really is saved.
Has anybody been in a similar situation and successfully solved it? We would appreciate the help!
Below you can see code for a much simpler version of our Shiny app. There is a file input for data from which a table is created and the buttons to save and download the .RData files.
library(shiny)
# User interface (ui)
ui <- fluidPage(
# Application header
titlePanel("Shiny app"),
# Application layout:
sidebarLayout(
sidebarPanel(
h3("Data input"),
fluidRow(
column(8, fileInput("datafile", label = "Choose data file to upload",
accept = c(".csv",
".tsv",
"text/csv",
"text/comma-separated-values",
"text/plain",
"text/tab-separated-values"))),
column(1, radioButtons("sep", "Separator",
choices = c(Comma = ",",
Space = " ",
Tab = "\t"))))
),
mainPanel(
fluidRow(
br(),
h3("Save & Load .RData files"),
column(5,
actionButton("saveRData", "Save .RData file"),
tags$style(type = "text/css", "#saveRData { margin-bottom:10px;}"),
downloadButton("downloadRData", "Download .RData file"),
tags$style(type = "text/css", "#downloadRData { margin-bottom:10px;}"),
fileInput("loadRData", "Choose .RData file to load",
accept = c(".rdata")))
)
)
),
fluidRow(
column(6,
tableOutput("DataTable"))
)
)
# Server function
server <- function(input, output) {
output$DataTable <- renderTable({
input_file <- input$datafile
if(is.null(input_file))
return(NULL)
else
return(read.csv(input_file$datapath, sep = input$sep, header = TRUE, check.names = FALSE))
})
observeEvent(input$saveRData, {
save.image(file = "WorkingEnvironment.RData")
})
output$downloadRData <- downloadHandler(
filename <- function() {
paste("WorkingEnvironment", "RData", sep=".")},
content <- function(file) {
file.copy("WorkingEnvironment.RData", file)},
contentType = NULL
)
}
shinyApp(ui = ui, server = server)