I have a simple app where a user selects a player's name from a selectInput (player_name) dropdown, and then you can choose other filters from selectInputs (season) and radioButtons (contested) which displays a table. Without using a submit or action button, is there anyway to observe that the player_name value has changed, which would then reset the season and contested inputs to their default values? Normally, I'd use a conditionalPanel for input changes, but in this situation, player_name has thousands of players.
1
votes
1 Answers
1
votes
The basic solution for this is to use an extra uiOutput "filters_UI" to put the second selectInput and radioButtons and also add input$player_name to it, so any time that the player name changes, the complete uiOutput will be created again with the initial values. See the example below.
library(shiny)
ui <- fluidPage(
fluidRow(
column(6, uiOutput('controls_UI')),
column(6, DT::dataTableOutput("table_DT"))
)
)
server <- shinyServer(function(input, output, session) {
# to simulate players
dat <- USJudgeRatings
players <- row.names(dat)
seasons <- names(dat)
output$controls_UI <- renderUI({
fluidRow(
column(6,
selectInput("player_name", "Players", players, selected = players[1])
),
column(6,
uiOutput("filters_UI"))
)
})
# render again if input$player_name changes
output$filters_UI <- renderUI({
input$player_name
fluidRow(
column(6,
selectInput("season", "Season", seasons, selected = seasons[1])
),
column(6,
radioButtons("contested", label = "Contested",
choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
selected = 1)
)
)
})
output$table_DT <- DT::renderDataTable(
datR(),
options = list(
scrollX = TRUE,
paging = TRUE,
lengthMenu = c(10, 20, 50)
),
selection = "single",
rownames = TRUE
)
# creates a table based on the sliders and radio buttons
datR <- reactive({
input$player_name
varName <- input$season
contested <- ifelse(is.null(input$contested),1,as.integer(input$contested))
dat0 <- data.frame( dat[sample(2:length(players),contested*3), varName] )
names(dat0) <- varName
dat0
})
})
shinyApp(ui, server)
I hope it could help you.