我正在尝试在 Shiny 中设置一个简单的纸牌游戏,因此想在
callModule(...)
里面使用observeEvent(input$..,{})
,所以我可以调用相同的模块并发生不同的事件。不幸的是,这似乎不起作用。
我知道,如果我只是observeEvent(input$...,{})
在我的模块中使用代码确实可以工作,但我必须为所有可能的事件定义类似的模型。
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)