[R] How to dump plots as bas64 strings?

Patrick Meyer paterijk at gmail.com
Thu Mar 5 13:09:02 CET 2009


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.

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")
>




More information about the R-help mailing list