将所选行的值显示为闪亮的小部件选项

Display values of selected row as shiny widget choices

我有下面这个闪亮的应用程序,我希望第二个小部件的选择是第一个小部件选择的一行的所有值。但我得到的是列名。

library(shiny)
library(shinydashboard)
RR<-structure(list(Target.country = c("Country_1", "Country_2", "Country_3", 
                                      "Country_4", "Country_5", "Country_6", "Country_7", "Country_8", 
                                      "Country_9", "Country_10"), Ref_1 = c(NA, "Country_3", NA, "Country_3", 
                                                                            "Country_3", "Country_3", "Country_3", NA, "Country_11", NA), 
                   Ref_2 = c(NA, "Country_4", "Country_4", NA, "Country_4", 
                             "Country_4", "Country_4", NA, "Country_12", NA), Ref_3 = c(NA, 
                                                                                        "Country_5", "Country_6", "Country_5", NA, "Country_5", "Country_5", 
                                                                                        NA, "Country_13", NA), Ref_4 = c(NA, "Country_6", "Country_7", 
                                                                                                                         "Country_6", "Country_6", NA, "Country_6", NA, "Country_14", 
                                                                                                                         NA), Ref_5 = c(NA, "Country_7", "Country_8", "Country_7", 
                                                                                                                                        "Country_7", "Country_7", NA, NA, "Country_15", NA), Ref_6 = c(NA, 
                                                                                                                                                                                                       "Country_8", NA, "Country_8", "Country_8", "Country_8", "Country_8", 
                                                                                                                                                                                                       NA, NA, NA), Ref_7 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                              NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
                                                                                                                                                                                                       ), Ref_8 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), Ref_9 = c(NA_real_, 
                                                                                                                                                                                                                                                                                 NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                 NA_real_, NA_real_, NA_real_), Ref_10 = c(NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                           NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                           NA_real_, NA_real_), Ref_11 = c(NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                           NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                           NA_real_), Ref_12 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                 NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
                                                                                                                                                                                                                                                                                                                                                           ), Ref_13 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                         NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), Ref_14 = c(NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                       NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                       NA_real_, NA_real_, NA_real_), Ref_15 = c(NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NA_real_, NA_real_), Ref_16 = c(NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 NA_real_), Ref_17 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ), Ref_18 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), Ref_19 = c(NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             NA_real_, NA_real_, NA_real_), Ref_20 = c(NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       NA_real_, NA_real_), Ref_21 = c(NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       NA_real_), Ref_22 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       )), row.names = c(NA, 10L), class = "data.frame")
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput("Price","Select the price for analysis", choices = RR[,1]),     
    
    uiOutput("sec")
    
    
  ),
  dashboardBody(
    
  )
)

server <- function(input, output, session) {
  output$sec<-renderUI({
    
    exnames<-subset(RR,`Target.country`%in%input$Price)
    pickerInput( "si2", label = "Select reference country",multiple = T, options = list(`actions-box` = TRUE), choices = exnames[1,2:22])
    
  })
}

shinyApp(ui, server)

问题是您将数据帧传递给 choices 参数。根据文档,choices

List of values to select from. If elements of the list are named then that name rather than the value is displayed to the user.

因此,由于数据框基本上是一个列表,因此列名显示在 pickerInput

如果您希望显示这些值,我建议将一个未命名的向量传递给 choices 参数。我还添加了一行代码来删除 NA 值。

library(shiny)
library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput("Price", "Select the price for analysis", choices = RR[, 1]),
    uiOutput("sec")
  ),
  dashboardBody()
)

server <- function(input, output, session) {
  output$sec <- renderUI({
    exnames <- subset(RR, `Target.country` %in% input$Price)
    choices <- unname(t(exnames[1, 2:22])[, 1])
    choices <- choices[!is.na(choices)]
    
    pickerInput("si2", label = "Select reference country", multiple = T, 
                options = list(`actions-box` = TRUE), choices = choices)
  })
}

shinyApp(ui, server)