0

I'm using

sapply(list.files('scritps/', full.names=TRUE),source)

to run 80 scripts at once in the folder "scripts/" and I do not know exactly how does this work. There are "intermediate" objects equally named across scripts (they are iterative scritps across 80 different biological populations). Does each script only use its own objects? Is there any risk of an script taking the objects of other "previous" script that has not been yet deleted out of the memory, or does this process works exactly like if was run manually sequentially one by one? Many thanks in advance.

shasha
  • 19
  • 5
  • Have you read about the `local` argument to `source`? – Martin Schmelzer Mar 24 '18 at 15:41
  • I see... I should use local = T for each script using its own specific environment, isn't it? – shasha Mar 24 '18 at 15:44
  • @MartinSchmelzer I doubt that has anything to do with the `local` argument... It determines what environment (global or local) the parsed expressions are evaluated, but OP is running `source` in the same environment. – ytu Mar 24 '18 at 15:50
  • Yes and his question is whether the evaluation of one script could effect another. So using `local` to make sure every script is sourced in its own environment ....... – Martin Schmelzer Mar 25 '18 at 04:32
  • @MartinSchmelzer Scripts don't have their own environments, do they? To make "every script is sourced in its own environment", one has to create as many environments as the script number like [this thread](https://stackoverflow.com/questions/39620669/source-script-to-separate-environment-in-r-not-the-global-environment) suggests. I don't see how it is more efficient, or more importantly, how it can answer the question "whether the evaluation of one script could effect another" per se. – ytu Mar 25 '18 at 06:31
  • Never said its more efficient o0 – Martin Schmelzer Mar 25 '18 at 11:38

2 Answers2

0

There are global variables that you can declare out a function. As it's name says they are global and can be re-evaluated. If you declare a var into a function it will be local variable and only will take effect inside this concrete function, it will not exists out of its own function. Example:

Var globalVar = 'i am global';
Function foo(){
    Var localVar = 'i don't exist out of foo function';
}

If you declared globalVar on the first script, and you call it on the latest one, it will answer. If you declared localVar on some script and you call it into another or out of the functions or in another function you'll get an error (var localVar is not declared / can't be found).

Edit:

Perhaps, if there aren't dependences between scripts (you don't need one to finish to continue with another) there's no matter on running them on parallel or running them secuentialy. The behaviour will be the same.

You've only to take care with global vars, local ones can't infer into another script/function.

JoelBonetR
  • 1,551
  • 1
  • 15
  • 21
  • Excuse me, is this written in R? – ytu Mar 24 '18 at 16:05
  • Nope it's an example with javascript. I could write the example with java, c, c++, c#, python, kotlin, php, vb... And it will take the same behaviour, local and global vars are common on all programming languages. There's no sense on delcaring a global var to use it only in one function and there's no use on create a local one if you need it out of the function/method – JoelBonetR Mar 24 '18 at 16:07
  • Its R. There are no dependences among scripts... so I think I can assume that they run independently. Just like the other response confirms. Thanks! – shasha Mar 24 '18 at 16:10
0

The quick answer is: each script runs independently. Imagine you run a for loop iterating through all the script files instead of using sapply - it should be the same in results.

To prove my thoughts, I just did an experiment:

# This is foo.R
x <- mtcars
write.csv(x, "foo.csv")

# This is bar.R
x <- iris
write.csv(x, "bar.csv")

# Run them at once
sapply(list.files(), source)

Though the default of "local" argument in source is FALSE, it turns out that I have two different csv files in my working directory, one named "foo.csv" with mtcars data frame, and the other named "bar.csv" with iris data frame.

ytu
  • 1,822
  • 3
  • 19
  • 42
  • I see. So independently of F or T argurment, scripts run independently. Thanks! – shasha Mar 24 '18 at 16:17
  • In fact, when I use local = T, there are problems at the end of each script and fails... Bur runs o when = F. – shasha Mar 24 '18 at 16:33
  • The "local" argument is _not_ indicating whether each script should be executed independently. It's really about global and local environment scoping in R language, which has little to do with your question. As for the reasons why `local = T` fails, I can't give straight answers since I don't know how exactly your code is designed. Check [this chapter](http://adv-r.had.co.nz/Environments.html) if you are interested in environments. – ytu Mar 24 '18 at 18:32