【发布时间】: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