How would I use the same functions carried out by:
to export a plot produced in an shiny app via downloadHandler?
I have tried using variants of Cairo pdf output functions, including cairo_pdf in a shiny app but have not been able to replicate the desired output as produced by the above. All I have been able to produce are blank plots, error messages, and low quality plots with the text and point resizing and aliasing all wrong.
Here are the relevant components of the latest version of the code, with the most informative error message I have yet received:
library(shiny)
library(Cairo)
library(grDevices)
library(ggbiplot)
library(vegan)
ui <- fluidPage(
mainPanel(
tabsetPanel(
tabPanel("NMS Ordination", plotOutput("ordplot"),
downloadLink("downloadPlot", "Download Plot")
)
)
server <- function(input, output, session){
options(shiny.usecairo=T)
plotInput <- function(){
file <- tempfile(fileext = '.pdf')
pdf(file)
cairo_pdf(filename = "file",
width = 18, height = 10, pointsize = 12, family = "sans", bg = "transparent",
antialias = "subpixel",fallback_resolution = 300)
vegan::ordiplot(nms_graph) #Note that this is not the exact code (just for simplicity here)
dev.off()
dev.copy2pdf(file="file")
list(src = "file")}
output$ordplot <- renderPlot({
vegan::ordiplot(nms_graph) #Note again that this is not the exact code (just for simplicity here) - the plot is successfully rendered in the UI, but the quality is too low to useable, thus requiring a high quality download
})
output$downloadPlot <- downloadHandler(
filename = function(){paste(input$dataset, '.csv', sep = '')},
content = function(file){plotInput()},
contentType = "application/pdf",
outputOptions(output, "downloadPlot", suspendWhenHidden=FALSE)
)
}
shinyApp(ui=ui, server=server)
The error returned is as follows:
Listening on http://127.0.0.1:3133
Warning: Error in .subset2(x, "impl")$outputOptions: downloadPlot is not in list of output objects
55: stop
54: .subset2(x, "impl")$outputOptions
53: outputOptions
47: server [/Users/xxx/yyy/zzz/app.R#214]
Error in .subset2(x, "impl")$outputOptions(name, ...) :
downloadPlot is not in list of output objects
I'd change your downloadHandler's filename argument to end in .pdf, not .csv
In plotInput, add a file parameter to the function signature, and get rid of the line `file <- tempfile(fileext = '.pdf')
In the rest of plotInput, everywhere you have "file", it should be file (no double quotes). Also, you can remove the last line, the one that returns the list.
In downloadHandler's content function, call plotInput(file).
Listening on http://127.0.0.1:5809
Warning: Error in as.character: cannot coerce type 'closure' to vector of type 'character'
80: self$downloads$set
79: shinysession$registerDownload
78: origRenderFunc
77: output$downloadPlot
1: runApp
On running the app, the error message is displayed in place of the download link (which should read: "Download Plot") for the desired plot as shown in this screenshot: