Based on this question R shinyjs shinydashboard box uncollapse on action button input and question How to manually collapse a box in shiny dashboard, I would like to substitute the actionButton
with the radioButtons
(or selectInput
). Below a reproducible example. When I click yes I want box id=B2 and id=B3 to collapse, when I click no, box id =B1 and id =B3 to collapse, and when maybe is clicked, box id=B1 and id=B2 to collapse. With the code below, there is a collapse, but it does not work as intended.
library(shiny)
library(shinyBS)
library(dplyr)
library(shinydashboard)
# javascript code to collapse box
jscode <- "
shinyjs.collapse = function(boxid) {
$('#' + boxid).closest('.box').find('[data-widget=collapse]').click();
}
"
#Design sidebar
sidebar <- dashboardSidebar(width = 225, collapsed=F,
sidebarMenu(id="tabs",
menuItem("zz", tabName = "zz", selected=TRUE)))
#Design body
body <- dashboardBody(shinyjs:::useShinyjs(),
shinyjs:::extendShinyjs(text = jscode),
tabItems(
tabItem(tabName = "zz",
fluidRow(box(radioButtons('go','Go', choices = c("yes", "no", "maybe"))),
box(id="B1", collapsible=T, status = "primary", color="blue", solidHeader = T,
title="Test"),
box(id="B2", collapsible=T, status = "primary", color="blue", solidHeader = T,
title="Test2"),
box(id="B3", collapsible=T, status = "primary", color="blue", solidHeader = T,
title="Test3")
))
))
Header <- dashboardHeader()
#Show title and the page (includes sidebar and body)
ui <- dashboardPage(Header, sidebar, body)
server <- shinyServer(function(input, output, session){
observeEvent(input$go == "yes",
{js$collapse("B2", "B3")}
)
#
observeEvent(input$go == "no",
{js$collapse("B1", "B3")}
)
observeEvent(input$go == "maybe",
{js$collapse("B1", "B2")}
)
})
shinyApp( ui = ui, server = server)