我正在尝试将存储在列表中的多个ggplots导出到单个pdf中。不知何故我无法正常工作。
在我看来,问题在于使用反应性内容使用lapply生成列表,因为当我使用静态数据集时,一切都很好。使用反应性数据集时,此方法不起作用。
这可行:
PlotDataFilt1<-filter(readRDS(paste0(get_lookup_path(),"/PlotData.rds")),Instelling=="agb")
plot_list <-lapply(as.list(unique(PlotDataFilt1$variabele)), function(x) {
ggplot(PlotDataFilt1%>%filter(variabele==x),
aes(x=jaar, y = waarde, group=Instelling ,colour=Instelling),tooltip = c("x","y","group")) +
geom_point()+
geom_line() +
labs(x=NULL,y=NULL)+
ggtitle(x)+
theme(axis.text.x = element_text(angle = 45, hjust = 1, face="bold", size=8))+
theme(legend.title=element_blank()) +
theme(legend.position="bottom") +
scale_y_continuous(labels=function(n){format(n, scientific = FALSE)})
})
output$downloadPlot = downloadHandler(
filename = 'graphs.pdf',
content = function(file) {
#print(plot_list()[[1]])
pdf(file)
arrangeGrob(
for (i in 1: length(plot_list)) {
print(plot_list[[i]]) },
ncol =length(plot_list)
)
dev.off()
}
)
这不是:
plot_list <- observe({
plot_list <-lapply(as.list(unique(PlotDataFilt()$variabele)), function(x) {
ggplot(PlotDataFilt()%>%filter(variabele==x),
aes(x=jaar, y = waarde, group=Instelling ,colour=Instelling),tooltip = c("x","y","group")) +
geom_point()+
geom_line() +
labs(x=NULL,y=NULL)+
ggtitle(x)+
theme(axis.text.x = element_text(angle = 45, hjust = 1, face="bold", size=8))+
theme(legend.title=element_blank()) +
theme(legend.position="bottom") +
scale_y_continuous(labels=function(n){format(n, scientific = FALSE)})
})
return(plot_list)
})
output$downloadPlot = downloadHandler(
filename = 'graphs.pdf',
content = function(file) {
#print(plot_list()[[1]])
pdf(file)
arrangeGrob(
for (i in 1: length(plot_list)) {
print(plot_list[[i]]) },
ncol =length(plot_list)
)
dev.off()
}
)
编辑:诀窍是将所有内容放入downloadhandler中,如下所示:
output$downloadPlot = downloadHandler(
filename = paste0("Plots_PaazPuk_", format(Sys.time(), "%Y%m%d_%H%M%S_"), ".pdf"),
content = function(file) {
plot_list <-lapply(as.list(unique(PlotDataFilt()$variabele)), function(x) {
ggplot(PlotDataFilt()%>%filter(variabele==x),
aes(x=jaar, y = waarde, group=Instelling ,colour=Instelling),tooltip = c("x","y","group")) +
geom_point()+
geom_line() +
labs(x=NULL,y=NULL)+
ggtitle(x)+
theme(axis.text.x = element_text(angle = 45, hjust = 1, face="bold", size=8))+
theme(legend.title=element_blank()) +
theme(legend.position="bottom") +
scale_y_continuous(labels=function(n){format(n, scientific = FALSE)})
})
pdf(file)
arrangeGrob(
for (i in 1: length(plot_list)) {
print(plot_list[[i]]) },
ncol =length(plot_list)
)
dev.off()
}
)
'''