Home > Blockchain >  Display values of selected row as shiny widget choices
Display values of selected row as shiny widget choices

Time:04-18

I have the shiny app below in which I want the choices of the 2nd widget to be all the values of one row which is selected by the 1st widget. But I get the column names instead.

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)

CodePudding user response:

The issue is that you are passing a dataframe to the choices argument. According to the docs, choices is a

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.

Hence, as a dataframe is basically a list the column names are shown in the pickerInput.

If you want the values to be shown I would suggest to pass an unnamed vector to the choices argument. I also added a line of code to get rid of the NA values.

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)

enter image description here

  • Related