【问题标题】:Non-reactive scope in ShinyShiny 中的非反应范围
【发布时间】:2017-08-06 02:37:54
【问题描述】:

在我的 Shiny App 中,有两个输入:观察次数(整数)和颜色(要在红色、绿色和蓝色之间选择的字符)。 还有一个“GO!”操作按钮。

使用哪个 Shiny 函数来:

  • 重新生成随机数并更新直方图 此新数据仅当用户点击“开始!”按钮。
  • 能够动态更改直方图的颜色无需 重新生成随机数。

我更喜欢能够最大限度地提高代码清晰度的解决方案。

请参阅下面我使用isolate 尝试的不成功的尝试之一。

# DO NOT WORK AS EXPECTED

# Define the UI
ui <- fluidPage(
  sliderInput("obs", "Number of observations", 0, 1000, 500),
  selectInput('color', 'Histogram color', c('red', 'blue', 'green')),
  actionButton("goButton", "Go!"),
  plotOutput("distPlot")
)

# Code for the server
server <- function(input, output) {
  output$distPlot <- renderPlot({
    # Take a dependency on input$goButton
    input$goButton

    # Use isolate() to avoid dependency on input$obs
    data <- isolate(rnorm(input$obs))
    return(hist(data, col=input$color))
  })
}

# launch the App
library(shiny)
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny reactive-programming


    【解决方案1】:

    这样的?它只会在按钮单击时更新data() 变量。您可以阅读observeEventeventReactive here

    #rm(list = ls())
    # launch the App
    library(shiny)
    ui <- fluidPage(
      sliderInput("obs", "Number of observations", 0, 1000, 500),
      selectInput('color', 'Histogram color', c('red', 'blue', 'green')),
      actionButton("goButton", "Go!"),
      plotOutput("distPlot")
    )
    
    # Code for the server
    server <- function(input, output) {
      data <- eventReactive(input$goButton,{
        rnorm(input$obs)
      })
      
      output$distPlot <- renderPlot({
        return(hist(data(), col=input$color))
      })
    }
    shinyApp(ui, server)
    

    【讨论】:

    • 谢谢猪排,它完全回答了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多