[Rd] Conventions: Use of globals and main functions

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Aug 26 02:08:59 CEST 2019


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 main functions and defining other named functions outside of main are encouraged. For example, in Scheme, from which R derives its scoping rules, the community generally organizes code with almost exclusively functions and few non-function global variables at top level. The common use of globals in R seems to be mostly a consequence of historical interactive use and, relatedly, an inherited practice from S.
> 
> It is true, though, that since anonymous functions (such as in lapply) play a large part in idiomatic R code, as you put it, "[l]exical scoping means that all of the problems of global variables are available to writers who use main()." Nevertheless, using a main function with other functions defined outside it seems like a good quick alternative that offers similar advantages to making a package when functions are tightly coupled to the script and the project may not be large or generalizable enough to warrant making a package.
> 

I think the idea that making a package is too hard is just wrong. 
Packages in R have lots of requirements, but nowadays there are tools 
that make them easy.  Eleven years ago at UseR in Dortmund I wrote a 
package during a 45 minute presentation, and things are much easier now.

If you make a complex project without putting most of the code into a 
package, you don't have something that you will be able to modify in a 
year or two, because you won't have proper documentation.

Scripts are for throwaways, not for anything worth keeping.

Duncan Murdoch



More information about the R-devel mailing list