[Rd] automatically loading packages

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue May 4 16:30:06 CEST 2010


If I start an R session with a previously saved .RData that has S4
objects in it, there are times when the associated package loads when
I use (or mention) the object, and times when it doesn't. For example:

 > dim(casesIn)
 NULL
 > library(sp)
 > dim(casesIn)
 [1] 726  22

 - because casesIn is a SpatialPointsDataFrame. But if I try and plot
it (starting from scratch again), it loads the sp package:

 > plot(casesIn)
 Loading required package: sp

 - and then falls over:

 Error in as.double(y) :
   cannot coerce type 'S4' to vector of type 'double'

 - which is the error you get from plot.default(casesIn).

 Trying plot(casesIn) again then works, producing a point map. Why
doesn't it work the first time?

 dim(casesIn) never triggers a library(sp) call:

 > dim(casesIn)
 NULL

 - so do it manually:

 > library(sp)
 > dim(casesIn)
 [1] 726  22

 - I guess it must depend on whether its a generic of S3 or S4 flavour
and/or the current phase of the moon.

 I can't find any information about this behaviour, and I can't think
of a good query to search for it either. I keep hitting the 'autoload'
documentation, which seems to be something completely different.

R 2.10.1 on Linux - can't see anything relevant in the latest NEWS file.

Barry



More information about the R-devel mailing list