Zizou
Zizou

Reputation: 503

Display input field based on item selected in list in R

I would like to be able to select an item in the list by grouped names. Unfortunately, the group does not appear for a single name, as seen in the picture below. How can I change it?

enter image description here

My code:

library(plotly)
library(dplyr)
library(shiny)
library(shinyWidgets)

trend_pal <-  c('red','blue', 'yellow', 'green') #Palette

TD <- data.frame(Name = rep(c("John Smith", "Antonio Gilbert", "Rickie Hooley", "John Marquez", "Christian Thompson", "Rickie Galvan", "John Anan", "Antonio Rossi")[1:8], each = 12), 
                 Month = rep(month.abb[1:12],8,replace = TRUE), 
                 Value = sample(c(0:300),96, replace = T), stringsAsFactors = F)
TD=as.tbl(TD)

output <- split(TD[,1], sub("\\s.*", " ", TD$Name))

for (i in seq_along(output)){
    colnames(output[[i]]) <- ''
}

# UI
ui <- fluidPage(
    pickerInput("All", "Choose", multiple = T,choices = c("Antonio" = unique(output$Antonio), 'Christian' = unique(output$Christian),
                                                          "John" = unique(output$John), 'Rickie' = unique(output$Rickie)),
    options = list(`max-options` = 4,size = 10)),
    plotlyOutput('plot')
)

# Server code
server <- function(input, output) {
    output$plot <- renderPlotly({
        #Filtering data based on user input
        trend <- TD %>% 
            filter(Name %in% input$All) %>% 
            arrange(Month) %>% 
            droplevels()

        #Plot
        plot_ly(data=trend, x=~Month,  y = ~Value, 
                type = 'scatter', mode = 'lines+markers',
                color = ~Name , colors = trend_pal)      
    })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 71

Answers (1)

Sylvain Berthelot
Sylvain Berthelot

Reputation: 141


you can see this example option-groups-for-selectize-input, when you have just one name in your group, you have to set a list. in your case :

ui <- fluidPage(
  pickerInput("All", "Choose", multiple = T,choices = c("Antonio" = unique(output$Antonio), 'Christian' = list(unique(output$Christian)),
                                                        "John" = unique(output$John), 'Rickie' = unique(output$Rickie)),
              options = list(`max-options` = 4,size = 10)),
  plotlyOutput('plot')
)

EDIT : to answer your comment

library(plotly)
library(dplyr)
library(shiny)
library(shinyWidgets)

trend_pal <-  c('red','blue', 'yellow', 'green') #Palette

TD <- data.frame(Name = rep(c("John Smith", "Antonio Gilbert", "Rickie Hooley", "John Marquez", "Christian Thompson", "Rickie Galvan", "John Anan", "Antonio Rossi")[1:8], each = 12), 
                 Month = rep(month.abb[1:12],8,replace = TRUE), 
                 Value = sample(c(0:300),96, replace = T), stringsAsFactors = F)

output <- split(TD[,1], sub("\\s.*", "", TD$Name))
# creation of choices
choices <- lapply(output,function(x){
  if(length(unique(x))>1){
    unique(x)
  } else{
    list(unique(x))
  }
})


# UI
ui <- fluidPage(
  pickerInput("All", "Choose", multiple = T,choices = choices,
              options = list(`max-options` = 4,size = 10)),
  plotlyOutput('plot')
)

# Server code
server <- function(input, output) {
  output$plot <- renderPlotly({
    #Filtering data based on user input
    trend <- TD %>% 
      filter(Name %in% input$All) %>% 
      arrange(Month) %>% 
      droplevels()

    #Plot
    plot_ly(data=trend, x=~Month,  y = ~Value, 
            type = 'scatter', mode = 'lines+markers',
            color = ~Name , colors = trend_pal)      
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions