R shinyjs shinydashboard box uncollapse on action button input

你说的曾经没有我的故事 提交于 2020-01-02 21:53:49

问题


In my shiny app, I have few boxes that are collapsed when application starts. Upon clicking on action button, calculations are run and then box should uncollapse and show results. Here is an example code that I am using, but it does not uncollapse the box. I got the code for "jscode" from here How to manually collapse a box in shiny dashboard. I believe this code was for collapsing the box upon clicking the button; I am not sure how to make it to uncollapse the box upon clicking the button.

many thanks,

Krina

ui.R

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("XYZ", tabName = "XYZ", selected=TRUE)))

#Design body 
body <- dashboardBody(shinyjs:::useShinyjs(), 
                      shinyjs:::extendShinyjs(text = jscode),
             tabItems(
               tabItem(tabName = "zz", 
                      fluidRow(box(actionButton('go','Go', class='btn btn-info', icon=icon('play-circle-o','fg-lg'))),
                               box(id="B1", collapsible=T, collapsed = T, status = "primary", color="blue", solidHeader = T, 
                                title="Test")))))

Header <- dashboardHeader()

#Show title and the page (includes sidebar and body)
dashboardPage(Header, sidebar, body)

server.R

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();
}
"
shinyServer(function(input, output, session){

eventReactive(input$go,
              {js$collapse("B1")
              js$collapse("B2")})
})

回答1:


You need to use observeEvent instead of eventReactive. Your code will look something like this:

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(actionButton('go','Go', class='btn btn-info', icon=icon('play-circle-o','fg-lg'))),
                                         box(id="B1", collapsible=T, collapsed = T, status = "primary", color="blue", solidHeader = T, 
                                             title="Test")))))

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,{js$collapse("B1")})
})

shinyApp( ui = ui, server = server)

Hope it helps!



来源:https://stackoverflow.com/questions/49659804/r-shinyjs-shinydashboard-box-uncollapse-on-action-button-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!