Ken S. Ken S. - 1 month ago 6x
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

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
and through entering code at the
command line:


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.
will ignore those calls (unless made specific with e.g.
) and the command line codes will print these plots.


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

I have been working with
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.


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.