【发布时间】:2022-01-26 08:08:11
【问题描述】:
我发现自己面临一个问题:
我已经有了像这样的闪亮应用程序(服务器部分):
observe({
data <- my_function() %>%
filter(col1 == "X1")
h <- sort(unique(data$year))
nb <- length(h)
lst <- as.list(h)
output$plot_name <- renderUI({
plot_output <- lapply(1:nb,
function(n1) {
plotname <- paste0("plots", n1)
plotOutput(plotname)
})
do.call(tagList, plot_output)
})
my_data <- list()
i <- 0
for (i in 1:length(lst)) {
local({
my_i <- i
plotname <- paste0("plots", my_i)
my_data[[my_i]] <- data %>%
filter(year == lst[[my_i]])
output[[plotname]] <- renderPlot({
g <- ggplot(..........)
print(g)
})
})
}
})
这让我可以为给定的 X 每年输出图表。
但是我想删除过滤器并为每个 X 的每一年提供一个图表,因为我知道所有 X 的年数不同(X1:2000,X2:2000 和 2001,X3:2001等等......)。
因为我想动态确定图表的数量。
如果你有任何线索,那将非常有用。
提前致谢。
【问题讨论】:
-
能否提供
my_function()的内容让代码可复现? -
感谢@jpdugo17,您的评论。为了简化 my_function 返回一个像这样的数据表: Col1 = X1, X1, X2, X1, X3, X2, X2, X3; Col2 = 1、2、1、0、1、1、2、2;年 = 2000、2001、2000、2000、2000、2000、2002、2001; pts = 24, 36, 48, 24, 72, 24, 48, 24 。我的 ggplot 是 x = pts, y = col2 的简单图,图标题显示年份和相应的 X。
标签: for-loop shiny shinydashboard shiny-server shinyapps