Saving uploaded data files and analysis done on them in Shiny app - .RData file empty

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)

What do you mean by "uploaded into Rstudio"? Do you mean e.g. deployed to shinyapps.io?

I don't think there is a global environment in shiny that save.image can access. You might need to access the session object somehow.

https://shiny.rstudio.com/articles/scoping.html

I don't think your save.image is actually saving anything - what environment do you expect it to save? For example, this works:

	observeEvent(input$saveRData, {
		cat("save.image\n")
		test = "something"
		save(test, file = "WorkingEnvironment.RData")
	})

	output$downloadRData <- downloadHandler(
		filename <- function() {
			paste("WorkingEnvironment", "RData", sep=".")},
		content <- function(file) {
			cat("file.copy\n")
			file.copy(from = "WorkingEnvironment.RData", to = file)},
		contentType = NULL
	)

You can get the session object like this, but I can't see sample, which makes me suspect accessing variables requires a specific fetch. You might have to keep track of the data instead of hoping shiny will collect them for you.

	sample <- "a thing in the session"

	observeEvent(input$saveRData, {
		cat("save", ls(all.names = TRUE, envir = session),"\n")
		save(list = ls(all.names = TRUE, envir = session),
			 file = "WorkingEnvironment.RData",
			 envir = session)
	})

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.