Emacs ESS: Eval region vs. source ()

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

However, I noticed that the Eval Function command in Emacs is much slower than just starting source("fns.R") , where fns.R is the file containing the function I want to evaluate.

Why is this so?

+4
source share
3 answers

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

 (setq ess-eval-visibly-p nil) 
+8
source

I just guess, but when you say

  • source("fns.R") you don’t participate in Emacs / ESS at all, and the computation time is the time that R takes to split the file and digest it - it may be very small, whereas

  • Eval Function passes the region to the Emacs interpreter, which should send it (presumably in rows) to the R engine, which then digests it in parts.

and this will make the second approach slower.

However, in the grand scheme of things, who cares? I often send entire buffers or large regions, and it takes maybe most of a second? I still think that, as you say, the ability of the (rich) editor and the main language to interact in this way is something extremely powerful.

Kudos to Emacs hackers and the ESS team.

+4
source

If you want to execute your entire buffer - if you are running Unix / Linux, you can also run your script with shebang:

 #!/usr/bin/Rscript 

And make your executable

 chmod 744 myscript.r 

(I remember that Google loves their r-scripts to end in .R, but ok ...) and you can execute it like this:

 ./myscript.r 

And, with arguments,

 ./myscript.r arg1 arg2 

(which I actually used to call the R function from the Matlab system call), and in your R file, you can use

 userargs = tail(commandArgs(),2) 

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

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

etc. With Windows, I remember that it was

 R CMD BATCH myscript.r 

or something like that ... I noticed a slight delay when running commands through ESS (although I really love ESS), so when I know that I want to run the entire buffer, I sometimes run the shell in the window below the R script (where usually there will be a buffer R) and use the tricks above.

You can also use

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

- The advantage of using these methods when running 'source ("myscript.r")' directly in R or the R buffer is that you start with a clear work area (although you have to be careful that your. Rprofile will not load if you will not name the "source (" ~ / .Rscript ") explicitly in" myscript.r "), so you can be sure that your script is autonomous (it calls the appropriate libraries, lexically -scoped do not refer to unintended variables in global space that you forgot to delete, etc.).

+1
source

All Articles