【问题标题】:RStudio Shiny actionButton ignoredRStudio闪亮的动作按钮被忽略
【发布时间】:2014-10-03 23:57:38
【问题描述】:

server.R 中由actionButton 触发的input$goButton 是否应该在RStudio Shiny 中的if 语句中? Shiny 网页上的示例显示:

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Click the button"),
  sidebarPanel(
    sliderInput("obs", "Number of observations:",
                min = 0, max = 1000, value = 500),
    actionButton("goButton", "Go!")
  ),
  mainPanel(
    plotOutput("distPlot")
  )
))

library(shiny)
shinyServer(function(input, output, message) {
  output$distPlot <- renderPlot({
    # Take a dependency on input$goButton
    input$goButton

    # Use isolate() to avoid dependency on input$obs
    dist <- isolate(rnorm(input$obs))
    hist(dist, main=isolate(paste(system(paste("echo", dist[1],"> /tmp/1 && md5sum /tmp/1"),intern=TRUE),collapse='')))
  })
})

我有一个稍微复杂的例程,其中包含比上面的示例更多的语句,并且事件发生在用户单击 Go 按钮之前的事件。当反应中的语句之一是 R system() 调用时,这让我觉得 input$goButton 被忽略了。

Shiny Server v1.1.0.10000
Node.js v0.10.21
packageVersion: 0.10.0

有什么想法吗?

【问题讨论】:

  • 这里显示了依赖项在 goButton 上,因为它在反应表达式中。这样看,如果你想依赖你的按钮,那么只需在你的反应式表达式中输入 input$goButton ,每次点击它都会执行反应式表达式。
  • 在我更复杂的例子中,我有一堆isolate() 调用。这可能是它在 goButton 之前被触发的原因吗?我可以在响应式调用中拥有多个 isolate() 吗?
  • 提供你的例子然后我就能看到问题的确切根源是什么
  • @pops 我想我把它缩小到system() 命令被触发,不管按钮是什么。我更新了我的示例。
  • 所以你只想在按下按钮时绘制 hist?

标签: shiny rstudio


【解决方案1】:

我这就是你想要的?每当按下按钮时,它都会增加计数 + 1(从 0 开始),因此如果没有按下,则有带有 return()“nothing”的 if 语句

rm(list = ls())
library(shiny)
runApp(list(ui = pageWithSidebar(
  headerPanel("Click the button"),
  sidebarPanel(
    sliderInput("obs", "Number of observations:",min = 0, max = 1000, value = 500),
    actionButton("goButton", "Go!")
  ),
  mainPanel(plotOutput("distPlot"))),

  server = function(input, output,session) {
    my_data <- reactive({
      if(input$goButton == 0)
      {
        return()
      }
      isolate({
        input$goButton
        dist <- isolate(rnorm(input$obs))
        hist(dist, main=isolate(paste(system(paste("echo", dist[1],"> /tmp/1 && md5sum /tmp/1"),intern=TRUE),collapse=''))) 
      })
    })
    output$distPlot <- renderPlot({my_data()
    })
  }
)
)

【讨论】:

  • 这非常有效。我没想到我必须让input$goButton 出现两次,一次在 if return() 中,另一次在大的isolate() 中。谢谢。
  • 它根本不应该在isolate() 中。它什么也没做。
猜你喜欢
  • 1970-01-01
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 2019-06-18
  • 2015-01-15
  • 2019-02-12
相关资源
最近更新 更多