rshinyrdata

How to save data and load into Shiny app from R.Data File


I'm creating DT Table with observe() with value can be updated when user fills the table as below picture :

enter image description here

my goal is I would like to save the updated data to R.Data when clicked the Do Save Data Button, and if close my shiny application and if I opened again I can call again the information that I have been input before.

The issue is I don't know how to load the data enter image description here enter image description here

Any solution or suggestion is really helpful, below are my script

library(shiny)
library(shinyjs)
library(shinydashboard)
library(DT)
library(readr)
library(dplyr)
library(data.table)

ui <- dashboardPage(
  dashboardHeader(title = "Test dashboard"),
  dashboardSidebar(uiOutput("sidebarpanel")),
  dashboardBody(shinyjs::useShinyjs(), uiOutput("body"))
)



server <- function(input, output) {
  
  output$sidebarpanel <- renderUI({
    sidebarMenu(
        menuItem("test2", tabName = "T2", icon = icon("th")),
        actionButton("doSave", "Do Save Data"),
        actionButton("doLoad", "Do Load Data")
      )
    })
  
  output$body <- renderUI({
        tabItems(
        
        tabItem(tabName ="T2", class = "active",
                fluidRow(
                  box(width = 12, title = "DT Result", status = "warning", solidHeader = TRUE,
                      collapsible = TRUE,
                      splitLayout(
                        box(width = 12, title = "Content A", status = "warning", solidHeader = TRUE, 
                            collapsible = TRUE, dataTableOutput('content_SC_RSR'))))
                  
                )))
    })
  
  
  ##################################### TABLE RESULT 1 ##################################################
  vc <- 'value'
  SC_RSR <- reactiveValues(data=NULL)
  
  data_SC_RSR = data.frame(
    KSF = c('A', 'B', 'C'),
    SCORE = c(NA, NA, NA)
    
  )
  
  SC_RSR_Data <- reactive ({
    data_SC_RSR
  })
  
  observe({
    SC_RSR$data <- SC_RSR_Data()
    
  })
  
  output$content_SC_RSR <-  DT::renderDataTable({
    SC_RSR$data %>%
      datatable(editable = list(target = "cell", disable = list(columns = c(0,1))), options = list(paging = FALSE, searching = FALSE))
  })
  
  observeEvent(input$content_SC_RSR_cell_edit, {
    info = input$content_SC_RSR_cell_edit
    str(info)
    i = info$row
    j = info$col
    v = as.numeric(info$value)
    
    SC_RSR$data[i, j] <<- DT::coerceValue(v, SC_RSR$data$data[i, j])
    
  })
  
  
  
  #Saving data
  observeEvent(input$doSave, {
    save(SC_RSR,  file = "data.RData")
    Save_done <- showNotification(paste("Message", "Data Has been saved"), duration = NULL)
  })
  
  #Load data
  observeEvent(input$doLoad, {
    SC_RSR <- load("data.RData")
    Load_done <- showNotification(paste("Message", "Data Has been loaded"), duration = NULL)
  })
}

shinyApp(ui, server)

UPDATE

I already found the solution, I add some solutions as below :

#Saving data
  observeEvent(input$doSave, {
    savedata <<- SC_RSR$data
    save(savedata,  file = "data.RData")
    Save_done <- showNotification(paste("Message", "Data Has been saved"), duration = NULL)
  })
  
  #Load data
  observeEvent(input$doLoad, {
    load("data.RData")
    SC_RSR$data <- savedata
    Load_done <- showNotification(paste("Message", "Data Has been loaded"), duration = NULL)
  })


Solution

  • You cannot yet make persistent data storage on shiny servers run on shinyapps.io. The solution is to write and read to a cloud drive like dropbox. See https://shiny.rstudio.com/articles/persistent-data-storage.html

    edit: if you are just local or on your own server, you can use save() and load() for RData files.