闪亮:通过单击valueBox触发弹出窗口

问题描述 投票:1回答:2

我想通过点击valueBox在弹出窗口中显示数据表。 valueBox本身应该作为actionButton

当我点击valueBox时,它应该在弹出窗口中呈现一个表格,如下图所示。

enter image description here

任何人都可以帮助这个代码吗?

我的代码:

library(shiny)
library(shinydashboard)

data <- iris

ui <- dashboardPage(
  dashboardHeader(title = "Telemedicine HP"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      valueBox( 60, subtitle = tags$p("Attended", style = "font-size: 200%;"),
                icon = icon("trademark"), color = "purple", width = 4,
                href = NULL))))

server <- function(input,output){
}

shinyApp(ui, server)
r shiny shinydashboard shinyjs shinybs
2个回答
0
投票

你可以用onclick创建一个shinyjs事件。因此,您需要在您的ui中添加useShinyjs(),您可以通过将您的ui包装在tagList中来完成。

当单击具有给定ID的元素时,将在服务器中触发onclick函数。所以你还需要给valueBox一个ID。我决定将它包装在带有ID的div中。

接下来的部分是在触发onclick事件时创建一个弹出窗口。您可以使用showModal中的shinyBS函数来完成此操作。

工作实例

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

data <- iris

ui <- tagList(
  useShinyjs(),
  dashboardPage(
    dashboardHeader(title = "Telemedicine HP"),
    dashboardSidebar(),
    dashboardBody(
      fluidRow(
        div(id='clickdiv',
            valueBox(60, subtitle = tags$p("Attended", style = "font-size: 200%;"), icon = icon("trademark"), color = "purple", width = 4, href = NULL)
        )
      )
    )
  )
)

server <-  function(input, output, session){
  onclick('clickdiv', showModal(modalDialog(
    title = "Your title",
    renderDataTable(data)
  )))
}

shinyApp(ui, server)

0
投票

这是没有shinyjs的另一种解决方案

library(shiny)
library(shinydashboard)
library(shinyBS)

data <- iris

ui <- tagList(
  dashboardPage(
    dashboardHeader(title = "Telemedicine HP"),
    dashboardSidebar(),
    dashboardBody(
      fluidRow(
        div(id='clickdiv',
            valueBox(60, subtitle = tags$p("Attended", style = "font-size: 200%;"), icon = icon("trademark"), color = "purple", width = 4, href = NULL)
        )
      ),
      bsModal("modalExample", "Data Table", "clickdiv", size = "large",dataTableOutput("table"))
    )
  )
)

server <-  function(input, output, session){

  output$table <- renderDataTable({
    head(data)
  })

}

shinyApp(ui, server)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.