R Shiny:如何将数据表添加到动态创建的选项卡中 [英] R Shiny: How to add data tables to dynamically created tabs

查看:22
本文介绍了R Shiny:如何将数据表添加到动态创建的选项卡中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建动态创建的数据表,每个表都有自己的选项卡.选项卡的数量由用户确定.我使用了 这篇文章的代码 作为一个框架.

I am currently trying to create dynamically-created data tables that each have its own tab. The number of tabs is determined by the user. I have used the code from this post as a framework.

我能够动态创建选项卡,但我不知道如何将数据表添加到选项卡.数据表也由用户输入确定.因此,例如,在 ui.R 中,用户可以选择他们想要查看的数据集:

I am able to create the tabs dynamically, but I can't figure out how to add data tables to the tabs. The data tables are determined by user input too. So, for example, say in ui.R, the user has the choice to pick which data sets they want to see:

ui.R

 library(shiny)
 shinyUI(fluidPage(
      titlePanel("Example"),
      sidebarLayout(
           sidebarPanel(
                selectInput("decision", label = "Choose Dataset", 
                            choices = list("mtcars" = "mtcars", 
                                           "iris" = "iris", 
                                           "precip" = "precip", 
                                           "quakes" = "quakes"), 
                            selected = NULL, multiple = TRUE)
           ),
           mainPanel(
                uiOutput('mytabs')
           )
      )
 ))

服务器.R

 library(shiny)
 library(ggplot2)

 shinyServer(function(input, output, session) {
      output$mytabs <- renderUI({
           nTabs = length(input$decision)
           myTabs = lapply(paste('dataset', 1:nTabs), tabPanel)
           do.call(tabsetPanel, myTabs)
      })
 })

所以,我想把对应的数据集分别渲染到每个标签下的数据表中.

So, I would like the corresponding data sets rendered into data tables under each tab separately.

提前感谢您的所有帮助!

Thank you in advance for all of the help!

推荐答案

要做你想做的事,你需要在动态生成它们时将 dataTableOutput 添加到你的 tabPanel,然后需要动态生成对应的renderDataTable.

To do what you want, you need to add dataTableOutput to your tabPanel as you dynamically generate them, and then you need to dynamically generate the corresponding renderDataTable.

在您的服务器中执行此操作:

Do this in your server:

library(DT) # need datatables package
server <- shinyServer(function(input, output, session) {

  output$mytabs <- renderUI({
    nTabs = length(input$decision)
    # create tabPanel with datatable in it
    myTabs = lapply(seq_len(nTabs), function(i) {
      tabPanel(paste0("dataset_",i),
        DT::dataTableOutput(paste0("datatable_",i))       
               )
      })

    do.call(tabsetPanel, myTabs)
  })

  # create datatables
  observe(
    lapply(seq_len(length(input$decision)), function(i) {
      output[[paste0("datatable_",i)]] <- DT::renderDataTable({
        as.data.frame(get(input$decision[i]))
      })
    })  
  )  
})

这篇关于R Shiny:如何将数据表添加到动态创建的选项卡中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆