Saving the state of the Shiny application for later recovery

I have a brilliant app with many tabs and many widgets on each tab. This is a data driven application, so data is tied to each tab.

I can save the application using image.save() and create a .RData file for later use.

The problem I am facing, how can I restore state for widgets?

If the user has checkboxes, selected radio buttons, and set baseline values ​​in the lists, can I set the ones in the load() step?

I found libraries like shinyURL and shinystore , but is there a direct way to return the environment when write.image was run?

I am not sure where to even start, so I can not send the code.

edit: this is a cross reference from the Shiny Google Group where other solutions have been proposed

+5
source share
1 answer

This is a bit hacky, but it works. It uses the "internal" function ( session$sendInputMessage ), which should not be called explicitly, so there is no guarantee that it will always work.

You want to save all the values ​​of the input object. I get all widgets using reactiveValuesToList(input) (note that this will also save the state of the buttons, which is not entirely clear). An alternative approach would be to list exactly which widgets will be saved, but this solution will be less general, and you will have to update it every time you add / remove input. In the code below, I just save the values ​​in a list called values , you can save it to a file, but you want (RDS / text file / whatever). Then, the download button scans this list and updates each entry based on the value in the list.

There is a similar idea in this thread.

 library(shiny) shinyApp( ui = fluidPage( textInput("text", "text", ""), selectInput("select", "select", 1:5), uiOutput("ui"), actionButton("save", "Save"), actionButton("load", "Load") ), server = function(input, output, session) { output$ui <- renderUI({ tagList( numericInput("num", "num", 7), checkboxGroupInput("chk", "chk", 1:5, c(2,4)) ) }) observeEvent(input$save, { values <<- lapply(reactiveValuesToList(input), unclass) }) observeEvent(input$load, { if (exists("values")) { lapply(names(values), function(x) session$sendInputMessage(x, list(value = values[[x]])) ) } }) } ) 
+3
source

Source: https://habr.com/ru/post/1232854/


All Articles