R Shiny - 我无法从selectInput操作中检索选定的输出值

问题描述 投票:0回答:2

我似乎无法从selectInput操作中检索选定的输出值。向量选择和选择的列表适当地插入到UI中,但input$Site_User_Override变量值始终为NULL

output$Site_Pick_User = renderUI({
    choices=as.list(Subset_List()$to_Select)
    selected = Site_Pick_Initial()
    selectInput("Site_User_Override", "Override Site_Pick:", choices=choices,
        selected=selected, multiple = FALSE, selectize = TRUE, width = NULL, size = NULL)
    })

我的单行向量Site_Pick_Initial()="Name5_1.9mi_S"Subset_List()的结构是标题和下面的五行。

Name    STATE   lat lon Air_Miles   Bearing Rose    Rank    to_Select   Delta_Dir
Name1   NJ  42.3    -74 8.826   12.092  NNE 33  Name1_8.8mi_NNE 175.89
Name2   NJ  42.2    -74 2.690   14.615  NNE 27  Name2_2.7mi_NNE 173.37
Name3   NJ  42.3    -74 9.049   348.271 NNW 34  Name3_9.0mi_NNW 160.29
Name4   NJ  42.2    -74 3.962   329.387 NNW 28  Name4_4.0mi_NNW 141.40
Name5   NJ  42.2    -74 1.868   187.983 S   21  Name5_1.9mi_S   0.00

如何检索input$Site_User_Override的值?上面的代码是简化的;我需要在后面的计算和显示中使用input$Site_User_Override

r select shiny
2个回答
0
投票

根据您的问题,以下代码应该是等效的,但它完全正常。如果您尝试做的不同,请更新。

library(shiny)

ui <- fluidPage(
  uiOutput("test"),
  verbatimTextOutput("debug")
)

data <- data.frame(
  Name = c("n1", "n2", "n3"), 
  to_Select = c("Name1_8.8mi_NNE", "Name4_4.0mi_NNW", "Name5_1.9mi_S"),
  stringsAsFactors = FALSE
)

server <- function(input, output, session) {
  output$test <- renderUI({
    selectInput(
      "Site_User_Override",
      "Override Site_Pick:",
      choices = data$to_Select,
      selected = "Name5_1.9mi_S"
    )
  })

  # input$Site_User_Override is updated and can be used elsewhere
  output$debug <- renderPrint({
    input$Site_User_Override
  })

}

shinyApp(ui, server)

0
投票

谢谢,哈伦。 https://stackoverflow.com/users/4629916/harlan-nelson你给了我我需要的领先优势。 “秘密酱”在ui端使用了两个选择输入()

            selectInput(
                inputId = "Site1_Override",
                label = "Override Site_Pick:",
                choices = "",
                selected = ""
                )

服务器端的AND updateSelectInput(),带有observeEvent()

    observeEvent(
        input$checkGroup, {
        updateSelectInput(session,
            inputId = "Site1_Override",
            label = "Override Site_Pick:",
            choices = as.list(Subset_List()$to_Select),
            selected = Site_Pick_Initial()
            )
        })

我在YouTube视频https://www.youtube.com/watch?v=tXh01HFyH68上看到了对解决方案的额外提示,我首先看到了两个ui和服务器编辑。

我现在可以成功输出所选项目,

    output$result_item  <- renderPrint({
        input$Site1_Override
        })

    output$result <- DT::renderDataTable( 
        Subset_List()[Subset_List()$to_Select  %in% input$Site1_Override,]
        )
© www.soinside.com 2019 - 2024. All rights reserved.