Question

I am following the documentation for rpy2 here (http://rpy.sourceforge.net/rpy2/doc-2.1/html/graphics.html?highlight=lattice). I can successfully plot interactively using lattice from rpy2, e.g.:

iris = r('iris')
p = lattice.xyplot(Formula("Petal.Length ~ Petal.Width"),
                   data=iris)
rprint = robj.globalenv.get("print")
rprint(p)

rprint displays the graph. However, when I try to save the graph to pdf by first doing:

r.pdf("myfile.pdf")

and then my lattice calls, it does not work and instead results in an empty pdf. If I do the same (call r.pdf, then plot) with ggplot2 or with the R base, then I get a working pdf. Does lattice require anything special from within Rpy2 to save the results to a PDF file? The following does not work either:

iris = r('iris')
r.pdf("myfile.pdf")
grdevices = importr('grDevices')
p = lattice.xyplot(Formula("Petal.Length ~ Petal.Width"),
                   data=iris)
rprint = robj.globalenv.get("print")
rprint(p)
grdevices.dev_off()

Thank you.

Was it helpful?

Solution

you need some equivalent of dev.off() after the print command.

That is, in order to save your graphs to pdf, the general outline is:

pdf(...)
print(....)
dev.off()

Failing to call dev.off() will result in an empty pdf file.


from this source, it appears that the equivalent in rpy2 might be

grdevices.dev_off()

OTHER TIPS

The solution is to use:

robjects.r["dev.off"]()

For some reason the other variants do not do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top