我有兴趣为每次刷新页面时更改的闪亮应用程序设置默认选项。所以例如,在hello world Shiny演示中,我希望它是500
而不是默认选择sample(1:1000,1)
http://shiny.rstudio.com/gallery/example-01-hello.html
我已经尝试将随机生成的值直接放在value =
部分,但这似乎只在每次启动应用程序时更新,而不是每次加载页面时。
如何进行随机默认设置?
我们可以使用updateSliderInput
,例如
server <- function(input, output, session) {
observe({
updateSliderInput(session, "bins", value = sample(1:500,1))
})
....
}
不要忘记将session变量添加到服务器函数定义,并将sliderInput中的max值更新为500。
您需要使用响应式UI元素。
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for number of observations
sidebarLayout(
sidebarPanel(
uiOutput("slider")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
# Expression that generates a plot of the distribution. The expression
# is wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should be automatically
# re-executed when inputs change
# 2) Its output type is a plot
#
output$slider <- renderUI({
sliderInput("obs",
"Number of observations:",
min = 1,
max = 1000,
value =runif(1,1,1000))
})
output$distPlot <- renderPlot({
req(input$obs)
# generate an rnorm distribution and plot it
dist <- rnorm(input$obs)
hist(dist)
})
}
shinyApp(ui = ui, server = server)
这将在滑块中随机选择一个新值。这是你追求的吗?