Shiny UI: Save the Changes in the Inputs

后端 未结 3 1567
一整个雨季
一整个雨季 2020-12-30 18:07

I have quite a problem. I\'m trying to run a program with quite a few different settings, which can be set in the ui. In my case the user may need to run the programm with t

3条回答
  •  悲哀的现实
    2020-12-30 18:33

    This is a tricky subject. It maybe best to have a client side solution. HTML5 allows the use of local storage. There are a number of javascript libraries which have simple api's for this. I have put a wrapper around one of them as proof of concept:

    devtools::install_github("johndharrison/shinyStorage")
    library(shinyStorage)
    library(shiny)
    
    runApp(
      list(
        ui = fluidPage(
          addSS(),
          uiOutput("textExample")
          )
        , server = function(input, output, session){
          ss <- shinyStore(session = session)
          output$textExample <- renderUI({
            myVar <- ss$get("myVar")
            if(is.null(myVar)){
              textInput("textID", "Add some text to local storage")
            }else{
              textInput("textID", "Add some text to local storage", myVar)          
            }
          })
    
          observe({
            if(!is.null(input$textID)){
              if(input$textID != ""){
                ss$set("myVar", input$textID)
              }
            }
          })
        }
        )
      )
    

    So the demo doesnt look like much. Input some text in the textInput box refresh your browser and the text is remembered hip hurrah!!! The approach can be extended for any R list like objects upto 10mb in size. I will tinker some more on the package.

提交回复
热议问题