Dear Duncun, What if there is no interactive "session" running? I will be running my scripts automatically from crontab in Linux. THanking you, Yours sincerely, AKSHAY M KULKARNI ________________________________ From: Duncan Murdoch <murdoch.duncan at gmail.com> Sent: Monday, March 20, 2023 12:20 AM To: akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list <r-help at r-project.org> Subject: Re: [R] lexical scoping for scripts...... On 19/03/2023 2:33 p.m., akshay kulkarni wrote:> 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?The objects you create *will* appear in the workspace of the session that's running. They won't be saved to disk automatically so they'll disappear at the end of the BATCH run. You can use various functions (save(), save.image(), saveRDS(), writeLines(), etc.) to write them to disk if you don't want them to disappear. Duncan Murdoch> > THanking you, > yours sincerely, > AKSHAY M KULKARNI > ------------------------------------------------------------------------ > *From:* Duncan Murdoch <murdoch.duncan at gmail.com> > *Sent:* Saturday, March 18, 2023 11:49 PM > *To:* akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list > <r-help at 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]]
What do _you_ mean when you use the term "interactive"? Because R distinguishes between executing code in a function and executing code from the global environment, but it does not care whether a person is doing the typing or not. I get the feeling that you think of your R code in terms of "scripts" when you should be thinking of your code in terms of functions. What parameters do you give to them, and what values do they return. Where you put those return values is up to you... how do you save them now? (Depending on auto-saving to .Rdata files is not a good idea in any case, but if you choose to use save() to specific filename.Rdata files works the same whether you type it interactively or use R CMD BATCH. On March 19, 2023 11:55:37 AM PDT, akshay kulkarni <akshay_e4 at hotmail.com> wrote:>Dear Duncun, > What if there is no interactive "session" running? I will be running my scripts automatically from crontab in Linux. > >THanking you, >Yours sincerely, >AKSHAY M KULKARNI > >________________________________ >From: Duncan Murdoch <murdoch.duncan at gmail.com> >Sent: Monday, March 20, 2023 12:20 AM >To: akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list <r-help at r-project.org> >Subject: Re: [R] lexical scoping for scripts...... > >On 19/03/2023 2:33 p.m., akshay kulkarni wrote: >> 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? > >The objects you create *will* appear in the workspace of the session >that's running. They won't be saved to disk automatically so they'll >disappear at the end of the BATCH run. You can use various functions >(save(), save.image(), saveRDS(), writeLines(), etc.) to write them to >disk if you don't want them to disappear. > >Duncan Murdoch > > >> >> THanking you, >> yours sincerely, >> AKSHAY M KULKARNI >> ------------------------------------------------------------------------ >> *From:* Duncan Murdoch <murdoch.duncan at gmail.com> >> *Sent:* Saturday, March 18, 2023 11:49 PM >> *To:* akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list >> <r-help at 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]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
On 19/03/2023 2:55 p.m., akshay kulkarni wrote:> Dear Duncun, > ? ? ? ? ? ? ? ? ? ? ? ? ?What if there is no interactive "session" > running? I will be running my scripts automatically from crontab in Linux.I was talking about the session that is created for the duration of the BATCH run, not some other session that may be running in another process. Sorry for the confusion. Duncan Murdoch> > THanking you, > Yours sincerely, > AKSHAY M KULKARNI > > ------------------------------------------------------------------------ > *From:* Duncan Murdoch <murdoch.duncan at gmail.com> > *Sent:* Monday, March 20, 2023 12:20 AM > *To:* akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list > <r-help at r-project.org> > *Subject:* Re: [R] lexical scoping for scripts...... > On 19/03/2023 2:33 p.m., akshay kulkarni wrote: >> 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? > > The objects you create *will* appear in the workspace of the session > that's running.? They won't be saved to disk automatically so they'll > disappear at the end of the BATCH run.?? You can use various functions > (save(), save.image(), saveRDS(), writeLines(), etc.) to write them to > disk if you don't want them to disappear. > > Duncan Murdoch > > >> >> THanking you, >> yours sincerely, >> AKSHAY M KULKARNI >> ------------------------------------------------------------------------ >> *From:* Duncan Murdoch <murdoch.duncan at gmail.com> >> *Sent:* Saturday, March 18, 2023 11:49 PM >> *To:* akshay kulkarni <akshay_e4 at hotmail.com>; R help Mailing list >> <r-help at 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 >