15
votes

I have a script with self-written functions (no plots). When I copy-paste that script into the R-Studio console, 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 the source() function vs. entering code at the R-Studio console:

From ?source:

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

The way I understand this: source() will not plot graphs (unless made specific with e.g. print(plot)), while the R Studio console codes will always plot graphs. I'm sure this will affect the speed of execution to a certain degree, but this seems irrelevant in my case, because there are barely any plot calls.

And:

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

I have been working with R for a while now, but I'm not sure whether this relevant for the speed-issue I'm having. Is it possible that completely parsing all code "before any of it is run" speeds up the execution of my helper functions script by a factor of a hundred?

Edit: I'm using R version 3.2.3.

1
R must parse your code regardless if you source it all at once or if you do it line by line. Are you using Rgui? What exactly takes long? - Roland
I use RStudio. I always start my scripts with source("Helperfunctions.R"), this is quick. However, when I copy the content of Helperfunctions.R and execute the code directly, it takes ages for the code to run. Does that help, @Roland ? - KenHBS
I've observed the same and don't know the reason. I think there is even something about it in the RStudio support forums. - Roland
Do you observe the same performance degradation when you paste your commands in a plain R shell? If no, then this is definitely RStudio (where I also observed performance issues executing lines of code from the editor). - Martin Nyolt
@MartinNyolt @Roland the R shell is certainly much (!!) faster than RStudio. Please post it as a solution. Any chance there is a simple answer as to why RStudio is so slow in this case? Are there cases in which RStudio is actually faster? - KenHBS

1 Answers

10
votes

The issue is not source() vs. console 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 nearly the same speed as when I use source("Helperfunctions.R") in RStudio.

Apparently, lines of code always (?) require more execution time in RStudio than in RGui. Even though you may usually not notice the time-difference when executing a couple of lines in the console, it seems to make a huge difference when, say, 3.000 lines of code are being executed in the R Studio console at once.

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.