通过闪亮的 for 循环生成多个选项卡 table
generate mutiple tab table via for loop in shiny
我的真实数据在一个循环中生成多个 tables,我想把它们全部显示出来。
当我测试假数据时,我无法通过 for 循环生成多个选项卡 table,这是我的代码:
library(shiny)
df1<-data.frame(matrix(ncol = 5,nrow = 10,data = runif(50,0,100),dimnames = list(1:10,paste0("S",1:5))))
ui <- fluidPage(
fluidRow(
tabsetPanel(
tabPanel("S1",tableOutput("S1")),
tabPanel("S2",tableOutput("S2")),
tabPanel("S3",tableOutput("S3")),
tabPanel("S4",tableOutput("S4")),
tabPanel("S5",tableOutput("S5"))
)
),
##not working here
fluidRow(
tabsetPanel(
for(i in 1:ncol(df1)){
tabPanel(title = paste0("S",i),tableOutput(outputId = paste0("test_S",i)))
}
)
)
)
server <- function(input, output) {
output$S1<-renderTable(df1[1])
output$S2<-renderTable(df1[2])
output$S3<-renderTable(df1[3])
output$S4<-renderTable(df1[4])
output$S5<-renderTable(df1[5])
##not woking here
for(i in 1:ncol(df1)){
id<-paste0("test_S",i)
output[[id]]<-renderTable(df1[i])
}
}
shinyApp(ui = ui, server = server)
代码在 for 循环部分生成注意,我如何修改 for 循环部分以生成与我上面相同的结果table?
我会尝试使用 lapply
在 server
而不是 ui
中创建动态内容。这将包括 renderUI
以根据您的 data.frame(列数)创建动态的选项卡数量,以及 observe
以动态创建 renderTable
输出。让我知道这是否是您想要的。
library(shiny)
set.seed(42)
df1 <- data.frame(matrix(ncol = 5, nrow = 10, data = runif(50, 0, 100),
dimnames = list(1:10, paste0("S", 1:5))))
ui <- fluidPage(
uiOutput("mytabs")
)
server <- function(input, output) {
output$mytabs <- renderUI({
thetabs <- lapply(paste0('table_', seq_len(ncol(df1))),
function(x) {
tabPanel(x, tableOutput(x))
})
do.call(tabsetPanel, thetabs)
})
observe({
lapply(seq_len(ncol(df1)), function(x) {
output[[paste0("table_", x)]] <- renderTable({ df1[x] })
})
})
}
shinyApp(ui = ui, server = server)
我的真实数据在一个循环中生成多个 tables,我想把它们全部显示出来。
当我测试假数据时,我无法通过 for 循环生成多个选项卡 table,这是我的代码:
library(shiny)
df1<-data.frame(matrix(ncol = 5,nrow = 10,data = runif(50,0,100),dimnames = list(1:10,paste0("S",1:5))))
ui <- fluidPage(
fluidRow(
tabsetPanel(
tabPanel("S1",tableOutput("S1")),
tabPanel("S2",tableOutput("S2")),
tabPanel("S3",tableOutput("S3")),
tabPanel("S4",tableOutput("S4")),
tabPanel("S5",tableOutput("S5"))
)
),
##not working here
fluidRow(
tabsetPanel(
for(i in 1:ncol(df1)){
tabPanel(title = paste0("S",i),tableOutput(outputId = paste0("test_S",i)))
}
)
)
)
server <- function(input, output) {
output$S1<-renderTable(df1[1])
output$S2<-renderTable(df1[2])
output$S3<-renderTable(df1[3])
output$S4<-renderTable(df1[4])
output$S5<-renderTable(df1[5])
##not woking here
for(i in 1:ncol(df1)){
id<-paste0("test_S",i)
output[[id]]<-renderTable(df1[i])
}
}
shinyApp(ui = ui, server = server)
代码在 for 循环部分生成注意,我如何修改 for 循环部分以生成与我上面相同的结果table?
我会尝试使用 lapply
在 server
而不是 ui
中创建动态内容。这将包括 renderUI
以根据您的 data.frame(列数)创建动态的选项卡数量,以及 observe
以动态创建 renderTable
输出。让我知道这是否是您想要的。
library(shiny)
set.seed(42)
df1 <- data.frame(matrix(ncol = 5, nrow = 10, data = runif(50, 0, 100),
dimnames = list(1:10, paste0("S", 1:5))))
ui <- fluidPage(
uiOutput("mytabs")
)
server <- function(input, output) {
output$mytabs <- renderUI({
thetabs <- lapply(paste0('table_', seq_len(ncol(df1))),
function(x) {
tabPanel(x, tableOutput(x))
})
do.call(tabsetPanel, thetabs)
})
observe({
lapply(seq_len(ncol(df1)), function(x) {
output[[paste0("table_", x)]] <- renderTable({ df1[x] })
})
})
}
shinyApp(ui = ui, server = server)