闪亮:使用actionButton

时间:2019-09-18 09:52:08

标签: r shiny shinydashboard action-button

我想通过按第一个仪表板上的操作按钮来打开第二个仪表板。我可以使用下面的代码来做到这一点,但仪表板相互连接。例如。如果我关闭第二个仪表板上的侧栏,则第一个仪表板的侧栏也会关闭。

这是 server.R 文件:

function(input, output, session) {
  # some more code

  # react to clicking on button show2
  observeEvent(input$show2, {
      # here is some more code
      showModal(settngsModal())
  })

  settngsModal <- function() {
    modalDialog(

      withTags({ 
        dashboardPage(
          dashboardHeader(
            title = "Second Dashboard"
          ),
          dashboardSidebar(
            sidebarMenu(
              menuItem("Widgets", tabName = "widgets", icon = icon("th"))
            )),
          dashboardBody(
            tabItem(tabName = "widgets",
                    h1("Widgets tab content")
            )
          )
        )
      }),
      title = "Settings",
      fade = TRUE)
  }
}

这是 ui.R 文件:

dashboardPage(

    dashboardHeader(
      title = "First dashboard"
    ),

    dashboardSidebar(collapsed = TRUE,sidebarMenu()),
    dashboardBody(),

      h1('Headline'),

      actionButton("show2", "Show second dashboard", size = 'lg')
    )
)

是否可以有一个“独立”仪表板?

也许甚至有两个可以并排使用的仪表板(因为现在第二个仪表板是一个弹出窗口,并且只有在关闭第二个仪表板的情况下才可以使用第一个仪表板)?

1 个答案:

答案 0 :(得分:1)

您可以在两个shinyjs标签之间使用toggledashBoardPage

下面是在两个仪表板之间切换的示例,在渲染UI dashboardPage元素reactively周围有一个decent issue thread


library(shiny)
library(shinydashboard)
library(shinyjs)

ui <-  tagList(
  useShinyjs(),

  div(id = "dashboard_two",
      style = "display:none",
      dashboardPage(
        dashboardHeader(
          title = "Second dashboard"
        ),
        dashboardSidebar(collapsed = TRUE,sidebarMenu()),
        dashboardBody(fluidRow(actionButton("show1", "Show first dashboard")),
                      fluidRow(box(title = "Dash Two", height = 300, "Testing Render")) )
      )
  ),

  div(id = "dashboard_one",
      style = "display:none",
      dashboardPage(

        dashboardHeader(
          title = "First dashboard"
        ),
        dashboardSidebar(collapsed = TRUE, sidebarMenu()),
        dashboardBody(actionButton("show2", "Show second dashboard")
                      )
      )
  )
)


server <- function(input, output) {
  shinyjs::show("dashboard_one")
  observeEvent({ input$show1; input$show2}, {
    shinyjs::toggle("dashboard_one")
    shinyjs::toggle("dashboard_two")
  })
}

shinyApp(ui, server)