[R] How to dump plots as bas64 strings?

Duncan Temple Lang duncan at wald.ucdavis.edu
Thu Mar 5 19:27:10 CET 2009

Patrick Meyer wrote:
> Here's the solution I'm using now, but it is not very "clean":
> png("out.png")
> plot(c(1,2,3))
> dev.off()
> system("base64 -w0 out.png > out.base64")
> b64txt <- readLines(file("out.base64","rt"))
> It would be nice to have an internal solution, even if this exactly does
> what I want.

I'm not certain which bit of this you are referring to when you say
"internal".  It would be covenient to have graphics devices be able to 
write to a buffer as well a  file and it wouldn't be terribly difficult
to do, but hasn't been a big enough problem so far.

As far calling the executable base64, why not use the function
in either the caTools or RCurl packages.


> Patrick
> Gabor Grothendieck wrote:
>> On Wed, Mar 4, 2009 at 9:17 AM, Patrick Meyer <paterijk at gmail.com> wrote:
>>> Hello
>>> My question might sound awkward, but I am looking for a way to somehow
>>> convert a plot in R into a base64 string.
>>> Here's an idea, but it is not at all satisfying.
>>> 1. write the plot to the harddisk:
>>> ---------------------------
>>> png("toto.png")
>>> plot(c(1,2,3))
>>> dev.off()
>>> ---------------------------
>>> 2. somehow reload that file from the disk and transform it into a base64
>>> string:
>>> ---------------------------
>>> bin<-readBin(file("toto.png","rb"), raw(), n=1000,endian = "little")
>> This isn't much of an improvement and does not even address the main
>> concern but as no one has answered note that we can pass the filename
>> as a character string and it will get "rb" automatically and we can
>> just use "raw" if we like.  Also n can be any size larger than the file
>> size (see ?readBin) so if the file size is 10000 or less:
>> readBin("toto.png", "raw", n = 10000, endian = "little")
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list