2

I'm trying to set up a simple Card game in Shiny and therefore want to use callModule(...) inside of observeEvent(input$..,{}), so I can call the same module with different events occurring. Unfortunately this does not seem to work.

I know, that if I simply use observeEvent(input$...,{}) inside my module the code does work but than I would have to define similar models for all possible Events.

playingUI <- function(id) {

  ns <- NS(id)
  tagList(# Create market and hand output
    uiOutput(ns("market")),
    uiOutput(ns("hand")),

    # Actionbutton to take cards
    actionButton(ns("take"),
             label = "TAKE"))
}




player_server <- function(input, output, session, cards) {
  # Pickerinput for Market
  output$market <- renderUI(tagList(
   pickerInput(
      inputId = session$ns("market1"),
      label = "Market",
      choices = cards$market,
      multiple = TRUE
    ),

   # Pickerinput for Hand
   pickerInput(
      inputId = session$ns("Hand"),
      label = "Hand",
      choices = cards$hand,
      multiple = TRUE
    )

  ))
}



taking_server <- function(input, output, id, cards) {
   cards$hand <- c(cards$hand, "new")
}



ui <- fluidPage(playingUI('game'))


server <- function(input, output, session) {
  # Define playing cards
  cards <- reactiveValues(
    # Define market
    market = c("Camel", "Gold", "Diamond"),
    # Define hand
    hand = c("Diamond", "Silver")
  )

  callModule(player_server, 'game', cards)

  # Wrap the module 'taking_server' inside observe - does not work

  observeEvent(input$take, {
    callModule(taking_server, 'game', cards)

  })

}

shinyApp(ui = ui, server = server)
Hayden Y.
  • 448
  • 2
  • 8

0 Answers0