exact_extract 抛出错误 "Error in .num_expected_args(fun) : 'list' object cannot be coerced to type 'double'"

exact_extract throws error "Error in .num_expected_args(fun) : 'list' object cannot be coerced to type 'double'"

我正在尝试使用 exactextractr:: 程序包的 exact_extract() 函数来给出被 classified 的每个 class 覆盖的多边形面积百分比栅格。但是,我收到一条神秘的错误消息“.num_expected_args(fun) 中的错误:无法强制 'list' 对象键入 'double'。”错误似乎是说所有的覆盖率分数都存储在一个列表中,并且汇总例程试图将其转换为双精度数据类型。我唯一能想到的是,也许这是 exact_extract() 函数中的错误。任何指导将不胜感激。下面是我可重现的例子来说明这个问题:

##Loading Necessary Packages##
library(exactextractr)
library(sf)
library(spData)
library(raster)
library(tmap)

## Getting the State of Oregon from the us_states and projecting it into Oregon Statewide Lambert (EPSG 2992)  
data("us_states")
OR<-st_transform(us_states[us_states$NAME=="Oregon",], st_crs("EPSG:2992"))

##Making a fake classified raster with the same extent and projection as the state of Oregon
FAKE<-raster(ext=extent(OR), res=1000, crs=crs(OR))
values(FAKE)<-sample(c(1:4), ncell(FAKE),replace=TRUE)

#Map to demonstrate that polygon overlays the raster
tmap_mode("plot")
tm_shape(FAKE)+
  tm_raster()+
tm_shape(OR)+
  tm_polygons(border.col="black", alpha=0)

test<-exact_extract(FAKE, OR, fun=count, summarize_df=TRUE) # This will throw an error

原来我只是个白痴。 exact_extract() 期望函数被引用,因为它们是 运行 内部的而不是从外部函数中获取的。这完全解决了问题:

##Loading Necessary Packages##
library(exactextractr)
library(sf)
library(spData)
library(raster)
library(tmap)

## Getting the State of Oregon from the us_states and projecting it into Oregon Statewide Lambert (EPSG 2992)  
data("us_states")
OR<-st_transform(us_states[us_states$NAME=="Oregon",], st_crs("EPSG:2992"))

##Making a fake classified raster with the same extent and projection as the state of Oregon
FAKE<-raster(ext=extent(OR), res=1000, crs=crs(OR))
values(FAKE)<-sample(c(1:4), ncell(FAKE),replace=TRUE)

#Map to demonstrate that polygon overlays the raster
tmap_mode("plot")
tm_shape(FAKE)+
  tm_raster()+
tm_shape(OR)+
  tm_polygons(border.col="black", alpha=0)

test<-exact_extract(FAKE, OR, fun="count", summarize_df=TRUE)