Rstudio calls source() when saving script
Are you sure "Source on save" box is unchecked?
What exactly does Source on Save mean or do?
This is kind of a shortcut to save and execute your code. You type something, save the script and it will be automatically sourced.
Very useful for short scripts but very annoying for time consuming longer scripts.
So sourcing is basically running each line of your file.
EDIT:
SO thinking of a scenario where this might be useful...
You developing a function which you will later put into a package... So you write this function already in an extra file but execute the function for testing in the command line...
Normally, you have to execute the whole function again, when you changed something. While using "Source on Save" the function will be executed and you can use Ctrl + 2
to jump into command line and test the function directly.
Since I am working with R, my datasets are much bigger. But I am remembering starting coding in python and vi, I updated my setting in a way to execute the code on save, since these little scripts where done in less then 10 seconds...
So maybe it is just not standard to work with small datasets... But I can still recommend it, for development, to use only 10% of a normal dataset. It will speed up the graphics creation and a lot of other things as well. Test it with the complete dataset every now and then.
Avoid RStudio to execute code when saving
Source on Save
is an RStudio option, located between the tabs and the top of the source pane. If the code is executed on save, this option is probably turned on (right panel). Thus, the option need to be turned off by unchecking the option (left panel).
RStudio fails when run through Source as Local Job
If you want to have it available in the same environment, you can try to use the local = TRUE
source("s1.R", encoding = "UTF-8", local = TRUE)
RStudio, difference between Run and Source on the result of a script
essai
is assigned in env
.
But env
is defined as parent.frame()
.
When you use Run, parent.frame()
gives <environment: R_GlobalEnv>
.
From source
, with the default local = FALSE
, your script is also executed in "The global environment .GlobalEnv
, more often known as the user's workspace" (quoting from help(".GlobalEnv")
), which you can check if you add environment()
to your script.
But (again!). Even though the expressions are executed in .GlobalEnv
, they are evaluated with calls to other (probably deeply nested) other functions from the R code of source()
. And that is what you get with parent.frame()
, the calling environment of the evaluation function, which is itself an execution environment created on the fly inside the call to source()
.
You can check it if you print parent.frame()
in your script, you'll get a different value each time, for instance <environment: 0x40dca18>
, then <environment: 0x53be0f8>
, then...
Well, enough confusion.
When you do ls.str( essai )
with Run, essai
is easily found in .GlobalEnv
, which is equal to env
!
When you use source()
, essai
cannot be found in .GlobalEnv
or any enclosing environment, since it belongs to <environment: 0x53be0f8>
or whatever.
You can fix it with ls.str(get("essai", envir = env))
Related Topics
Create an Arrow with Gradient Color
R Sum Every K Columns in Matrix
Date-Time Differences Between Rows in R
Draw Histograms Per Row Over Multiple Columns in R
How to Save the Wordcloud in R
Find Overlapping Regions and Extract Respective Value
Counting Non-Blank Cells for Selected Columns
What Is Your Preferred Style for Naming Variables in R
Embedding Googlevis Charts into a Web Site
How to 'Compress' an Lm() Object for Later Prediction
"Unpacking" a Factor List from a Data.Frame
Display Duplicate Records in Data.Frame and Omit Single Ones
Regex; Eliminate All Punctuation Except
How to Calculate a Table of Pairwise Counts from Long-Form Data Frame