I am building a Shiny app that allows users to build a roster for home-ran Fantasy Football league during the playoff season. Users can only pick one player from each team that makes it to the playoffs. Therefore, I want to update my selectizeInput choices to remove players if their team has been selected due to a prior selection. I've created several textOutput helpers to test that my filtering logic works, and on that side I am mostly confident that it is working. But when I add an observe() with updateSelectizeInput() the input does not work the way I expect. When I select, I can see the new value for a fraction of a second and then the value gets overwritten to an empty or NULL value (as if I never made a selection).
I've paired down my app to a relatively small minimally reproducible example that exhibits the same behavior as the full app (although I could provide the full code if preferred). You'll see the updateSelectizeInput(...) at the very bottom of this code chunk.
I'm sure it is something that I'm doing but for the life of me I can't figure it out. Appreciate any help/insight you can provide!
library(tidyverse)
library(shiny)
library(data.table)
players <- data.table(
team = c("ARI","LAC","PHI", "LAC", "PHI"),
player = c("Player 1", "Player 2", "Player 3", "Player 4", "Player 5")
)
ui <- fluidPage(
titlePanel("Playoff Fantasy Football"),
tabsetPanel(
tabPanel(
"Regular Season 2023 Stats"
),
tabPanel(
"Select Roster",
sidebarLayout(
sidebarPanel(
selectizeInput(
inputId = "roster_selections_made",
label = "Select Players and Defensive Team:",
choices = c("",players$player),
options = list(maxItems = 14)
),
width = 3
),
mainPanel(
textOutput("teams_remaining_text"),
textOutput("players_remaining_text")
)
)
)
)
)
server <- function(input, output, session) {
teams_selected <- reactive({
players[player %in% input$roster_selections_made, team] %>%
unique() %>%
sort()
})
teams_remaining <- reactive({
players[!(team %in% teams_selected()), team] %>%
unique() %>%
sort()
})
output$teams_remaining_text <- renderText({
paste0("Teams remaining: ", paste0(teams_remaining(), collapse = ", "))
})
players_remaining <- reactive({
players %>%
filter(team %in% teams_remaining()) %>%
distinct(player) %>%
arrange(player)
})
output$players_remaining_text <- renderText({
paste0("Players remaining: ", paste0(players_remaining()$player, collapse = ", "))
})
# if you comment out the observe() below, the app generally works as expected
observe({
updateSelectizeInput(
session,
inputId = "roster_selections_made",
choices = players_remaining()$player
)
})
}
shinyApp(ui, server)