3

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

fstevens
  • 1,287
  • 1
  • 17
  • 28
  • I get a different error after the first three lines `Error in .plotraster2(x, col = col, maxpixels = maxpixels, add = add, : no values associated with this RasterLayer`. Use the example from the help and see if you can replicate your error. – mnel Jan 21 '13 at 11:40
  • 2
    I get the same error as mnel...PS : you can use [this](http://stackoverflow.com/questions/13575180/how-to-change-the-language-of-errors-in-r) to have error message in English.. – agstudy Jan 21 '13 at 11:43
  • Yes, I forgot to fill the raster with numbers, sorry. Now it should work (or fail the same way it does for me). – fstevens Jan 21 '13 at 12:41

5 Answers5

2

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"). 
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • +1. I'd forgotten this was an S4 issue. There is certainly a `plot` method there, just not an S3 one hence your first part of the answer shows no plot method whilst `persp` shows up because it is the exported generic. The last bit can be done a bit more efficiently using `findMethods("plot", "package:raster")`. – Gavin Simpson Jan 21 '13 at 12:26
  • Thanks for the answer. I understand the fact that I had not found the raster plot method does not mean it does not exist. But 'getMethod' does not get it either. getMethod('plot',signature=signature(x='Raster', y='ANY')) Error in getMethod("plot", signature = signature(x = "Raster", y = "ANY")) : No method found for function "plot" and signature Raster – fstevens Jan 21 '13 at 12:53
  • @fstevens can you try this in you R session please...and if not give me your sessionInfo()? – agstudy Jan 21 '13 at 12:55
  • @fstevens oops ..maybe it is better to do it new R session... where you have fewer packages.. – agstudy Jan 21 '13 at 13:06
  • @agstudy I have done it with only basic packages + raster. The simple example still fails. – fstevens Jan 21 '13 at 13:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23092/discussion-between-agstudy-and-fstevens) – agstudy Jan 21 '13 at 13:23
1

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).

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • I've been having the same problem. I'm not sure how you load a new session without it automatically loading previous settings so I deleted all my .R* files from the working dir and it seems to be functioning again. – Dominik Aug 07 '13 at 16:52
1

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

install.packages("raster")
Claudia
  • 996
  • 1
  • 10
  • 27
0

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 ;)

Adam Erickson
  • 6,027
  • 2
  • 46
  • 33
0

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.

mitmat
  • 1
  • 1