Skip to content

Instantly share code, notes, and snippets.

@bearloga
Created February 6, 2015 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bearloga/c9eea0d3179b0b7fb258 to your computer and use it in GitHub Desktop.
Save bearloga/c9eea0d3179b0b7fb258 to your computer and use it in GitHub Desktop.
Sometimes you just want to send a report to someone as a single HTML file (e.g. when PDF isn't an option) and you want to avoid the hassle of external images. This code lets you convert a plot in R into base64 string that you can include in the HTML source.
library(base64) # install.packages('base64')
pngfile <- tempfile()
png(pngfile,width=600,height=600,pointsize=12) # adjust as needed
# Plotting code...
dev.off()
# Prints the HTML code to console
cat(img(pngfile))
# Copy and paste the whole output into your HTML source to embed the image
unlink(pngfile) # clean up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment