r语言 - 将一个变量从服务器传递到UI



请查看电子邮件末尾的摘要。我希望第二个选择器输入有一个选择列表,这不是硬编码的seq(100),而是seq(nn),其中nn是在服务器部分生成的。如何将该值从服务器传递到用户界面?

多谢

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
pickerInput("init_seed","Select the value of the random number seed",
choices=(c(1000,2000,3000)),
selected=c(1000),
options = list(`actions-box` = TRUE,
`selected-text-format` = "count > 3"),multiple = F),
pickerInput("2nd_choice","Select the value of the output random number",
choices=seq(100),
selected=c(10),
options = list(`actions-box` = TRUE,
`selected-text-format` = "count > 3"),multiple = F)
)

server <- function(input, output) {
nn <- reactive({
set.seed(input$init.seed)
round(runif(1,0,1000),0)
})
}

shinyApp(ui = ui, server = server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
<div style="width: 100% ; height: 400px ; text-align: center; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;" class="muted well">Shiny applications not supported in static R Markdown documents</div>

按OP在评论中的要求

请注意,我已经纠正了nnreactive中的错字。

如果nn仅用于生成选择列表的上限,则可以删除reactive,并在observeEvent中生成上限。我不确定硬编码reactive(或observeEvent)内的随机数种子是一个好主意。

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
pickerInput("init_seed","Select the value of the random number seed",
choices=(c(1000,2000,3000)),
selected=c(1000),
options = list(`actions-box` = TRUE,
`selected-text-format` = "count > 3"),multiple = F),
pickerInput("2nd_choice","Select the value of the output random number",
choices=seq(100),
selected=c(10),
options = list(`actions-box` = TRUE,
`selected-text-format` = "count > 3"),multiple = F)

)
server <- function(input, output, session) {
nn <- reactive({
set.seed(input$init_seed)
round(runif(1,0,1000),0)
})

observeEvent(nn(), {
updatePickerInput(session, "2nd_choice", choices=seq(1:nn()))
})
}
shinyApp(ui = ui, server = server)

最新更新