Rscript : Why is Error in UseMethod(“extract_”) : being indicated when attempting to use raster::extract?

China☆狼群 提交于 2019-12-07 09:46:33

问题


I attempting to use raster package's extract method to extract values from a Raster* object.

RStudioPrompt> jpnpe <- extract(jpnp, jpnb, fun = mean, na.rm = T)

where jpnp is the raster object and jpnb is SpatialPolygonsDataFrame

However the following error is indicated:

Error in UseMethod("extract_") : 
  no applicable method for 'extract_' applied to an object of class "c('RasterStack', 'Raster', 'RasterStackBrick', 'BasicRaster')"

How can I get passed this error?


回答1:


Issue may be due to having another package with the same method name, obfuscating the raster extract method.

The tidyr package has an extract method which may conflict with raster's extract method.

Confirm by checking libraries loaded by doing:

>search()




[1] ".GlobalEnv"           **"package:tidyr"**        "package:dplyr"       
 [4] "package:rgeos"        "package:ggplot2"      "package:RColorBrewer"
 [7] "package:animation"    "package:rgdal"        "package:maptools"    
[10] **"package:raster"**       "package:sp"           "tools:rstudio"       
[13] "package:stats"        "package:graphics"     "package:grDevices"   
[16] "package:utils"        "package:datasets"     "package:methods"     
[19] "Autoloads"            "package:base"    

you can also check which extract method is being loaded by typing name of function without brackets (as below, the environment will tell you which package is being used):

> extract

function (data, col, into, regex = "([[:alnum:]]+)", remove = TRUE, 
    convert = FALSE, ...) 
{
    col <- col_name(substitute(col))
    extract_(data, col, into, regex = regex, remove = remove, 
        convert = convert, ...)
}
<environment: namespace:tidyr>

To resolve the error just unload the offending package, in RStudio you can use the following command:

>.rs.unloadPackage("tidyr")

and re-execute the raster extract method:

>jpnpe <- extract(jpnp, jpnb, fun = mean, na.rm = T)


来源:https://stackoverflow.com/questions/36616254/rscript-why-is-error-in-usemethodextract-being-indicated-when-attemptin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!