将变量从服务器传递到 UI

Pass a variable from Server to UI

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

非常感谢

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 在评论中的要求。

请注意,我已更正 nn reactive 中的拼写错误。

如果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)