tags:

views:

370

answers:

3

I love the Emacs ESS combination. I love sending lines, functions, regions, and buffers of code to the command line for evaluation without using the mouse.

However, I've noticed that the Eval Function command in Emacs is much slower than simply running source("fns.R"), where fns.R is the file that contains the function I want to evaluate.

Why is this the case?

+4  A: 

I am just guessing but when you say

  • source("fns.R") you are not involving Emacs/ESS at all and the computing time is just the time R takes to slurp in the file and to digest it -- probably very little, whereas

  • Eval Function passes a region to the Emacs interpreter which has to send it (presumably line-by-line) to the R engine which then digests it in piecemeal fashion.

and that would make the second approach slower.

Yet, in the grand scheme of things, who cares? I often send entire buffers or large regions, and that takes maybe a large part of a second? I still think---just as you say---that the ability for the (rich) editor and the underlying language to interact that way is something extremely powerful.

Kudos to the Emacs hackers and the ESS team.

Dirk Eddelbuettel
+1  A: 

If you wanted to execute your whole buffer - if you are in Unix/Linux, you can also start off your script with a shebang:

#!/usr/bin/Rscript

And make your file executable

chmod 744 myscript.r

(I recall reading Google likes their r scripts to end in .R but oh well...) and you can execute it this way:

./myscript.r

And, with arguments,

./myscript.r arg1 arg2

(which I have actually used to invoke an R function from a Matlab system call) and in your R file you might use

userargs = tail(commandArgs(),2)

to get arg1 and arg2. You can also do without the shebang:

R --no-save < myscript.r arg1 arg2

and so on. With Windows I recall it was

R CMD BATCH myscript.r

or something to that effect... I did notice a little delay when running commands through ESS (though I do love ESS dearly) so when I know I want to run the the whole buffer I sometimes start a shell in a window below the R script (where the R buffer would normally reside) and use the tricks above.

You can also use

echo 'source("myscript.r")' | R --no-save

as well - the benefit of using these methods over running 'source("myscript.r")' directly in R or an R buffer is that you are starting out with a clear workspace (though you should be careful that your .Rprofile will not be loaded unless you call 'source("~/.Rscript")' explicitly in 'myscript.r') so you can be sure that your script is self-contained (it calls the proper libraries, your lexically-scoped functions aren't referencing unintended variables in the global space that you forgot to remove, and so on).

Stephen
Yes, you could do that. Or you could simply use Ctrl-c Ctrl-l.
Eduardo Leoni
+4  A: 

I think the folks at ess list have better answers for you. But if you evaluate invisibly, the processing is much much faster. Try putting this in your .emacs file:

(setq ess-eval-visibly-p nil)
Eduardo Leoni