ritm
December 24, 2020, 10:44am
1
I have been trying to create a multi input select list/dropdown using r shiny but when i select more than one value from the input,it still shows only the value for the first input selected.
The code is as follows:
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
ui <- fluidPage(
selectInput("Region","Select Region",max_usage_hours_per_region$Region,multiple=TRUE),
tableOutput("table")
)
server <- function(input, output) {
output$table <- renderTable(
max_usage_hours_per_region[max_usage_hours_per_region$Region==input$Region,])}
shinyApp(ui = ui, server = server)
The output is as follows:
library(shiny)
(max_usage_hours_per_region <- data.frame(Region=rep(letters[1:3],3),
other_info=1:9))
ui <- fluidPage(
verbatimTextOutput("peek_at_region"),
fluidRow(style="display:flex;",
tableOutput("table_naive"),tableOutput("table_correct")),
selectInput("Region","Select Region",
max_usage_hours_per_region$Region,
multiple=TRUE)
)
server <- function(input, output) {
filtered_table_naive <- reactive({
req(input$Region)
max_usage_hours_per_region[max_usage_hours_per_region$Region==input$Region,]
})
filtered_table_correctly <- reactive({
req(input$Region)
max_usage_hours_per_region[max_usage_hours_per_region$Region %in% input$Region,]
})
output$table_naive <- renderTable(
req(filtered_table_naive())
)
output$table_correct <- renderTable(
req(filtered_table_correctly())
)
output$peek_at_region <- renderPrint({
input$Region
})
}
shinyApp(ui = ui, server = server)
please note how I use a made up data frame so that my code is runnable.
Hope it helps, and good luck !
ritm
January 4, 2021, 8:41am
3
Sorry for late reply.Happy new year to you.When i select more than two values from the input,it doesn't show the output for all the values selected in the dropdown.
The code is as follows:
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
ui <- fluidPage(
selectInput("Region","Select Region",max_usage_hours_per_region$Region,multiple=TRUE),
tableOutput("table")
)
server <- function(input, output) {
output$table <- renderTable(
max_usage_hours_per_region[max_usage_hours_per_region$Region==input$Region,])}
shinyApp(ui = ui, server = server)
max_usage_hours_per_region is a dataframe with the following output:-
Region Sum_as_Hours
1 Africa 1156.0833
2 Americas 740.1667
3 APAC 740.2833
4 Europe 1895.2000
5 PDO 1053.3500
6 UK 0.0000
I already answered you that this is wrong, you should use the in operator not the equals operator when you are testing multiple conditions. This is not shiny related, and is general R behaviour.
ritm
January 4, 2021, 9:34am
5
Thanks a lot.It had skipped my notice.
system
Closed
January 25, 2021, 9:34am
6
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.