Reputation:
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.
Upvotes: 2
Views: 827
Reputation:
The solution is to use:
robjects.r["dev.off"]()
For some reason the other variants do not do the trick.
Upvotes: 0
Reputation: 55360
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()
Upvotes: 2