Plotting depends on search() order?
On Mon, 6 Apr 2009, Don MacQueen wrote:
I am finding that plotting a SpatialPolygonsDataFrame object fails when the base graphics packages are ahead of sp in the search() path. This occurs when I put require(maptools) [ or require(rgdal), or require(sp) ] in my .Rprofile file.
I never use an .Rprofile file for loading packages, because I forget that it/they is/are there. Mine sets up an alternative terminal handling of help pages, running R code from the base package. Could you try manually ordering the search path by requiring() the base packages graphics:datasets before maptools in your .Rprofile? Reading ?.Rprofile does suggest that one needs to tread carefully here. Roger
In the examples below, the object 'srdf' was created exactly as in the
example in help('overlay').
An entire transcript of both cases is included at the end, but here are the
key excerpts:
-- Case 1 (problem):
search()
[1] ".GlobalEnv" "package:graphics" "package:grDevices" [4] "package:utils" "package:datasets" "package:maptools" [7] "package:sp" "package:foreign" "package:stats" [10] "package:methods" "Autoloads" "package:base"
plot(srdf)
Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double' -- Case 2 (ok):
search()
[1] ".GlobalEnv" "package:maptools" "package:sp" [4] "package:foreign" "package:stats" "package:graphics" [7] "package:grDevices" "package:utils" "package:datasets" [10] "package:methods" "Autoloads" "package:base"
plot(srdf)
Do I understand correctly that the reason is that there is 'plot' function in the base graphics package, and another in the sp package, and the latter is required for plotting spatial objects, so it has to come first in the search order? Or have I missed something? This is easy enough to deal with, but it would be convenient to be able to put those "requires" in .Rprofile. Thanks -Don ------------------------------------------------------------- maptools.load.order[119]% cat .Rprofile require(maptools) maptools.load.order[120]% maptools.load.order[120]% R R version 2.8.1 (2008-12-22) Copyright (C) 2008 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. Loading required package: maptools Loading required package: foreign Loading required package: stats Loading required package: sp [Previously saved workspace restored]
class(srdf)
[1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp"
search()
[1] ".GlobalEnv" "package:graphics" "package:grDevices" [4] "package:utils" "package:datasets" "package:maptools" [7] "package:sp" "package:foreign" "package:stats" [10] "package:methods" "Autoloads" "package:base"
plot(srdf)
Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'
q()
Save workspace image? [y/n/c]: y maptools.load.order[121]% -------------------------------------------------------------- maptools.load.order[123]% cat .Rprofile ## require(maptools) maptools.load.order[124]% maptools.load.order[124]% R R version 2.8.1 (2008-12-22) Copyright (C) 2008 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored]
require(maptools)
Loading required package: maptools Loading required package: foreign Loading required package: sp
class(srdf)
[1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp"
search()
[1] ".GlobalEnv" "package:maptools" "package:sp" [4] "package:foreign" "package:stats" "package:graphics" [7] "package:grDevices" "package:utils" "package:datasets" [10] "package:methods" "Autoloads" "package:base"
plot(srdf) sessionInfo()
R version 2.8.1 (2008-12-22) i386-apple-darwin8.11.1 locale: C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] maptools_0.7-21 sp_0.9-34 foreign_0.8-34 loaded via a namespace (and not attached): [1] grid_2.8.1 lattice_0.17-20 Warning message: 'DESCRIPTION' file has 'Encoding' field and re-encoding is not possible
q()
Save workspace image? [y/n/c]: y maptools.load.order[125]%
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no