Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function call to close boxPlus #10

Closed
happyshows opened this issue Aug 14, 2018 · 4 comments
Closed

Add function call to close boxPlus #10

happyshows opened this issue Aug 14, 2018 · 4 comments

Comments

@happyshows
Copy link

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.

@samuelhuerga
Copy link

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!

@DivadNojnarg
Copy link
Member

@samuelhuerga: Hi! Can you provide an example please? What kind to action do you want to see?

@kiesner
Copy link

kiesner commented Apr 30, 2019

@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?

@DivadNojnarg
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants