R shinyjs shinydashboard box uncollapse on action button input

时间:2018-04-04 20:25:09

标签: r shiny shinyjs

在我的闪亮应用程序中,我有几个框在应用程序启动时崩溃。单击操作按钮后,将运行计算,然后框将取消折叠并显示结果。这是我正在使用的示例代码,但它不会解开该框。我从这里获得了“jscode”的代码How to manually collapse a box in shiny dashboard。我相信这个代码是用于在点击按钮时折叠盒子;我不确定如何在单击按钮时将其解除折叠。

非常感谢,

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 个答案:

答案 0 :(得分:1)

您需要使用observeEvent代替eventReactive。您的代码将如下所示:

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)

希望它有所帮助!