fstevens
fstevens

Reputation: 1297

Namespace issue with raster package plot function?

I have experiencing a weird problem with the 'plot' function of the 'raster' package.

library(raster)
ras <- raster(ncol=10, nrow=10)

EDIT

values(ras) <- runif(ncell(ras))

END EDIT

plot(ras)

Erreur dans as.double(y) : 
cannot coerce type 'S4' to vector of type 'double'

For what I have read on the net, this error depends on the user, and probably depends on the loaded packages. In my case, the problem comes from the fact that r uses the standard 'plot' method from the 'graphics' package, when it should use the specific 'raster' method since 'ras' is a rasterLayer object. However, for a reason I do not understand, 'plot' is not imported in the 'raster' namespace, while all the other functions are.

> raster::plot
Erreur : 'plot' n'est pas un objet exporté depuis 'namespace:raster'

To be compared with :

raster::persp
standardGeneric for "persp" defined from package "graphics"
function (x, ...) 
standardGeneric("persp")
<environment: 0x0cd9eb80>
Methods may be defined for arguments: x
Use  showMethods("persp")  for currently available ones.

Since I do not completely understand how namespaces behave, I am looking for your help ! Is this kind of situation familiar for you, and do you have a way to solve it ? In the meantime, do you know a function to display the content of a namespace (so I could check the content of the raster namespace step by step) ?

PS: I am using R 2.15.2 with RStudio, many packages loaded but all are up to date.

sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=French_Belgium.1252  LC_CTYPE=French_Belgium.1252          LC_MONETARY=French_Belgium.1252 LC_NUMERIC=C                   
[5] LC_TIME=French_Belgium.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] raster_2.0-41 sp_1.0-5     

loaded via a namespace (and not attached):
 [1] grid_2.15.0      hexbin_1.26.0    intervals_0.13.3 lattice_0.20-6   rgdal_0.8-4      spacetime_1.0-3  spam_0.29-2      tools_2.15.0    
 [9] xts_0.9-2        zoo_1.7-9             

Thanks you,

François

Upvotes: 3

Views: 2780

Answers (5)

Claudia
Claudia

Reputation: 1016

I had the same problem and re-installing the raster package fixed it.

install.packages("raster")

Upvotes: 1

mitmat
mitmat

Reputation: 1

I've been running in the same error, also using RStudio.

My issues was that I loaded the raster package via library(raster) in the .Rprofile file of my project. But code in Rprofile gets loaded before anything else, so the graphics package (containing the plot generic) is loaded after raster, causing the problems.

Solution: Put library(graphics) before library(raster) in Rprofile, and it worked for me.

Upvotes: 0

Adam Erickson
Adam Erickson

Reputation: 6363

For me, what resolved this S4 class namespace issue was to add the raster package as a Dependency. Hence, using the attach() function should also work, as that is what dependencies do. I know that is not an ideal solution, but hey, it's a statistics language ;)

Upvotes: 0

Robert Hijmans
Robert Hijmans

Reputation: 47081

This sometimes happens when you have a stale session (typically caused by loading an old session at startup), that goes away if you start a fresh R session (without loading previously saved sessions).

Upvotes: 1

agstudy
agstudy

Reputation: 121568

Using this you get all the list of object of package raster

basevals <- ls(pos="package:raster") 

for example

   which(basevals == 'persp')  ## function persp shows up because it is the exported generic.
   141
   which(basevals == 'plot')   ## no function plot
   integer(0)

No when I do this , it works for me:

library(raster)
r <- raster(ncol=10, nrow=10)
values(r) <- runif(ncell(r))
plot(r, main='Raster with 100 cells')

So There is certainly a plot method here. It is not in the previous list "basevals" beacuse it is an S4 method.

To get the plot method of raster package , try this :

 getMethod('plot',signature=signature(x='Raster', y='ANY'))

or more efficiently using

findMethods("plot", "package:raster"). 

Upvotes: 2

Related Questions