tic*_*hoc 3 r reactive-programming shiny
在我的 Shiny 应用程序中,有两个输入:观察次数(整数)和颜色(在红色、绿色和蓝色之间选择的字符)。还有一个“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)
Run Code Online (Sandbox Code Playgroud)
像这样的东西吗?它只会data()在单击按钮时更新变量。您可以阅读observeEvent和eventReactive 这里
#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)
Run Code Online (Sandbox Code Playgroud)