Closed happyshows closed 3 years ago
It would be amazing too to provide some server logic to close button, just to do the opposite: once user has clicked close button on a boxPlus, perform an action on the server side.
Thanks!
@samuelhuerga: Hi! Can you provide an example please? What kind to action do you want to see?
@samuelhuerga @DivadNojnarg Hi, I face the same issue -- I want to execute server side code when the box is closed.
Example: I want to dynamically add boxes to the UI by pressing a button, but allow at most 10 boxes to be created. I store those boxes in a list, and when the close button is pressed, i need a handler that allows me to remove the box from the list again.
pacman::p_load(shiny, shinydashboard, shinydashboardPlus)
ui <- dashboardPage(
header = dashboardHeaderPlus(
title = "Minimal Example",
left_menu =
tagList(actionButton(
inputId = "actionBtnBoxCreation",
label = "create box"
))
),
sidebar = dashboardSidebar(disable = TRUE),
body = dashboardBody(
fluidRow(uiOutput("boxes"))
)
)
server <- function(input, output, session) {
renderBox <- function(runIndex) {
boxPlus(title = paste("Box", runIndex))
}
boxes <- reactiveValues(values = list())
observeEvent(input$actionBtnBoxCreation, {
if (length(boxes$values) >= 10) {
print("Not more than 10 boxes allowed!")
return()
}
boxes$values <- lapply(seq(length(boxes$values) + 1), renderBox)
}, ignoreInit = TRUE)
output$boxes <- renderUI({
boxes$values
})
# observe(<event that fires when box is closed>, {
# <remove box from boxes$values>
# })
}
shinyApp(ui = ui, server = server)
Is this possible?
WIP in v 0.8.0.9000 (github):
For now the box can only be programmatically collapsed but the remove action is also possible (not yet available)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
ui <- dashboardPagePlus(
dashboardHeaderPlus(),
dashboardSidebar(),
dashboardBody(
tags$style("body { background-color: ghostwhite};"),
br(),
boxPlus(
title = textOutput("box_state"),
"Box body",
inputId = "mybox",
collapsible = TRUE,
plotOutput("plot")
),
actionButton("toggle_box", "Toggle Box", class = "bg-success")
)
)
server <- function(input, output, session) {
output$plot <- renderPlot({
req(!input$mybox$collapsed)
plot(rnorm(200))
})
output$box_state <- renderText({
state <- if (input$mybox$collapsed) "collapsed" else "uncollapsed"
paste("My box is", state)
})
observeEvent(input$toggle_box, {
updateBoxPlus("mybox")
})
}
shinyApp(ui, server)
Use case: use modules together with the boxPlus, each box represents certain data element and there's an action button to delete the element (box) in database. Will need a programatic interface to close the box once the data element is deleted.