【问题标题】:Refresh input values in shiny app with `actionButton`使用“actionButton”刷新闪亮应用程序中的输入值
【发布时间】:2015-08-27 21:59:50
【问题描述】:

我正在开发一个闪亮的应用程序来探索线性回归中的平方和 (link)。这个应用程序有三个sliderInput,所以用户可以选择:(i)回归斜率; (ii) 样本量和 (iii) 标准差。有了这些输入,应用程序会生成一个原始数据集来绘制一些图表。这适用于 reactive 函数。任何一个参数的变化都会产生新的数据。我的问题是我想包含一个按钮来“刷新”所有值,实际上是重新运行生成这些参数的函数。

所以我的问题是如何将它包含在服务器中?

我知道我必须在 ui 中包含按钮:

actionButton(inputId = "refresh", label = "Refresh" , 
             icon = icon("fa fa-refresh"))
)

但我不知道如何使用此按钮重新运行生成数据的反应函数。这是在服务器中生成数据的代码:

### Saving data:
Rawdata <- reactive({
  slope <- input$slope
  SD <- input$SD
  sample <- input$sample
  x <- round(1:sample + rnorm(n = sample, mean = 1, sd = 1), digits = 2)
  y <- round(slope * (x) + rnorm(n = sample, mean = 3, sd = SD ), digits     = 2)
  mod <- lm(y ~ x, data.frame(y,x))
  ypred <- predict(mod)
  Rawdata <- data.frame(y, x, ypred)
})

完整的源代码可在 github 中找到: ui | server

感谢任何建议。

最好的祝愿, 古斯塔沃

【问题讨论】:

  • 将您的数据放入reactiveValues,然后有一个observeer 响应操作按钮并更新反应值。

标签: r shiny


【解决方案1】:

您可以isolate 其他输入变量并使actionButton 仅依赖于反应式表达式:

library(shiny)

shinyApp(
  server = function(input, output, session) {
      rawdata <- reactive({
        # Make action button dependency
        input$refresh 
        # but isolate input$sample
        isolate(rnorm(input$sample))
      })

      output$mean <- renderText({ mean(rawdata()) })
  },
  ui = fluidPage(
    actionButton(inputId = "refresh",
      label = "Refresh", icon = icon("fa fa-refresh")),
    sliderInput(inputId = "sample",
      label = "Sample size",
      value = 50, min = 10, max = 100),
    textOutput("mean")
  )
)

【讨论】:

  • 完美!现在工作非常顺利!
猜你喜欢
  • 2016-02-09
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 2021-12-05
相关资源
最近更新 更多