Shiny UI: Save the Changes in the Inputs

后端 未结 3 1568
一整个雨季
一整个雨季 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:32

    Here is POC of how to make the user input consistent across sessions.

    DPUT and DGET are the two commands to make the it work. So you end up with a local file that stores the value of the input variable. Here I am only making the input$dataset variable consistent. I think you can use more advance command or even database if you have more variables.. but this works for one is fairly easy.

    Since I have been playing with this a few iterations between server.R and ui.R and I probably you might need to initialize the file at the beginning for one time through command line or even build some logical in your code to check if the file exist or not, if not, create a new file with some default value.

    server.R

    shinyServer(function(input, output) {
    
      datasetInput <- reactive({
        switch(input$dataset,
               "rock" = rock,
               "pressure" = pressure,
               "cars" = cars)
      })
    
      output$summary <- renderPrint({
        dataset <- datasetInput()
        summary(dataset)
        dput(input$dataset, "inputdata_dataset")
      })
    
      output$view <- renderTable({
        head(datasetInput(), n = input$obs)
      })
    })
    

    ui.R

    library(shiny)
    shinyUI(pageWithSidebar(
      headerPanel("Shiny Text"),
      sidebarPanel(
        selectInput("dataset", "Choose a dataset:", 
                    choices = c("rock", "pressure", "cars"),
                    selected = dget("inputdata_dataset")),
        numericInput("obs", "Number of observations to view:", 10)
      ),
      mainPanel(
        verbatimTextOutput("summary"),
        tableOutput("view")
      )
    ))
    

提交回复
热议问题