闪亮 - dashboardPage - 我如何向框添加按钮 header

Shiny - dashboardPage - how can I add a button to box header

使用 dashboardPage 我制作了一个由盒子组成的仪表板。

我希望能够单击框 header 上的某处以触发某些操作。我知道 header 有按钮的唯一情况是可扩展框的情况。是否可以将其概括为在单击框的 header 某处时触发某些操作?

我的设计目标是当用户点击这个按钮时更新框中的信息,即改变框的内容。

谢谢!

body <- dashboardBody(
  fluidRow(
    box(
      title = "Title 1", width = 4, solidHeader = TRUE, status = "primary",
      "Box content"
    ),
    box(
      title = "Title 1", width = 4, solidHeader = TRUE, status = "warning",
      "Box content"
    )
  )
)
# We'll save it in a variable `ui` so that we can preview it in the console
ui <- dashboardPage(
  dashboardHeader(title = "Row layout"),
  dashboardSidebar(),
  body
)
# Preview the UI in the console
shinyApp(ui = ui, server = function(input, output) { })

如果你想在框的右上角有一个按钮 header 你可以修改原来的 box 功能或者你可以使用一些 JavaScript 在创建后添加按钮的盒子。

一个更简单的解决方案是创建一个带有 actionLinkactionButton 的框标题。贝娄是这两种情况的例子。第一个框有一个 actionLink 作为标题,当用户点击它时,框的内容会更新。在第二个框上,标题是用纯文本和一个小的 actionButton 创建的,它也会更新框的内容。对于第二个框,您可以添加一些自定义样式以创建与普通框相同大小的 header。

library(shiny)
library(shinydashboard)

body <- dashboardBody(
  fluidRow(
    box(
      title = actionLink("titleId", "Update", icon = icon("refresh")), 
      width = 4, solidHeader = TRUE, status = "primary",
      uiOutput("boxContentUI")
    ),
    box(
      title = p("Title 1", 
                actionButton("titleBtId", "", icon = icon("refresh"),
                  class = "btn-xs", title = "Update")
      ), 
      width = 4, solidHeader = TRUE, status = "warning",
      uiOutput("boxContentUI2")
    )
  )
)

ui <- dashboardPage(
  dashboardHeader(title = "Row layout"),
  dashboardSidebar(),
  body
)

server = function(input, output, session) { 
  output$boxContentUI <- renderUI({
    input$titleId
    pre(paste(sample(letters,10), collapse = ", "))
  }) 

  output$boxContentUI2 <- renderUI({
    input$titleBtId
    pre(paste(sample(LETTERS,10), collapse = ", "))
  })  
}

shinyApp(ui = ui, server = server)