【问题标题】:Link two dataTable Outputs in Shiny在 Shiny 中链接两个 dataTable 输出
【发布时间】:2021-06-18 09:08:04
【问题描述】:

我正在尝试在不使用额外选项卡的情况下从另一个表中打开一个表。我有以下代码可以同时显示 2 个表格,但我希望只有在单击第一个表格后才能看到第二个表格。所以,我认为需要某种点击(链接)来使第二个表格可见,在此之前只需要显示第一个表格。

set.seed(0)
mydf <- data.frame(Type = sample(LETTERS[1:5], 30, replace = TRUE),
                   Amount = sample(10:200, 30, replace = TRUE), 
                   stringsAsFactors= FALSE, check.names = FALSE)

mydf_agg <- aggregate(list(Amount=mydf$Amount),list(Type=mydf$Type),sum)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(width=3,status="primary",solidHeader=T,collapsible=T, title="Summary", DT::dataTableOutput("mytable_summary")),
    box(width=3,status="primary",solidHeader=T,collapsible=T, title="More Data", DT::dataTableOutput("mytable"))
    
  )
)


server <- function(input, output) {
  output$mytable_summary <-  DT::renderDataTable({  mydf_agg })
  output$mytable <-  DT::renderDataTable({  mydf })

}

# Run the application 
shinyApp(ui = ui, server = server)

非常感谢任何帮助。

【问题讨论】:

  • 我需要点击 table1 才能进入 table2,反之亦然。再次点击应该有相同的结果。
  • 是的,即使切换选项卡也可以帮助我,但我需要能够单击表格以移动到不同的选项卡/表格。

标签: r shiny shinydashboard


【解决方案1】:

我们可以在这里使用shinyjsshow/hide函数。

library(shiny)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    box(width=3,status="primary",solidHeader=T,collapsible=T, title="Summary", 
        DT::dataTableOutput("mytable_summary")),
    box(width=3,status="primary",solidHeader=T,collapsible=T, title="More Data", 
        DT::dataTableOutput("mytable"))
    
  )
)


server <- function(input, output) {
  rv <- reactiveValues(flag = FALSE)
  output$mytable_summary <-  DT::renderDataTable({mydf_agg})
  output$mytable <-  DT::renderDataTable({  mydf })
  observeEvent(input$mytable_summary_cell_clicked, {
    if(rv$flag) show('mytable')
    else hide('mytable')
    rv$flag <- !rv$flag 
  })
  
  
}

# Run the application 
shinyApp(ui = ui, server = server)

【讨论】:

  • 对不起,我之前应该提到过,我没有(无法安装)所有包,我使用 R v3.4 并且包有限。我有 Shiny 和 ShinyDashboard。
猜你喜欢
  • 2016-12-12
  • 2016-07-12
  • 2021-03-30
  • 1970-01-01
  • 2020-02-14
  • 2017-06-06
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多