Re: [Rd] Conventions: Use of globals and main functions

2019-08-28 Thread Cyclic Group Z_1 via R-devel
I meant that using a script both as a script and a library (sourcing into other files to serve as a package) is bad practice. I don't think having any functions in a script is necessarily bad practice. Best, CG __ R-devel@r-project.org mailing list ht

Re: [Rd] Conventions: Use of globals and main functions

2019-08-28 Thread Peter Meissner
The point is, that there are several possible problems. But. One the one hand they are not really problematic in my opinion (I do not care if my function has potential access to objects outside of its environment because this access is read-only at worst and it's not common practice to use this p

Re: [Rd] Conventions: Use of globals and main functions

2019-08-28 Thread Cyclic Group Z_1 via R-devel
I appreciate the well-thought-out comments. To your first point, I am not sure what "glattering" means precisely (a Google search revealed nothing useful), but I assume it means something to the effect of overfilling the main namespace with too many names. Per Norm Matloff's counterpoint in The

Re: [Rd] Conventions: Use of globals and main functions

2019-08-28 Thread Peter Meissner
Firtst, I think that thinking about best practice advise and beeing able to accomandate different usage scenarios is a good thing despite me arguing against introducing the main()-idiom. Let's have another turn on the global-environment is bad argument. It has two parts: (1) Glattering namespace

Re: [Rd] Conventions: Use of globals and main functions

2019-08-27 Thread Cyclic Group Z_1 via R-devel
> That beeing said I think the main task of scripts is to get things done via >running them end to end in a fresh session. Now, it very well may happen that >a lot of stuff has to be done. Than splitting up scripts into subscripts and >sourcing them from a meta script is a straightforward soluti

Re: [Rd] Conventions: Use of globals and main functions

2019-08-27 Thread Cyclic Group Z_1 via R-devel
Definitely, I agree that global variables have a place in programming. They play an especially important role in low-level software, such as embedded programming, as you mentioned, and systems programming. I generally would disagree with anyone that says global variables should never be used, an

Re: [Rd] Conventions: Use of globals and main functions

2019-08-27 Thread Henrik Bengtsson
FWIW, one could imagine introducing a helper function global(); global <- function(expr) { eval(substitute(expr), envir = globalenv(), enclos = baseenv()) } to make it explicit that any assignments (and evaluation in general) take place in the global environment, e.g. > local({ global(a <- 2) })

Re: [Rd] Conventions: Use of globals and main functions

2019-08-27 Thread Abby Spurdle
> "Running R" (in "R Installation and Administration") links to > "Appendix B Invoking R" (in "An Introduction to R"). > However, these sections do not cover the topics in this thread. Sorry, I made a mistake. It is in the documentation (B.4 Scripting with R) e.g. (excerpts only) R CMD BATCH "--a

Re: [Rd] Conventions: Use of globals and main functions

2019-08-27 Thread Abby Spurdle
> this appears to disagree with the software-engineering principle of avoiding > a mutating global state I disagree. In embedded systems engineering, for example, it's customary to use global variables to represent ports. Also, I note that the use of global variables, is similar to using pen and

Re: [Rd] Conventions: Use of globals and main functions

2019-08-27 Thread Peter Meissner
Hey, I always found it a strength of R compared to many other langaugas that simple things (running a script, doing something interactive, writing a function, using lambdas, installing packages, getting help, ...) are very very simple. R is a commandline statistics program that happens to be a ve

Re: [Rd] Conventions: Use of globals and main functions

2019-08-27 Thread Martin Maechler
> Duncan Murdoch > on Mon, 26 Aug 2019 14:19:36 -0400 writes: > On 26/08/2019 1:58 p.m., William Dunlap wrote: >> Duncan Murdoch wrote: >> > Scripts are for throwaways, not for anything worth keeping. >> >> I totally agree and have a tangentially relevant question

Re: [Rd] Conventions: Use of globals and main functions

2019-08-26 Thread Duncan Murdoch
On 26/08/2019 1:58 p.m., William Dunlap wrote: Duncan Murdoch wrote: > Scripts are for throwaways, not for anything worth keeping. I totally agree and have a tangentially relevant question about the <<- operator.  Currently 'name <<- value' means to look up the environment stack until you fin

Re: [Rd] Conventions: Use of globals and main functions

2019-08-26 Thread William Dunlap via R-devel
Duncan Murdoch wrote: > Scripts are for throwaways, not for anything worth keeping. I totally agree and have a tangentially relevant question about the <<- operator. Currently 'name <<- value' means to look up the environment stack until you find 'name' and (a) if you find 'name' in some frame

Re: [Rd] Conventions: Use of globals and main functions

2019-08-26 Thread Cyclic Group Z_1 via R-devel
Right, I did not mean to imply these tests are equivalent. Only that both similarly exclude execution of main() under some context.  Best, CG __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel

Re: [Rd] Conventions: Use of globals and main functions

2019-08-26 Thread Gábor Csárdi
That is unfortunately wrong, though. Whether the script runs as "main" and whether R is in interactive mode are independent properties. I guess most of the time it works, because _usually_ you run the whole script (main()) in non-interactive mode, and source() it in interactive mode, but this is no

Re: [Rd] Conventions: Use of globals and main functions

2019-08-25 Thread Duncan Murdoch
On 25/08/2019 7:09 p.m., Cyclic Group Z_1 wrote: This is a fair point; structuring functions into packages is probably ultimately the gold standard for code organization in R. However, lexical scoping in R is really not much different than in other languages, such as Python, in which use of

Re: [Rd] Conventions: Use of globals and main functions

2019-08-25 Thread Cyclic Group Z_1 via R-devel
This is a fair point; structuring functions into packages is probably ultimately the gold standard for code organization in R. However, lexical scoping in R is really not much different than in other languages, such as Python, in which use of main functions and defining other named functions

Re: [Rd] Conventions: Use of globals and main functions

2019-08-25 Thread Cyclic Group Z_1 via R-devel
This seems like a nice idiom; I've seen others use    if(!interactive()){        main()    }to a similar effect. Best,CG On Sunday, August 25, 2019, 01:16:06 AM CDT, Gábor Csárdi wrote: This is what I usually put in scripts: if (is.null(sys.calls())) {   main() } This is mostly equiv

Re: [Rd] Conventions: Use of globals and main functions

2019-08-25 Thread Duncan Murdoch
On 25/08/2019 12:08 a.m., Cyclic Group Z_1 via R-devel wrote: In R scripts (as opposed to packages), even in reproducible scripts, it seems fairly conventional to use the global workspace as a sort of main function, and thus R scripts often populate the global environment with many variables, w

Re: [Rd] Conventions: Use of globals and main functions

2019-08-24 Thread Gábor Csárdi
This is what I usually put in scripts: if (is.null(sys.calls())) { main() } This is mostly equivalent to the Python idiom. It the script runs from Rscript, then it will run main(). It also lets you source() the script, and debug its functions, test them, etc. It works best if all the code in th