Reputation: 779
As a way to understand Shiny I am developing a small game for multiple players.
I import a csv to retrieve my eligible players (players.csv) and also include a tab with a textInput and actionButton to incrementally add other eligible players as time passes.
My next step is to then select from these eligible players those who wish to play a given game. I offer this functionality as a checkboxGroupInput on the subsequent tabPanel. I have not considered in this query using the players, or indeed the game, any further at this time.
My issue however is that if I add a new player in the first step i can't seem to update the checkboxGroupInput options to reflect any players i have just added in the first step.
For the avoidance of doubt...
QUESTION - How do i force the checkboxGroupInput to also show the new player I just added?
I have nibbled at various different solutions without much success, including trying to make good on updateCheckboxGroupInput or through conditionalPanel.
Here's my working code and a big dose of thanks for your opinions. :)
library(shiny)
########################## SETUP
file_path = "C:/Users/Callum Hollywood/Desktop/"
write.table(data.frame(player = c("john", "paul", "george", "ringo")),
paste0(file_path, "players.csv"),
col.names = T,
row.names = F,
sep = ",")
read_in_players = read.table(paste0(file_path, "players.csv"), header = T, stringsAsFactors = F, sep = ",")$player
########################## UI
ui = fluidPage(
navlistPanel(
tabPanel("add_a_player",
"add a player to the list of eligible players",
textInput("addplayer", ""),
actionButton("submit", "SUBMIT")
),
tabPanel("play_game",
checkboxGroupInput(inputId = "add_players",
label = "add players to a current game",
choices = read_in_players))))
########################## SERVER
server = function(input, output){
players = eventReactive(input$submit,{input$addplayer})
observeEvent(input$submit,{
write.table(players(), paste0(file_path, "players.csv"), col.names = F, row.names = F, sep = ",", append = T)
})}
shinyApp(ui, server)
Upvotes: 0
Views: 1183
Reputation: 7704
You can use updateCheckboxGroupInput
as follows:
server = function(input, output,session){
players = eventReactive(input$submit,{input$addplayer})
observeEvent(input$submit,{
write.table(players(), paste0(file_path, "players.csv"), col.names = F, row.names = F, sep = ",", append = T)
updateCheckboxGroupInput(session, inputId = "add_players",
choices = read.table(paste0(file_path, "players.csv"), header = T, stringsAsFactors = F, sep = ",")$player )
})}
But since reading the csv file every time you add a player is not a good idea, you could instead use reactiveValues
as follow:
server = function(input, output,session){
Allplayers=reactiveValues(Val = read_in_players)
players = eventReactive(input$submit,{input$addplayer})
observeEvent(input$submit,{
write.table(players(), paste0(file_path, "players.csv"), col.names = F, row.names = F, sep = ",", append = T)
Allplayers$Val <- c( Allplayers$Val,players())
updateCheckboxGroupInput(session, inputId = "add_players", choices = Allplayers$Val)
})}
Upvotes: 2