R闪亮输入的记忆功能

时间:2019-07-23 11:57:54

标签: r shiny

我有一个Shiny应用,其中包含许多要指定的输入文件。每次我重新打开Shiny应用程序时,都需要重新指定所有一个。有没有一种方法可以让Shiny记住所选的文件? (不是通过在代码中编写默认值,而是通过单击保存按钮或类似的按钮。)

1 个答案:

答案 0 :(得分:0)

您可以创建一个按钮,单击该按钮将保存所有输入的值,另一个按钮将使用保存的值更新输入。 这是一个最小的示例:

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)

您可以通过节省一些费用,命名这些节省,使用selectInput选择所需的节省等来升级此想法...

相关问题