Question

Is there a way to preload libraries for the R instance that rpy2 talks to? I am spending 25-30% of my response time (about .5s per chart) in importr calls to lattice or grdevices, and would like to cut down if possible.

Code snippet:

grdevices = importr('grDevices')
importr('lattice')

imagefile = File(open('1d_%s.png' % str(uuid4()), 'w'))
grdevices.png(file=imagefile.name, type='cairo',width=400,height=350)

rcmd="""
print(
    xyplot(yvec~xvec,labels=labels,type=c('p','r'),
            ylab='%s',xlab='%s'
            )
)"""% (y_lab, x_lab)
robjects.r(rcmd)
grdevices.dev_off()

imagefile.close()

If I do not invoke importr("lattice"), robjects.r freaks at the "xyplot(..." call I make later. Can I use R_PROFILE or R_ENVIRON_USER to speed up the lattice and grdevices calls?

Was it helpful?

Solution

importr is a pretty high level function, trading performance for ease-of-use. It does a lot beside just loading an R package. It also maps all R objects in that package to Python (rpy2) objects. That effort is lost when doing importr('lattice') in your script, if the result is not used.

Beside that, importing packages in R itself is not without a cost (for the larger R packages with S4 class definitions, you this can be noticeable when the script is short). rpy2 can't do much about this.

Using R variables such as R_PROFILE is possible, but this was not enabled by default before very recently. How to enable it is on SO (here).

Now, here importr is taking "only" 25% of the response time. Optimization efforts focusing on this will not be able to make it more than 25% faster (and that's a very optimistic limit). Interpolating data into a string to evaluate it as R code after that is not very optimal (as warned in the documentation for rpy2 ). Consider calling the R function through rpy2, passing the data as anything exporting the buffer interface (for example).

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