繁体   English   中英

闪亮的非反应范围

[英]Non-reactive scope in Shiny

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

使用以下哪个Shiny函数以:

  • 仅当用户单击“ Go!” 使用该新数据重新生成随机数并更新直方图。 按钮。
  • 能够即时更改直方图的颜色而无需重新生成随机数。

我希望能为代码提供最大清晰度的解决方案。

请参阅下面我的一个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)

像这样吗 单击按钮后,它将仅更新data()变量。 您可以在此处阅读observeEventeventReactive

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)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM