Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Expecting a single string value: [type=closure; extent=1] #13

Closed
fab4ap opened this issue Mar 8, 2018 · 3 comments
Closed

Error: Expecting a single string value: [type=closure; extent=1] #13

fab4ap opened this issue Mar 8, 2018 · 3 comments

Comments

@fab4ap
Copy link

fab4ap commented Mar 8, 2018

Hello: Wanted to report this error. Hope the foll. is self-explanatory. Works with rasterize() but not in fasterize().

library(fasterize)
library(sf)
library(raster)
riv129mprdsf = st_read(dsn=".", layer="prdall_riv129_20k129")
Simple feature collection with 129084 features and 25 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: -4278174 ymin: 3802881 xmax: 3161826 ymax: 10742880
epsg (SRID): NA
proj4string: +proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs_

class(riv129mprdsf)
[1] "sf" "data.frame"

canus1kgrd
class : RasterLayer
dimensions : 6940, 7440, 51633600 (nrow, ncol, ncell)
resolution : 1000, 1000 (x, y)
extent : -4282964, 3157036, 3808483, 10748483 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0
data source : in memory
names : canus20kgrdsync
values : 0, 128710 (min, max)

act = fasterize(riv129mprdsf, canus1kgrd, field="riv129_20k", fun=mean, background=0)
Error in fasterize(riv129mprdsf, canus1kgrd, field = "riv129_20k", fun = mean) :
Expecting a single string value: [type=closure; extent=1].

@mdsumner
Copy link
Collaborator

mdsumner commented Mar 21, 2018

What the error is saying is that mean (the function, i.e. a closure) is not an allowed value for "fun" - it must be a

?fasterize
#character. The name of a function by which to combine overlapping polygons. 
# Currently takes "sum", "first", "last", "min", "max", "count", or "any".

You cannot apply a mean at all currently, but there's a workaround for using raster for this described in the help. Something like (untested ...)

x <- fasterize(riv129mprdsf, canus1kgrd, by = "riv129_20k", background=0)
act <- calc(x, mean)

Beware that if you have a lot of distinct values in that field you'll get a huge-number layered raster, which might be a pain so first try this, if it's more than "a few" you'll get a very large raster given your x/y dimensions here, so iterating over individual features and accumulating a sum with raster addition might be better than a big-calc in one go.

length(unique(riv129mprdsf$riv129_20k))

(Calling a function a closure shows a fun part of R's history as descendant from Lisp, and lexical scoping - it's explained here: http://adv-r.had.co.nz/Functional-programming.html#closures You also get a fun error like this from mean[1] )

@noamross
Copy link
Collaborator

I'm closing this as it is expected behavior, but opened #18, #19, and #20 in response.

@fab4ap
Copy link
Author

fab4ap commented Mar 23, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants