Re: [Rd] Loose code in R package files

From: Prof Brian Ripley <>
Date: Wed, 15 Jun 2011 15:22:26 +0100 (BST)

On Tue, 14 Jun 2011, Stephen Ellison wrote:

> I'm sure i've seen the answer to this, but can't find it:
> If there is executable code in an R package .R file that does not
> return a function (that is, something like x <- rnorm(5), outside

> any function body ), what will actually happens at build and load

> time?
> And (more importantly, so I can point a colleague to documented
> guidance on the matter) is there somewhere in R's docs that says why
> this is not likely to be a good idea and suggests the sort of things
> it is sensible - or not sensible - to include in .R files?

It is documented in 'Writing R Extensions':

The R subdirectory contains R code files, only. ...
It should be possible to read in the files using source(), so R objects must be created by assignments. Note that there need be no connection between the name of the file and the R objects created by it. Ideally, the R code files should only directly assign R objects and definitely should not call functions with side effects such as require and options. If computations are required to create objects these can use code `earlier' in the package (see the `Collate' field) plus, only if lazyloading is used, functions in the `Depends' packages provided that the objects created do not depend on those packages except via name space imports. (Packages without name spaces will work under somewhat less restrictive assumptions.)

So your example creates an object 'x' in the package or name space. Which is perfectly legal, but maybe not intentional. For example, R's base package does

## needs to run after paste()
.leap.seconds <- local({

     .leap.seconds <-
         c("1972-6-30", "1972-12-31", "1973-12-31", "1974-12-31",
           "1975-12-31", "1976-12-31", "1977-12-31", "1978-12-31",
           "1979-12-31", "1981-6-30", "1982-6-30", "1983-6-30",
           "1985-6-30", "1987-12-31", "1989-12-31", "1990-12-31",
           "1992-6-30", "1993-6-30", "1994-6-30","1995-12-31",
           "1997-6-30", "1998-12-31", "2005-12-31", "2008-12-31")
     .leap.seconds <- strptime(paste(.leap.seconds , "23:59:60"),
                               "%Y-%m-%d %H:%M:%S")
     c(as.POSIXct(.leap.seconds, "GMT")) # lose the timezone

S Ellison

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Wed 15 Jun 2011 - 14:26:37 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 16 Jun 2011 - 09:10:20 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive