问题
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")
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/39911076/r-error-in-as-doubley-cannot-coerce-type-s4-to-vector-of-type-double