Devin
Devin

Reputation: 21

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

Im working in Studio with a raster .tif image. I have watched a tutorial on plotting the raster with the code below, however it does not work for me. I get the error:

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

I have loaded the necessary packages(raster and rgdal) I have also tried loading the arulesViz, yet get the same error

YIELD <- raster("//Users//DevinOsborne//Desktop//Thesis//QGIS projects //Project//Rasters//Images//Yield_wheat.tif")

plot(YIELD,main= "Yield map")

Upvotes: 2

Views: 7326

Answers (2)

Mxblsdl
Mxblsdl

Reputation: 514

To expand on @Chelmy88 s answer, try sp::plot() You will first need the sp package; install.packages("sp")

I was able to recreate your error with graphics::plot()

I believe that when you load raster library sp will also be loaded.

Upvotes: 6

Chelmy88
Chelmy88

Reputation: 1116

I had similar issue while using rgdal in a custome R package. This error as been solved for me by loading the sp library. The problem was not arising when running the script alone, but once included in the package I had the same error when using plot():

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

This has been solved by adding import("sp") in the NAMESPACE file and "sp" to the Import list in the DESCRIPTION file.

Unfortunately I could not find which sp function is required, so I use a full import instead of a more targeted importFrom() in the NAMESPACE file.

Upvotes: 2

Related Questions