I am forced to output multiple plots not in the static device. But I dynamically create variable number of plots in Shiny with renderPlot. I can successfully create separate pdf files for each device using the function pdf() within the renderPlot.
I have tried to make multipage pdf file as it is specified here and in other similar posts. However in my case the created pdf file has the damaged contents (size of 7 kb) and can't be open. My code after simplification looks as follows.
for (PlotI in 1:PlotN)
{
poObject <- plotOutput(outputId = PlotI)
insertUI(selector = '#AnyPlaceHolder', where = "beforeBegin",
ui = poObject, multiple = FALSE, immediate = FALSE)
} # for PlotI
fnPDF <- paste0(tempdir(), '\\', 'plot.pdf')
pdf(file = fnPDF, width = 7, height = 4, onefile = TRUE, title = 'R output',
paper = 'a4', pointsize = 1/PlotResolution, compress = TRUE)
for (PlotI in 1:PlotN)
{
local(
{
PlotJ <- PlotI
output[[PlotJ]] <- renderPlot(
{
opar <- par(no.readonly = TRUE)
par(mar = c(2,4,2,0.5))
plot(AnyTimeSeries[[PlotJ]])
par(opar)
}
) # renderPlot
}
) # local
} # for PlotI
dev.off()
What is wrong?