gpt4 book ai didi

R Shiny : How to add data tables to dynamically created tabs

转载 作者:行者123 更新时间:2023-12-04 21:09:03 25 4
gpt4 key购买 nike

我目前正在尝试创建动态创建的数据表,每个表都有自己的选项卡。选项卡的数量由用户决定。我使用了来自 this post 的代码作为框架。

我能够动态创建选项卡,但我不知道如何将数据表添加到选项卡。数据表也由用户输入决定。
因此,例如,在 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')
)
)
))

服务器
 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)
})
})

因此,我希望将相应的数据集分别呈现到每个选项卡下的数据表中。

在此先感谢您的帮助!

最佳答案

做你想做的,你需要添加dataTableOutput给您的 tabPanel当你动态生成它们,然后你需要动态生成对应的renderDataTable .

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

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 : How to add data tables to dynamically created tabs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39276104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com