1

I have a Shiny app with lots of input files to specify. Each time I reopen my Shiny app, I need to specify all of them again. Is there a way that Shiny remembers the selected files? (Not by writing default values in the code but by clicking on a save button or something similar).

1 Answers1

0

You can create a button which saves the values of all the inputs when clicked, and another button which updates the inputs with the saved values. Here is an minimal example :

library(shiny)

# Global variables
path_to_save <- "save_param.RData"

ui <- fluidPage(

  titlePanel("Hello Shiny!"),
  sidebarLayout(

    sidebarPanel(
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30),
      checkboxGroupInput(inputId = "color",
                         label = "Bins color",
                         choices = c("red", "blue", "green")),

      actionButton(inputId = "save",
                   label = "Save parameters"),
      tags$hr(),
      actionButton(inputId = "apply_save",
                   label = "Load saved parameters")

    ),

    mainPanel(
      plotOutput(outputId = "distPlot")
    )
  )
)

# Define server logic required to draw a histogram ----
server <- function(input, output, session) {

  output$distPlot <- renderPlot({

    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = input$color, border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")

  })

  observeEvent(input$save,{
    params <- list(bins = input$bins, color = input$color)
    save(params, file = path_to_save)
  })

  observeEvent(input$apply_save,{
    load(path_to_save) # of course you need to add verifications about the existence of the file
    updateSliderInput(session = session, inputId = "bins", min = 1, max = 50, value = params$bins)
    updateCheckboxGroupInput(session = session, inputId = "color", label = "Bins color", choices = c("red", "blue", "green"),
                        selected = params$color)
  })

}

shinyApp(ui, server)

You could upgrade this idea with the possibility of making several savings, naming these savings, chosing the one you want with a selectInput etc ...

gdevaux
  • 2,308
  • 2
  • 10
  • 19