[R] lexical scoping for scripts......

akshay kulkarni @k@h@y_e4 @end|ng |rom hotm@||@com
Sun Mar 19 19:33:09 CET 2023

Dear Duncun,
                         thanks for the reply....

So when I run a script in the system command line by R CMD BATCH, the objects created in the script cannot be stored in the workspace ,right? If yes, how to save them? Moreover, the only way to save the objects CREATED from the script permanently is to save them to the disk, right?

THanking you,
yours sincerely,
From: Duncan Murdoch <murdoch.duncan using gmail.com>
Sent: Saturday, March 18, 2023 11:49 PM
To: akshay kulkarni <akshay_e4 using hotmail.com>; R help Mailing list <r-help using r-project.org>
Subject: Re: [R] lexical scoping for scripts......

On 18/03/2023 1:57 p.m., akshay kulkarni wrote:
> Dear members,
>                               The documentation for source() says:
> Input is read and parsed from that file until the end of the file is reached, then the parsed expressions are evaluated sequentially in the chosen environment.
> What does this mean? I presume that any objects that are CREATED by the script are stored in the Global environment (if local = FALSE), but the rules for lexical scoping are the same as for functions, right?

No, assignments will happen in the "chosen environment" as well.

I'm not sure exactly what you mean about the rules for lexical scoping,
but I think the answer is yes.  So if you do the following:

Put this code in a file named "f.R":

   x <- 123
   f <- function() x

and you run this code in your global environment:

   x <- 456
   e <- new.env()
   source("f.R", local = e)

Then you'll find that x retains the value 456, and e$f() returns 123.

> Does the same apply for running the same script from the system command line by R CMD BATCH?

I don't think R CMD BATCH has any equivalent to the local argument.
Everything is evaluated in the global environment.

Duncan Murdoch

	[[alternative HTML version deleted]]

More information about the R-help mailing list