wkth
wkth

Reputation: 127

Use constant as a function argument in `purrr::map()`

Consider the following reprex that I would like to check whether the points in each row are in a given bounding box.

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

validate_lonlat <- function(lon, lat, bbox) {
  if (is.na(lat) | is.na(lon)) {
    return(FALSE)
  }

  if (lon <= bbox[["xmin"]] | lon >= bbox[["xmax"]]) {
    return(FALSE)
  }

  if (lat <= bbox[["ymin"]] | lat >= bbox[["ymax"]]) {
    return(FALSE)
  }

  return(TRUE)
}

lonlat_test <- data.frame(
  lon = c(NA, NA, 100, 114, 114.3, 114.3),
  lat = c(NA, 20, 20, 22.3, 22.5, 22.7)
)

BOUNDING_BOX <- c(xmin = 113.824223, ymin = 22.138010, xmax = 114.553484, ymax = 22.571833)

lonlat_test %>%
  mutate(
    bbox = list(BOUNDING_BOX)
  ) %>%
  mutate(
    is_valid_lonlat = pmap_lgl(list(lon, lat, bbox), validate_lonlat)
  )
#>     lon  lat                                     bbox is_valid_lonlat
#> 1    NA   NA 113.82422, 22.13801, 114.55348, 22.57183           FALSE
#> 2    NA 20.0 113.82422, 22.13801, 114.55348, 22.57183           FALSE
#> 3 100.0 20.0 113.82422, 22.13801, 114.55348, 22.57183           FALSE
#> 4 114.0 22.3 113.82422, 22.13801, 114.55348, 22.57183            TRUE
#> 5 114.3 22.5 113.82422, 22.13801, 114.55348, 22.57183            TRUE
#> 6 114.3 22.7 113.82422, 22.13801, 114.55348, 22.57183           FALSE

Created on 2021-03-12 by the reprex package (v0.3.0)

The code works. However, I would like to know if it is possible to use constant as an argument in map()? When I use the constant BOUNDING_BOX directly in map(), I got an error.

latlon_test %>%
  mutate(
    is_valid_lonlat = pmap_lgl(list(lon, lat, BOUNDING_BOX), validate_lonlat)
  )
#> Error: Problem with `mutate()` input `is_valid_lonlat`.
#> x Element 3 of `.l` must have length 1 or 6, not 4
#> ℹ Input `is_valid_lonlat` is `pmap_lgl(list(lon, lat, BOUNDING_BOX), validate_lonlat)`.

Created on 2021-03-12 by the reprex package (v0.3.0)

Upvotes: 1

Views: 304

Answers (1)

akrun
akrun

Reputation: 887851

Wrap the BOUNDING_BOX in a list to do the recycling as 'BOUNDING_BOX' is a named vector of length 4, so the unit is each element of the 'BOUNDING_BOX'. Wrapping with list, shifts the units from the vector element to the list element which gets recycled

lonlat_test %>%
 mutate(
     is_valid_lonlat = pmap_lgl(list(lon, lat, list(BOUNDING_BOX)), 
         validate_lonlat)
 )

-output

#    lon  lat is_valid_lonlat
#1    NA   NA           FALSE
#2    NA 20.0           FALSE
#3 100.0 20.0           FALSE
#4 114.0 22.3            TRUE
#5 114.3 22.5            TRUE
#6 114.3 22.7           FALSE

Upvotes: 2

Related Questions