【发布时间】:2019-02-24 02:36:53
【问题描述】:
我有一个数据框,在这种情况下,我想根据年份过滤和创建表格。我现在有 4 年了。所以我想创建 4 个新表并在闪亮的应用程序上分别显示它们。我确实得到了循环的一部分并传递了过滤器变量,但是如何创建 4 个新表并在 UI 中显示它们.我能够获得动态标签面板,但库(闪亮)
library(shinyWidgets)
library(shinydashboard)
library(DT)
sidebar <- dashboardSidebar(
sidebarMenu(id = "tab",
menuItem("1", tabName = "1")
)
)
body <- ## Body content
dashboardBody(box(
uiOutput('mytabs')
))
ui <- dashboardPage(dashboardHeader(title = "Scorecard"),
sidebar,
body)
# Define the server code
server <- function(input, output,session) {
df <- data.frame(structure(list(`Mazda` = c(21000,20000,21500,24000), `Honda` = c(21500,20500,22000,24500)
, Sales = c(2017,2015,2016,2014)
)
, class = "data.frame", row.names = c(NA, -4L)))
toAdd <- as.vector(df$Sales)
for(i in length(toAdd)){
print(length(toAdd))
output[[paste0("datatable_",i)]] <- DT::renderDataTable({
df %>% filter(Sales == toAdd[i])
})
#}
# for(i in 1:length(toAdd)){
output$mytabs <- renderUI({
nTabs = length(toAdd)
# create tabPanel with datatable in it
myTabs = lapply(seq_len(nTabs), function(i) {
tabPanel(paste0("dataset_",toAdd[i]),
DT::dataTableOutput(paste0("datatable_",i))
)
})
do.call(tabsetPanel, myTabs)
})
}
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
for(i in length(toAdd))应该是for(i in 1:length(toAdd))或for(i in seq_along(toAdd))。 -
谢谢@StéphaneLaurent。但现在它只过滤
2014而不是向量中的任何其他值。