1
votes

I have a shiny application similar to this example, what I want is to get choices in the input without having the "all"choice. For example here, if i remove "all" all the other choices disappear and I dont want this...

For information this application is able to update the choices of inputs in function of the first inputs for example if I choose "A" in the first input, I will see only "1" in my second input choice.

@Benjamin: Yes it's this but keeping my update functionality like said with my example

Thanks in advance

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

  titlePanel("Title"),

  sidebarLayout(
    sidebarPanel(width=3,
                 selectInput("filter1", "Filter 1", multiple = T, choices = c("All", LETTERS), selected = "All"),
                 selectInput("filter2", "Filter 2", multiple = T, choices = c("All", as.character(seq.int(1, length(letters), 1))),
                                selected = "All")
    ),

    mainPanel(
      DT::dataTableOutput("tableprint")
    )
  )
)

server <- function(input, output, session) {

  output$tableprint <- DT::renderDataTable({

    # Data
    df <- tibble(Letters = LETTERS, Numbers = as.character(seq.int(1, length(letters), 1)))

    # Create filters based on inputs
    f1 <- if("All" %in% input$filter1) LETTERS else input$filter1
    f2 <- if("All" %in% input$filter2) as.character(seq.int(1, length(letters), 1)) else input$filter2

    # Filter data
    filtered_df <- filter(df, 
                          Letters %in% f1,
                          Numbers %in% f2)

    # Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
    updateSelectInput(session, "filter1", choices = c("All", filtered_df$Letters), selected = input$filter1)
    updateSelectInput(session, "filter2", choices = c("All", filtered_df$Numbers), selected = input$filter2)

    datatable(filtered_df)

  })
}

# Run the application 
shinyApp(ui = ui, server = server)
2
I'm not entire sure what you're aiming for, but the updateSelectInput calls seem unnecessary to me. If you take those out, do you get what you want? - Benjamin
No i need the updateSelectInput for updating my choices in my choices depending on the last choice - Mostafa790

2 Answers

2
votes

If I have understood the question right, this should work.

UPDATE (added third variable):

library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

  titlePanel("Title"),

  sidebarLayout(
    sidebarPanel(width=3,
                 selectInput("filter1", "Filter 1", multiple = T, choices = c("All", LETTERS), selected = "All"),
                 selectInput("filter2", "Filter 2", multiple = T, choices = c("All", as.character(seq.int(1, length(letters), 1))),
                             selected = "All"),
                 selectInput("filter3", "Filter 3", multiple = T, choices = c("All", letters),
                             selected = "All")
    ),

    mainPanel(
      DT::dataTableOutput("tableprint")
    )
  )
)

server <- function(input, output, session) {

  output$tableprint <- DT::renderDataTable({

    # Data
    df <- tibble(LETTERS = LETTERS, Numbers = as.character(seq.int(1, length(letters), 1)),
                 letters = letters)

    df1 <- df

    if("All" %in% input$filter1){
      df1
    } else if (length(input$filter1)){
      df1 <- df1[which(df1$LETTERS %in% input$filter1),]
    }

    if("All" %in% input$filter2){
      df1
    } else if (length(input$filter2)){
      df1 <- df1[which(df1$Numbers %in% input$filter2),]
    }

    if("All" %in% input$filter3){
      df1
    } else if (length(input$filter3)){
      df1 <- df1[which(df1$letters %in% input$filter3),]
    }


    # Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
    updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1)
    updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2)
    updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3)

    datatable(df1)

  })
}

# Run the application 
shinyApp(ui = ui, server = server)
2
votes

Just change the selectInputs like the following, it should work without a choice 'All':

selectInput("filter1", "Filter 1", multiple = T, choices = LETTERS, selected = NULL),
selectInput("filter2", "Filter 2", multiple = T, 
            choices = as.character(seq.int(1, length(letters), 1)), selected = NULL)