首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R闪光输入的记忆功能

R闪光输入的记忆功能
EN

Stack Overflow用户
提问于 2019-07-23 19:57:55
回答 1查看 89关注 0票数 0

我有一个闪亮的应用程序,有很多输入文件要指定。每次我重新打开我的闪亮的应用程序时,我都需要再次指定所有它们。有没有办法让Shiny记住所选的文件?(不是通过在代码中写入默认值,而是通过单击保存按钮或类似的东西)。

EN

回答 1

Stack Overflow用户

发布于 2019-07-23 22:01:27

您可以创建一个按钮,用于在单击时保存所有输入的值,还可以创建另一个按钮,用于使用保存的值更新输入。下面是一个最小的例子:

代码语言:javascript
复制
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选择你想要的那个,等等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57163739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档