Ken S. Ken S. - 3 months ago 17
R Question

Why is source speed different from RStudio console line code?

I have a script with self-written functions (no plots). When I execute that script on the command line it takes ages to execute, but when I use

source("Helperfunctions.R")
it doesn't take more than a second.

Question: Where does the difference in speed come from?



I am aware of two differences between running code via
source
and through entering code at the
R
command line:

From
?source
:


Since expressions are not executed at the top level, auto-printing is not done.


Please, correct me if I'm wrong: Difference in speed is heavily affected when the code prints plots.
source
will ignore those calls (unless made specific with e.g.
print(plot)
) and the command line codes will print these plots.

And:


(...) the complete file is parsed before any of it is run


I have been working with
R
for a while now, but I am not a programmer. Is it possible that parsing speeds up the execution of my helper functions script by a factor of a hundred?

EDIT #1: I use RStudio, R version 3.2.3.

Answer

The issue is not source() vs. command line code. Instead, it is an issue of how RStudio sends code from the source pane to the console.

When I copy the content of Helperfunctions.R and run it in RGui (instead of RStudio), the code is executed with the same speed as when I use source("Helperfunctions.R") in RStudio. The problem posed in the question compares the execution time of source("Helperfunctions.R") with execution time of running its content in RStudio, in which case there is a huge discrepancy in execution time.

Apparently, lines of code always (?) require more execution time in RStudio than in RGui.

My understanding is that upon using source("Helperfunctions.R") in the RStudio source pane, the code is not actually sent to the RStudio console (which would have been slow), but is actually executed directly in the R language.