Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 630
KK. Avatar asked Apr 14 '16 07:04

KK.


1 Answers

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)
like image 62
KK. Avatar answered Oct 05 '22 12:10

KK.