Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny UI: Save the Changes in the Inputs

Tags:

r

shiny

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 the same settings more than once. My Problem is that if you refresh or restart the UI everything is set to the default values. For example:

numericInput("1", 
            label = h4("...."),
                                        4,
                                        min=1, 
                                        max=100, 
                                        step=1 
                                        ),
                           br(),
                           numericInput("2", 
                                        label = h4("..."),
                                        1000000,
                                        min=1, 
                                        max=100000000, 
                                        step=1
                                        )

If I set the numericInput "1" to 7, and rerun the program it will be by default at 4. Due to the fact that i have quite a few of those settings, this can be quite a buzzkill. So my question is:"Is there a way to save the changes i have made?"

thank you:)

like image 503
Richard Avatar asked Sep 09 '14 16:09

Richard


1 Answers

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.

like image 157
jdharrison Avatar answered Sep 22 '22 00:09

jdharrison