Shiny Selectinput to Select All from Dropdown

Shiny selectInput to select all from dropdown

There wonderful shinyWidgets package which already has the Select All feature in its pickerInput

library(shiny)
library(shinyWidgets)

ui <- basicPage(
sidebarPanel(
pickerInput("locInput","Location", choices=c("New Mexico", "Colorado", "California"), options = list(`actions-box` = TRUE),multiple = T)
)
)

server <- function(input, output) {

observe({
print(input$locInput)
})

}

shinyApp (ui = ui, server = server)

Sample Image

Implement 'Select All' option in reactive shiny

Here's a full-working example showing how one might incorporate "All" into a selectInput:

library(shiny)
dat <- mtcars
app <- shinyApp(
ui = shinyUI(
pageWithSidebar(
headerPanel("Simple Test"),
sidebarPanel(
selectInput("cyl", "Cylinders", choices = c("All", sort(unique(dat$cyl)))),
selectInput("gear", "Gears", choices = c("All", sort(unique(dat$gear))))
),
mainPanel(
shiny::tableOutput("out")
)
)
),
server = function(input, output, session) {
filtered <- reactive({
rows <- (input$cyl == "All" | dat$cyl == input$cyl) &
(input$gear == "All" | dat$gear == input$gear)
dat[rows,,drop = FALSE]
})
output$out <- renderTable(filtered())
}
)

This takes advantage of R's recycling: while input$gear == "All" returns a single logical, dat$gear == input$gear returns a vector as long as the number of rows in the table. Because the first logical is length 1, it is recycled as long as the second vector. This means that input$gear == "All" is effectively a vector of length "nrow(dat)", and is always all TRUE or all FALSE. When true, none of the other $gear comparisons matter (I'm glossing over NA comparisons); when false, the other $gear comparisons are relevant.

Demonstration of recycling:

1 == 1
# [1] TRUE
1 == 2
# [1] FALSE

1 == 1:5
# [1] TRUE FALSE FALSE FALSE FALSE

1 == 1 | 1 == 1:5
# [1] TRUE TRUE TRUE TRUE TRUE
1 == 2 | 1 == 1:5
# [1] TRUE FALSE FALSE FALSE FALSE

How to make Shiny selectInput dropdown choose between multiple dataframes

We need to return the value of the object from the string i.e. get (using the OP's original syntax)

ui = fluidPage(

titlePanel("Title"),

fluidRow(
selectInput(inputId = "df_test",
label = "Select DF",
choices = c("DF 1" = "df1",
"DF 2" = "df2"),
selected = "DF 1",
width = "50%"),


DT::dataTableOutput("test_table")
)
)
server <- function(input, output) {

output$test_table <- DT::renderDataTable({
get(input$df_test)
})
}
shinyApp(ui, server)

-ouptut

Sample Image

The ability to select ALL potential options in a Shiny input along with a dynamic table

Here is something to try out. You can use updateSelectInput to change your inputs and make them dependent. A separate reactive expression can filter your data based on your inputs. See if this gives you the intended behavior.

library(shiny)
library(DT)

State <- c("NV", "NV","NV", "MD", "MD", "MD", "MD", "NY", "NY", "NY", "OH", "OH", "OH")
County <- c("CLARK", "WASHOE", "EUREKA", "MONTGOMERY", "HOWARD", "BALTIMORE", "FREDERICK", "BRONX", "QUEENS", "WESTCHESTER", "FRANKLIN", "SUMMIT", "STARK" )
City <- c("Las Vegas", "Reno", "Eureka", "Rockville", "Columbia", "Baltimore", "Thurmont", "Bronx", "Queens", "Yonkers", "Columbus", "Akron", "Canton")
Rating<- c(1,2,3,4,5,6,7,8,9,10,11,12,13)
df <- data.frame(State, County, City, Rating, stringsAsFactors = F)

ui <- fluidPage(
titlePanel("Test Dashboard "),
sidebarLayout(
sidebarPanel(
selectInput("data1", "Select State", choices = c("All", unique(df$State))),
selectInput("data2", "Select County", choices = NULL),
selectInput("data3", "select City", choices = NULL)
),
mainPanel(
DTOutput("table")
)
))

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

observeEvent(input$data1, {
if (input$data1 != "All") {
updateSelectInput(session, "data2", "Select County", choices = c("All", unique(df$County[df$State == input$data1])))
} else {
updateSelectInput(session, "data2", "Select County", choices = c("All", unique(df$County)))
}
}, priority = 2)

observeEvent(c(input$data1, input$data2), {
if (input$data2 != "All") {
updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City[df$County == input$data2])))
} else {
if (input$data1 != "All") {
updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City[df$State == input$data1])))
} else {
updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City)))
}
}
}, priority = 1)

filtered_data <- reactive({
temp_data <- df
if (input$data1 != "All") {
temp_data <- temp_data[temp_data$State == input$data1, ]
}
if (input$data2 != "All") {
temp_data <- temp_data[temp_data$County == input$data2, ]
}
if (input$data3 != "All") {
temp_data <- temp_data[temp_data$City == input$data3, ]
}
temp_data
})

output$table <- renderDT(
filtered_data()
)

}

shinyApp(ui, server)

Dplyr filter, select input, with select all condition for Shiny

You could include an all option in market_choices, and then put the filter expression in a conditional statement:

market_choices <- c("north","south","east","west", 'all')
...
test <- reactive({if(input$mkt == 'all') {
df
} else {
df %>% filter(Market == input$mkt)
}})

select input depending on previous select input shiny

I added UI parts and with a minor change of choices= c(), it is working but user need to select items when they appear in the dropdown Select Procedure.

   ui <- fluidPage(
uiOutput('SelectCategory'),
uiOutput('SelectProc')
)

server <- function(input,output,session){
output$SelectCategory <-renderUI({
selectInput("SelectCategory", "Select Category",
choices = c("A"="colname1",
"B"="colname2",
"C"="colname3",
"D"="colname4"))
})

output$SelectProc <-renderUI({
selectInput("SelectProc", "Select Procedure",
choices=c(input$SelectCategory), multiple = T,selected = T)
})
#end of server
}

shinyApp(ui = ui, server = server)

However, if you wish to update upon selection in Category, more change in server needed (See comments)

  server <- function(input,output,session){
output$SelectCategory <-renderUI({
selectInput("SelectCategory", "Select Category",
choices = c("A"="colname1",
"B"="colname2",
"C"="colname3",
"D"="colname4"))
})

output$SelectProc <-renderUI({
selectInput("SelectProc", "Select Procedure",
choices=NULL, #c(input$SelectCategory), #choices be made NULL
multiple = F,selected = T) #Multiple be made FALSE
})
# added an observer with updateSelectInput
observe(
{input$SelectCategory
updateSelectInput(session = session,
inputId = 'SelectProc',label = "Select Procedure",
choices = c(input$SelectCategory)
)

})

# end of server
}


Related Topics



Leave a reply



Submit