Reputation: 59
I am making a very simple game in Shiny. Basically every player plays in their own session, but they share 1 global variable to keep track of the scores of each player.
I think I'm succeeding having sessions update the global 'scores' variable, but for some (probably dumb) reason I cannot get the global variable to act as a reactive value (i.e. automatically triggering updateActionButton). Minimal code below:
working example:
score <- c(100)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
mainPanel(
actionButton("increase_score", label = " increase score player 1 "),
verbatimTextOutput("show_score_p1")
)
)
# Server logic
server <- function(input, output){
observeEvent(input$increase_score,{
score[1] <- score[1]+10
})
output$show_score_p1 <- renderText({paste(score[1])})
}
shinyApp(ui,server)
I tried a couple of methods to try and make my global 'score' to be reactive, i.e. makeReactiveBinding(score), but to no avail. Any ideas? Definitely feels like I'm missing something super-obvious
Upvotes: 3
Views: 3588
Reputation: 3729
Use can use a helpful function called reactiveValues
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
mainPanel(
actionButton("increase_score", label = " increase score player 1 "),
verbatimTextOutput("show_score_p1")
)
)
score<-reactiveValues(a=100)
# Server logic
server <- function(input, output){
observeEvent(input$increase_score,{
score$a <- score$a+10
})
output$show_score_p1 <- renderText({
score$a
})
}
shinyApp(ui,server)
Upvotes: 2