Dear Shiny-ers,
once more I return to you to figure out where I am going wrong in my attempts. I have a working script in shiny which generates some plots. The code runs on a 0.3.0 version of shiny, built on R 2.15.2 for 64-bit linux:
shiny /usr/lib64/R/library 0.3.0 NA R (>= 2.14.1) stats, tools, utils, datasets, methods, websockets (>= 1.1.6),
caTools, RJSONIO, xtable, digest NA markdown, Cairo, testthat NA NA GPL-3 2.15.2
I would, now, like to export those same plots to pdf, and offer my users the possibility of downloading them. After reading through the downloadButton/Link documentation, and the few threads in this forum, I've come up with the following setup.
# generates a path/string for the pdf filename
myFilename<-reactive(function(){sprintf("/var/tmp/Basename-%s.%s",input$jobName,'pdf')})
# the plot is generated in a (reactive) function called output$Plot
# the following lines, when inclued in the reactive function which generate the (lattice) plots, do work fine and produce a pdf in the expected folder.
# pdf(myFilename(), onefile=T, width=12,height=8)
# print(MySurfacePlot)
# dev.off()
This is how I tried to generate the pdf and connect it to the downloadHandler, within server.R
output$downloadPlot <- downloadHandler(
filename = myFilename(),
# a function that actually opens a connection to a pdf and print the result to it
content = function(FILE=NULL) { # this function must take at least an input variable, namely the filename of the file to save
pdf(
file=FILE, # hopefully this will catch the filename provided in the previous argument of the downloadHandler (filename=myFilename())
onefile=T, width=12,height=8)
print(output$Plot)
dev.off()
}
)
In the ui.R script, I insert the corresponding lines to tackle the downloadButton:
downloadButton('download_P.C_Plot', label = "Download plot as PDF")
When I load the page, the program seems to run fine up to the point where I click on the downloadButton (or Link), which instead of initiating a download opens a new windows where the following error message is printed:
invalid replacement for field ˜.label", should be from class "character" or a subclass (was class "NULL")
Is there something I didn't set correctly? Did I misunderstand the way the downloadHandler function works? Is the /var/tmp an issue for shiny (can it access outside its own app directory?)
Many thanks for any insights.
Luca