Shiny dataframe edits don't save on shinyapps.io

I created a simple app where you can input some data and add it to a dataframe. When I close the app and open it once again, the data added before should be displayed.

I got this working in R locally. But once I deployed it to shinyapps.io it does not seem to work. When I add some data and re-open the app, all the previously added data is gone.

My guess is that it does not save to the 'global environment' on shinyapps.io but am unsure since I am new to deploying this type of apps.

Below is a reproducible example.

library(shiny)
library(DT)

data <- data.frame(title = character(), start = as.Date(character()), end = as.Date(character()), 
                   stringsAsFactors=FALSE)

ui <- function(){fluidPage(

  titlePanel("Test"),

  sidebarLayout(
    sidebarPanel(
      dateInput("date",
                label = "Date"),
      selectInput("from", "From", choices = c("08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30",
                                            "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30",
                                            "16:00", "16:30")),
      selectInput("until", "Until", choices = c("08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30",
                                            "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30",
                                            "16:00", "16:30"), selected = "16:30"),
      actionButton("add", "Add")
    ),


    mainPanel(
      tabsetPanel(
        tabPanel("Table", DT::dataTableOutput("table"))
      )
    )
  )
)}

server <- function(input, output) { 

  values_data <- reactiveValues()
  values_data$df <- data

  addData <- observeEvent(input$add, {
    newLine <- isolate(data.frame(title = paste(input$from, " - ", input$until), start = as.Date(input$date)
                                  , end = as.Date(input$date)))
    values_data$df <- isolate(rbind(values_data$df, newLine))
    data <<- values_data$df
  })

  output$table <- DT::renderDataTable({
    datatable(values_data$df, editable = FALSE)
  })
}

shinyApp(ui, server)

Hi Stan! Welcome!

As I think you’ve found out, you can’t expect changes to your global environment — or even the local file system — to persist with an app deployed on shinyapps.io. Some background reading that explains some of why this is:

These two articles discuss some of the approaches available if you need to “save” changes made to data while using your app:

(As a side note, relying on R to endlessly preserve the global environment is not a very safe way to persist data locally, either — it’s better to use functions such as saveRDS() and readRDS() to explicitly save and load your objects, or otherwise write them out to the local file system yourself)

1 Like