点击标题时展开/折叠闪亮的框

4
我开发了一个闪亮的应用程序,我们在用户界面中使用各种框对象。目前,箱子通过单击框标题右侧的“+/-”符号进行展开/折叠,但是我们需要在单击标题(框标题的任何位置)时展开/折叠。 以下是示例代码: 如果您查看具有图表的框,请让扩展和折叠在单击标题即“直方图框标题”上执行,而不仅仅是在标题右侧的“+/-”符号上执行:
    ## Only run this example in interactive R sessions
    if (interactive()) {
      library(shiny)

      # A dashboard body with a row of infoBoxes and valueBoxes, and two rows of boxes
      body <- dashboardBody(
        # Boxes
        fluidRow(
          box(title = "Histogram box title",
              status = "warning", solidHeader = TRUE, collapsible = TRUE,
              plotOutput("plot", height = 250)
          )
        )


      )

      server <- function(input, output) {

        output$plot <- renderPlot({
          hist(rnorm(50))
        })
      }

      shinyApp(
        ui = dashboardPage(
          dashboardHeader(),
          dashboardSidebar(),
          body
        ),
        server = server
      )
    }
3个回答

5

使用JavaScript很容易实现此目标。您只需创建一个JavaScript函数并在头部代码中调用即可。请参考以下代码以获得更好的理解。我提供了3个选项,请告诉我哪个适合您。

## Only run this example in interactive R sessions
if (interactive()) {
  library(shiny)

# javascript code to collapse box
jscode <- "
shinyjs.collapse = function(boxid) {
$('#' + boxid).closest('.box').find('[data-widget=collapse]').click();
}
"

  # A dashboard body with a row of infoBoxes and valueBoxes, and two rows of boxes
  body <- dashboardBody(
    # Including Javascript
    useShinyjs(),
    extendShinyjs(text = jscode),
    # Boxes
    fluidRow(
      box(id="box1",title = actionLink("titleId", "Histogram box title",icon =icon("arrow-circle-up")), 
          status = "warning", solidHeader = TRUE, collapsible = T,
          plotOutput("plot", height = 250)
      ),
      box(id="box2",title = p("Histogram box title", 
                          actionButton("titleBtId", "", icon = icon("arrow-circle-up"),
                                       class = "btn-xs", title = "Update")), 
          status = "warning", solidHeader = TRUE, collapsible = T,
          plotOutput("plot1", height = 250)
      ),
      box(id="box3",title = actionButton("titleboxId", "Histogram box title",icon =icon("arrow-circle-up")), 
          status = "warning", solidHeader = TRUE, collapsible = T,
          plotOutput("plot2", height = 250)
      )
    )


  )

  server <- function(input, output) {

    output$plot <- renderPlot({
      hist(rnorm(50))
    })
    output$plot1 <- renderPlot({
      hist(rnorm(50))
    })
    output$plot2 <- renderPlot({
      hist(rnorm(50))
    })

    observeEvent(input$titleId, {
      js$collapse("box1")
    })
    observeEvent(input$titleBtId, {
      js$collapse("box2")
    })
    observeEvent(input$titleboxId, {
      js$collapse("box3")
    })
  }

  shinyApp(
    ui = dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      body
    ),
    server = server
  )
}

首先,感谢您的回复,这确实有所帮助。但是它的行为有点奇怪。在我的应用程序中,侧边栏面板中有2个主要框,每个框都有3个子框,其中包含不同的输入。我将您的示例1应用于所有框,对于第一个主框及其相应的子框,它运行良好,但对于第二个主框及其子框则无法正常工作。我已经检查了一切,代码似乎到处都很好。您有任何想法吗?我无法在此处分享我的原始代码,但会尝试放置一个类似的示例。 - WiseChirag
很高兴它有帮助...!! 关于您的新问题,即代码仅适用于一个主框... 我现在能建议的是请对以下两件事进行交叉检查:- 1)如果您已正确地包含了ShinyJS。 2)如果所有框的ID都不同... 如果仍然无法正常工作,请创建可重复使用的示例... - kawsleo
另外,如果它解决了您的问题...请接受/点赞答案以帮助其他人找到解决方案。 - kawsleo
先生,您是正确的,我弄错了ID,我已经纠正了它,现在它完美地工作了。再次感谢。 我已经为答案投票了,但由于我是StackOverflow的新手并且没有所需的声望分数,它没有反映在上面。 但是非常感谢。 - WiseChirag
很高兴我能帮到你!既然这是你的问题,你可以接受这个答案。你只需要将一个答案标记为正确(点击绿色勾图标)。请参考此链接以获得更多澄清(https://meta.stackexchange.com/questions/147531/how-mark-my-question-as-answered-on-stackoverflow)。 - kawsleo
1
在较新版本的Shinyjs中,您需要向extendShinyjs()添加函数参数。使用:extendShinyjs(text = jscode, functions = c("collapse")) - Nivel

0

我认为Lisa DeBruine的答案更好,因为您可以单击整个标题而不仅仅是标题。

将其粘贴到一个小示例中:

if (interactive()) {
  body <- dashboardBody(
    useShinyjs(),
    
      tags$style(HTML("
      .box-header {
        padding: 0 10px 0 0;
      }
      .box-header h3 {
        width: 100%;
        padding: 10px;
      }")),
    
      fluidRow(
        box(id="box1", title = "Histogram box title",
            status = "warning", solidHeader = TRUE, collapsible = T,
            plotOutput("plot", height = 250)
        )
      )
    )
  
    server <- function(input, output) {
    
      output$plot <- renderPlot({
        hist(rnorm(50))
      })
    
      runjs("
      $('.box').on('click', '.box-header h3', function() {
          $(this).closest('.box')
                 .find('[data-widget=collapse]')
                 .click();
      });")
    }
  
  shinyApp(
    ui = dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      body
    ),
    server = server
  )
}

0

你可以使用几行外部 CSS 和 JavaScript 来为应用程序中的所有框执行此操作。

当您单击标题时,JS 会触发小部件上的单击。它必须是 h3 元素,因为小部件位于 .box-header 内,否则会导致无限递归。

$('.box').on('click', '.box-header h3', function() {
    $(this).closest('.box')
           .find('[data-widget=collapse]')
           .click();
});

但是我们需要让h3元素填满整个.box-header,因此除了右侧之外,去掉标题的padding,将其添加到h3中,并使h3填充盒子的100%宽度。

.box-header {
  padding: 0 10px 0 0;
}
.box-header h3 {
  width: 100%;
  padding: 10px;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接