【问题标题】:Update content on server only after I click action button in Shiny仅在我单击 Shiny 中的操作按钮后才更新服务器上的内容
【发布时间】:2017-10-30 07:35:55
【问题描述】:

我在 R 中的闪亮服务器中遇到反应输出问题。

我想要做的是创建一个使用基于输入计算的值的情节图。

只有在我单击提交/完成按钮后才需要更新输出,并且必须根据需要多次计算输出。

我设法做的是独立于提交按钮更新内容(提交按钮根本没有功能),并且在我更改 1 个值后,情节立即发生了变化。

我想做的是只有在我指定了我想要的所有参数值之后才更改绘图。

这是我的 ui.R 代码的最小示例:

    shinyUI(fluidPage(
      titlePanel("Wages in Year 2016 for Czech Republic in CZK"),
       sidebarPanel(

      conditionalPanel(condition = "input.conditionedPanels==7",
                 selectInput("Age", "Age", choices = vek_mod$Vek, selected = "23-27"),
                 selectInput("ChosenSex", "Your sex", choices = pohlavi_mod$Pohlavie, selected = "Zena"),
                 actionButton("Button", "Done"),
                 p("Click so changes will take effect.")
      ),
        mainPanel(
         tabsetPanel(
           ...
            tabPanel("Minimal_Example", textOutput("Minimal_Example"), value = 7)
            ...
            , id = "conditionedPanels"
    )
   )
  )
 )

这里是 server.R 代码:

...
  output$Minimal_Example <- renderPrint({

    Age <- input$Age
    Sex <- input$ChosenSex
    c(Age, Sex)
    })
...

我用 observeEvent() 和 eventReactive() 尝试了很多东西,但到目前为止没有任何效果。

如果您想查看代码的行为方式,请查看此处: http://52.59.160.3:3838/sample-apps/Mzdy_ShinyApp/

标签被称为 Minimal_Example

谢谢

【问题讨论】:

  • 考虑使用submitButton 而不是actionButton

标签: r shiny plotly


【解决方案1】:

submitButton 就是为此而生的。


如果您想改用actionButton,只需使用isolate 来避免基于值更改的刷新,并在代码中添加input$Button,如下所示:

output$Minimal_Example <- renderPrint({
  input$Button
  Age <- isolate(input$Age)
  Sex <- isolate(input$ChosenSex)
  c(Age, Sex)
}) 

【讨论】:

  • 感谢您的建议,我会尝试并告诉您是否有效。
  • 谢谢,它有效。我不想使用 submitButton,因为官方闪亮文档不鼓励它:“通常不鼓励使用 submitButton,而支持更通用的 actionButton(请参阅下面的详细信息)”shiny.rstudio.com/reference/shiny/latest/submitButton.html
猜你喜欢
  • 2020-01-03
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 2021-11-24
  • 2012-03-26
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多