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

时间:2018-02-16 23:20:51

标签: r select shiny

我似乎无法从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

2 个答案:

答案 0 :(得分:0)

谢谢,哈伦。 https://stackoverflow.com/users/4629916/harlan-nelson 你给了我我需要的领导。 “秘密酱”正在使用BOTH ui侧的selectInput()

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

在服务器端使用AND updateSelectInput()
    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上看到了对解决方案的额外提示,我第一次看到了这两个视频和服务器编辑。

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

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

    output$result <- DT::renderDataTable( 
        Subset_List()[Subset_List()$to_Select  %in% input$Site1_Override,]
        )

答案 1 :(得分: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)