I'm currently rewriting a big shinyapp and I try to shift as much as possible into modules. At some point, the user can choose weather to use stuff that is inside box a) or inside box b). I know how to toggle or remove / restore a box in shiny, but I ran across a problem when using shinymodules: Inside the ui-function, I have a radiobutton, and the server-function should just observe its's value and hide or show a box according to the input. Well, the actual box to hide or show ist not inside the module because it is filled with another module.
Please see the code below for an example, you'll see that the box won't be removed or restored or whatever.
Maybe someone has an idea how to fix this or sees where I make a mistake?
Thank you!
# ui ----
testUI <- function(id){
tagList(
radioGroupButtons(NS(id, "switch"), label = NULL, individual = T,
choices = c("show", "dont show"), selected = "dont show"),
)
}
# server ----
testServer <- function(id, boxid){
moduleServer(id, function(input, output, session){
observeEvent(input$switch, {
if(input$switch == "show"){
updateBox(id = boxid, action = "restore", session = session)
} else {
updateBox(id = boxid, action = "remove", session = session)
}
})
})
}
# testing ----
testApp <- function(){
# create ui
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
testUI("zg"),
box(id = "mybox", title = "I am a box",
strong("some content")
) # end box
) # end dashboardBody
) # end dahsboardPage
# create server
server <- function(input, output, session){
testServer("zg", boxid = "mybox")
}
# start server
shinyApp(ui, server)
}
# start small app for testing (comment if not in use)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
testApp()
Normally, in order to use a updateXXX
function in a module, the widget to be updated must be in the UI part of the module.
But that does not work with updateBox
, I don't know why (I think the package author should add something in the implementation). See the example below. The updateRadioButtons
works, but not the updateBox
.
testUI <- function(id){
ns <- NS(id)
tagList(
radioButtons( # this widget will be updated
ns("switch"), label = NULL,
choices = c("show", "dont show"), selected = "show"
),
box( # this widget will *not* be updated
id = ns("mybox"), title = "I am a box", strong("some content")
)
)
}
# server ----
testServer <- function(id, boxid){
moduleServer(id, function(input, output, session){
observeEvent(input$switch, {
if(input$switch == "show"){
updateBox(id = boxid, action = "restore", session = session)
updateRadioButtons(session, "switch", label = "HELLO")
} else {
updateBox(id = boxid, action = "remove", session = session)
updateRadioButtons(session, "switch", label = "GOODBYE")
}
})
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.