I created a shiny app and now i want to plot a chart to pdf. So, is there any way to print a googlevis Chart to pdf in R.
I know its not possible directly, as stated in the help pages. But is there a way to print a static image (similar to a screenshot)? If possible without sweave/knitr?
Thank you in advance
You can do this using wkhtmltopdf, which you need to install and possibly add to your system path. I have got this working for other googlevis objects, where in some cases I did not need the --enable-javascript --javascript-delay option....
output$downloadmap <- downloadHandler("mymap.pdf" ,
content = function(file) {
#print gmap googlevis R object to a html file
print(gmap, file="gmap.html")
#call to wkhtmltopdf installed on server/pc to convert html file to pdf.
#add a delay otherwise (i got an) empty plot
system("wkhtmltopdf --enable-javascript --javascript-delay 2000 gmap.html gmap.pdf")
#copy pdf file to output
file.copy("gmap.pdf", file)
#remove created files from local storage
file.remove("gmap.pdf")
file.remove("gmap.html")
}
)
Related
I have the dynamic plot that I want. I have a problem knowing how to export the plot to an HTML file so I can share the plot with others. I tried what I have below but it doesn't save the file.
# save the widget
library(htmlwidgets)
setwd("C:/Users/12083/Desktop/")
saveWidget(p, file=paste0( getwd(), "C:/Users/12083/Desktop/ggplotlyAreachart.html"))
use auto_open=False:
plotly.offline.plot(fig, filename = 'filename.html', auto_open=False);
As of version 4.0, the write_html() function is available on go.Figure objects as well, so you can do fig.write_html("path/to/file.html") directly now too.
you can see the documentation here: https://plotly.com/python/interactive-html-export/
I have 3 R plots saved as pdf files (upper_left.pdf, upper_right.pdf, lower.pdf) as vector graphic and want to make a one-page pdf file and arrange them on it as follows:
What I have tried already
I have tried reading the pdf's using magick::image_read_pdf and appending them using magick::image_append. More specifically,
library(magick)
panel.ul <- image_read_pdf("upper_left.pdf")
panel.ur <- image_read_pdf("upper_right.pdf")
panel.l <- image_read_pdf("lower.pdf")
whole <- c(panel.ul, panel.ur) %>%
image_append() %>%
c(panel.l) %>%
image_append(stack = TRUE)
The first issue is magick::image_read_pdf imports the plot as png (if I'm right, not vector graphic though).
magick::image_append also 'works' and gives me what I want in viewer pane (in RStudio, next to Help).
I then try to save them using export::graph2pdf(whole), but it gives me a blank page.
So, if I am to use magick, there are two issues that need to be solved:
importing plots as vector graphic objects (do not know the technical term in R)
Exporting the stacked plot to a vector pdf file.
How can I solve it? thanks in advance.
You're basically done. You only need to add
plot(whole) # plot the external object generated in ImageMagick to R's plotting device
savePlot(type = "pdf") # saves the current plotting device to a pdf file.
You will find your plot in your workoing directory called "Rplot.pdf".
savePlot has many options to customize your pdf output. Make sure to check ?savePlot.
To recreate your scheme from above youll need to temporarily save the upper panel as a separate pdf before you paste it to on top of the lower panel:
whole2 <- image_append(c(panel.ul, panel.ur))
plot(whole2)
savePlot("whole2.pdf", type = "pdf")
If the upper and lower panel do not look proportionate you can use the heght and width parameters of savePlot to adjust the size of the first pdf.
panel.upr <- image_read_pdf("whole2.pdf")
final <- image_append(c(image_append(panel.upr),panel.l), stack = TRUE)
plot(final)
savePlot("final.pdf", type = "pdf")
I'm using R with imagemagick to crop some borders from a pdf file. I'm executing the following commands:
library(magick)
pdf_total <- image_read_pdf(path = "file1.pdf")
pdf_cropped <- image_crop(pdf_total,"3000x1500")
After this process I have a perfect cropped file, but my problem occurs when I try to save the file to a new pdf file. What is the correct procedure to save this converted pdf?
My final solution is:
library(magick)
pdf_total <- image_read_pdf(path = "file1.pdf")
pdf_cropped <- image_crop(pdf_total,"3000x1500")
for(i in seq(1,length(pdf_cropped))){
plot(pdf_cropped[i])
}
dev.off()
In this case I made a for loop to save all the pages, if you pass plot(pdf_cropped) the result is a pdf with a single page (first picture).
I have an interactive Rmarkdown document which embeds a few shiny apps that generates graphs based on user inputs.
After I am happy with the graphs generated, I would like to save the report (so that it becomes static).
I have opened the report in my browser (Chrome), and tried printing to pdf. It sort of works, however some figures are cut into two by the page break.
Does anyone know what is the best way to print/save such reports?
I think it's a bit tricky but this is what i use on my app to save html plot on pdf or png format.
Instal wkhtmltopdf
wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command
line tools to render HTML into PDF and various image formats using the
Qt WebKit rendering engine. These run entirely "headless" and do not
require a display or display service.
Use it in R
This allow you to convert a htmlfile into pdfor img.
In my ShinyApp i use inside a downloadHandler() function somthing like this :
system("wkhtmltopdf --enable-javascript --javascript-delay 2000 plot.html plot.pdf")
I think for your example you could simply convert your html by using :
system("wkhtmltopdf yourFile.html yourFile.pdf")
You can give a download button on the ui.R as:
downloadButton('report', 'Download PDF')
And the corresponding server.R code:
library(knitr)
output$report = downloadHandler(
filename = function() {
paste("Report_", <date/identifier>, ".pdf", sep="")
},
content = function(file) {
rnw <- normalizePath('input.rnw') # assuming you wrote the code to display the graphs etc in this file
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(rnw, 'input.rnw')
out = knit2pdf(rnw, clean=TRUE)
file.rename(out, file)
}
)
Follow up from :
https://groups.google.com/forum/#!topic/shiny-discuss/u7gwXc8_vyY
I have the exact same R Shiny structured program as the user in the post, except I am using the googleVis Shiny package for my plots. For example, here is a plot of a gVis table:
output$gvisTable <- renderGvis( {
if (is.null(dataset))
return(NULL)
gvisTable(dataset)
})
EDIT:
My code to save ggplots:
server.R
name <- paste0(input$filename, ".png")
print(p)
if(input$savePlot) {
ggsave(name, p, type="cairo-png")
}
ui.R (in sidePanel)
wellPanel(
textInput('filename', "Filename"),
checkboxInput('savePlot', "Check to save")
)
This is what I am using to try to save gVis plots:
name <- paste0(input$filename, ".png")
if(input$savePlot) {
png(name, *INPUT GVIS PLOT HERE*, type="cairo-png")
dev.off()
}
This does not work: I get the error: 'non-numeric argument to binary operator'
I can't find a way to allow the user to download a gVis plot. I can't use the method in the linked post because you cannot 'print' a gVis plot.
Also, the files are locally saved to my R working directory, but I plan to upload this app to the web. Where would the files be saved for the user? Ideas?
I see no png method documented for gvis objects.
There are two ways to "print" documented in the help page linked from the googleVis main Index page for print.gvis. The default method (when the tag is NULL or "html") is to send a Java script page to your browser. The other (when you set tag="chart" is to construct an html file with the name you give it. I suppose you could arrange something with system commands sent to a running instance of a particular browser, but for that you should use different SO tags so that you attract the interest of people using the same software as you. (I'm using a Mac with Firefox and having no difficulties seeing the "printed" output.) If you plan to "upload it to the web", then you need to have a server. Is my impression this is a bit new for you correct?
print(GTM, tag="chart", file="test.html")
#created in my working directory