R Shiny:如何将数据表添加到动态创建的选项卡
R Shiny: How to add data tables to dynamically created tabs
我目前正在尝试创建动态创建的数据表,每个表都有自己的选项卡。选项卡的数量由用户决定。我使用了 this post 中的代码作为框架。
我可以动态创建选项卡,但我不知道如何将数据表添加到选项卡。数据表也由用户输入确定。
因此,例如,在 ui.R 中,用户可以选择他们想要查看的数据集:
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')
)
)
))
server.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)
})
})
所以,我想将对应的数据集分别渲染到每个标签下的数据表中。
提前感谢您的所有帮助!
做你想做的,你需要在你的tabPanel
中添加dataTableOutput
,因为你动态生成它们,然后你需要动态生成相应的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]))
})
})
)
})
我目前正在尝试创建动态创建的数据表,每个表都有自己的选项卡。选项卡的数量由用户决定。我使用了 this post 中的代码作为框架。
我可以动态创建选项卡,但我不知道如何将数据表添加到选项卡。数据表也由用户输入确定。 因此,例如,在 ui.R 中,用户可以选择他们想要查看的数据集:
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')
)
)
))
server.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)
})
})
所以,我想将对应的数据集分别渲染到每个标签下的数据表中。
提前感谢您的所有帮助!
做你想做的,你需要在你的tabPanel
中添加dataTableOutput
,因为你动态生成它们,然后你需要动态生成相应的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]))
})
})
)
})