Branch over sf objects #713
Replies: 3 comments 13 replies
-
|
That's odd, your first pipeline works for me and produces reasonable-looking PNG maps of states. It looks like it may come from |
Beta Was this translation helpful? Give feedback.
-
|
The example provided by the OP works for me too, but I have a different pipeline where I run into the same error:
The strange thing is that the error does not occur the first time I could not even reproduce the error in debug mode. Using |
Beta Was this translation helpful? Give feedback.
-
|
The following reprex shows two issues with branching (iteration = "vector") over objects of class "sf" - "data.frame":
library(targets)
#> Warning: package 'targets' was built under R version 4.1.3
tar_script({
tar_option_set(packages = c("dplyr", "sf"))
example_sf_file <- function() {
system.file("shape/nc.shp", package = "sf")
}
read_shape <- function(shapefile) {
nc <- st_read(shapefile) %>% head()
nc
}
list(tarchetypes::tar_file(nc_file, example_sf_file()), tar_target(nc,
read_shape(shapefile = nc_file), format = "qs"), tar_target(by_row,
nc, pattern = map(nc), format = "qs"))
})
tar_make()
#> * start target nc_file
#> * built target nc_file
#> * start target nc
#> Reading layer `nc' from data source `C:\R\library\sf\shape\nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS: NAD27
#> * built target nc
#> * start branch by_row_c8040484
#> * built branch by_row_c8040484
#> * start branch by_row_bd44f74a
#> * built branch by_row_bd44f74a
#> * start branch by_row_58077b70
#> * built branch by_row_58077b70
#> * start branch by_row_9ff1fc0f
#> * built branch by_row_9ff1fc0f
#> * start branch by_row_c28ade2e
#> * built branch by_row_c28ade2e
#> * start branch by_row_bfe79c7e
#> * built branch by_row_bfe79c7e
#> * built pattern by_row
#> * end pipeline: 0.55 seconds
#> Warning messages:
#> 1: package 'targets' was built under R version 4.1.3
#> 2: package 'dplyr' was built under R version 4.1.3
#> 3: package 'sf' was built under R version 4.1.3
#> 4: 1 targets produced warnings. Run tar_meta(fields = warnings) for the messages.
tar_read(nc)
#> AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74
#> 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1
#> 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0
#> 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5
#> 4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1
#> 5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9
#> 6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7
#> NWBIR74 BIR79 SID79 NWBIR79
#> 1 10 1364 0 19
#> 2 10 542 3 12
#> 3 208 3616 6 260
#> 4 123 830 2 145
#> 5 1066 1606 3 1197
#> 6 954 1838 5 1237
#> geometry
#> 1 -81.47276, -81.54084, -81.56198, -81.63306, -81.74107, -81.69828, -81.70280, -81.67000, -81.34530, -81.34754, -81.32478, -81.31332, -81.26624, -81.26284, -81.24069, -81.23989, -81.26424, -81.32899, -81.36137, -81.36569, -81.35413, -81.36745, -81.40639, -81.41233, -81.43104, -81.45289, -81.47276, 36.23436, 36.27251, 36.27359, 36.34069, 36.39178, 36.47178, 36.51934, 36.58965, 36.57286, 36.53791, 36.51368, 36.48070, 36.43721, 36.40504, 36.37942, 36.36536, 36.35241, 36.36350, 36.35316, 36.33905, 36.29972, 36.27870, 36.28505, 36.26729, 36.26072, 36.23959, 36.23436
#> 2 -81.23989, -81.24069, -81.26284, -81.26624, -81.31332, -81.32478, -81.34754, -81.34530, -80.90344, -80.93355, -80.96577, -80.94967, -80.95639, -80.97795, -80.98284, -81.00278, -81.02464, -81.04280, -81.08425, -81.09856, -81.11331, -81.12938, -81.13840, -81.15337, -81.17667, -81.23989, 36.36536, 36.37942, 36.40504, 36.43721, 36.48070, 36.51368, 36.53791, 36.57286, 36.56521, 36.49831, 36.46722, 36.41473, 36.40380, 36.39138, 36.37183, 36.36668, 36.37783, 36.41034, 36.42992, 36.43115, 36.42285, 36.42633, 36.41763, 36.42474, 36.41544, 36.36536
#> 3 -80.45634, -80.47639, -80.53688, -80.54501, -80.55415, -80.59060, -80.62431, -80.66744, -80.69666, -80.72404, -80.73437, -80.75256, -80.76630, -80.78270, -80.87438, -80.87086, -80.88892, -80.92457, -80.95639, -80.94967, -80.96577, -80.93355, -80.90344, -80.83816, -80.61105, -80.43531, -80.45301, -80.45634, 36.24256, 36.25473, 36.25674, 36.27666, 36.27843, 36.26828, 36.27310, 36.24610, 36.25909, 36.25847, 36.26476, 36.25830, 36.26184, 36.24858, 36.23388, 36.32462, 36.35443, 36.37275, 36.40380, 36.41473, 36.46722, 36.49831, 36.56521, 36.56344, 36.55730, 36.55104, 36.25709, 36.24256
#> 4 -76.00897, -76.01735, -76.03288, -76.04395, -76.09509, -76.16093, -76.15815, -76.16829, -76.33025, -76.12740, -76.04596, -76.03321, -76.09106, -75.97607, -75.96976, -76.00161, -75.95126, -75.92812, -75.92459, -75.80006, -75.79885, -75.85516, -75.91376, -75.95751, -75.94193, -76.00897, 36.31960, 36.33773, 36.33598, 36.35359, 36.34892, 36.39190, 36.41269, 36.42709, 36.55606, 36.55716, 36.55695, 36.51437, 36.50357, 36.43621, 36.41512, 36.41891, 36.36547, 36.42324, 36.35095, 36.11282, 36.07282, 36.10567, 36.24480, 36.25945, 36.29434, 36.31960, -76.02717, -75.99866, -75.91192, -75.92480, -75.97728, -75.97629, -76.02717, 36.55672, 36.55665, 36.54253, 36.47398, 36.47802, 36.51793, 36.55672, -75.90199, -75.87817, -75.77316, -75.78317, -75.90199, 36.55620, 36.55587, 36.22926, 36.22519, 36.55620
#> 5 -77.21767, -77.23461, -77.29861, -77.29351, -77.30948, -77.33499, -77.38903, -77.37862, -77.41347, -77.41885, -77.45412, -77.53808, -77.55743, -77.57195, -77.58008, -77.55968, -77.56042, -77.63597, -77.65099, -77.69888, -77.74940, -77.90121, -77.89886, -77.76393, -77.32005, -77.17735, -77.15442, -77.09213, -77.07531, -77.08327, -77.12733, -77.13932, -77.14196, -77.21767, 36.24098, 36.21460, 36.21153, 36.16286, 36.16277, 36.18285, 36.20310, 36.24009, 36.25582, 36.28180, 36.31976, 36.30246, 36.30421, 36.31450, 36.32827, 36.37595, 36.40636, 36.44054, 36.47259, 36.47896, 36.47357, 36.50989, 36.55294, 36.55344, 36.55392, 36.55629, 36.52625, 36.50752, 36.48352, 36.47010, 36.47071, 36.45648, 36.41706, 36.24098
#> 6 -76.74506, -76.98069, -76.99475, -77.13007, -77.21767, -77.14196, -77.13932, -77.12733, -77.08327, -77.07531, -77.09213, -77.15442, -77.17735, -76.92413, -76.90842, -76.94577, -76.95367, -76.94351, -76.92408, -76.74135, -76.70750, -76.74506, 36.23392, 36.23024, 36.23558, 36.23346, 36.24098, 36.41706, 36.45648, 36.47071, 36.47010, 36.48352, 36.50752, 36.52625, 36.55629, 36.55415, 36.50428, 36.45896, 36.41923, 36.40173, 36.39244, 36.31517, 36.26613, 36.23392
class(tar_read(nc))
#> [1] "sf" "data.frame"
tar_read(by_row)
#> Error in `stop_vctrs()`:
#> ! Input must be a vector, not a <sfc_MULTIPOLYGON/sfc> object.
tar_script({
tar_option_set(packages = c("dplyr", "sf"))
example_sf_file <- function() {
system.file("shape/nc.shp", package = "sf")
}
read_shape <- function(shapefile) {
nc <- st_read(shapefile) %>% head()
nc
}
list(tarchetypes::tar_file(nc_file, example_sf_file()), tar_target(nc,
read_shape(shapefile = nc_file), format = "qs"), tar_target(by_row,
nc %>% select(AREA), pattern = map(nc), format = "qs"))
})
tar_make()
#> v skip target nc_file
#> v skip target nc
#> * start branch by_row_c8040484
#> x error branch by_row_c8040484
#> * end pipeline: 0.5 seconds
#> Error in tar_throw_run(target$metrics$error) :
#> could not load dependencies of target by_row_c8040484. Input must be a vector, not a <sfc_MULTIPOLYGON/sfc> object.
#> In addition: Warning messages:
#> 1: package 'targets' was built under R version 4.1.3
#> 2: package 'dplyr' was built under R version 4.1.3
#> 3: package 'sf' was built under R version 4.1.3
#> 4: 1 targets produced warnings. Run tar_meta(fields = warnings) for the messages.
#> Error in `tar_throw_run()`:
#> ! callr subprocess failed: could not load dependencies of target by_row_c8040484. Input must be a vector, not a <sfc_MULTIPOLYGON/sfc> object.
#> Visit https://books.ropensci.org/targets/debugging.html for debugging advice.
tar_script({
library(dplyr)
library(sf)
example_sf_file <- function() {
system.file("shape/nc.shp", package = "sf")
}
read_shape <- function(shapefile) {
nc <- st_read(shapefile) %>% head()
nc
}
list(tarchetypes::tar_file(nc_file, example_sf_file()), tar_target(nc,
read_shape(shapefile = nc_file), format = "qs"), tar_target(by_row,
nc %>% select(AREA), pattern = map(nc), format = "qs"))
})
tar_make()
#>
#> 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
#>
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
#> v skip target nc_file
#> v skip target nc
#> * start branch by_row_c8040484
#> * built branch by_row_c8040484
#> * start branch by_row_bd44f74a
#> * built branch by_row_bd44f74a
#> * start branch by_row_58077b70
#> * built branch by_row_58077b70
#> * start branch by_row_9ff1fc0f
#> * built branch by_row_9ff1fc0f
#> * start branch by_row_c28ade2e
#> * built branch by_row_c28ade2e
#> * start branch by_row_bfe79c7e
#> * built branch by_row_bfe79c7e
#> * built pattern by_row
#> * end pipeline: 0.2 seconds
#> Warning messages:
#> 1: package 'targets' was built under R version 4.1.3
#> 2: package 'dplyr' was built under R version 4.1.3
#> 3: package 'sf' was built under R version 4.1.3
tar_read(nc)
#> AREA PERIMETER CNTY_ CNTY_ID NAME FIPS FIPSNO CRESS_ID BIR74 SID74
#> 1 0.114 1.442 1825 1825 Ashe 37009 37009 5 1091 1
#> 2 0.061 1.231 1827 1827 Alleghany 37005 37005 3 487 0
#> 3 0.143 1.630 1828 1828 Surry 37171 37171 86 3188 5
#> 4 0.070 2.968 1831 1831 Currituck 37053 37053 27 508 1
#> 5 0.153 2.206 1832 1832 Northampton 37131 37131 66 1421 9
#> 6 0.097 1.670 1833 1833 Hertford 37091 37091 46 1452 7
#> NWBIR74 BIR79 SID79 NWBIR79
#> 1 10 1364 0 19
#> 2 10 542 3 12
#> 3 208 3616 6 260
#> 4 123 830 2 145
#> 5 1066 1606 3 1197
#> 6 954 1838 5 1237
#> geometry
#> 1 -81.47276, -81.54084, -81.56198, -81.63306, -81.74107, -81.69828, -81.70280, -81.67000, -81.34530, -81.34754, -81.32478, -81.31332, -81.26624, -81.26284, -81.24069, -81.23989, -81.26424, -81.32899, -81.36137, -81.36569, -81.35413, -81.36745, -81.40639, -81.41233, -81.43104, -81.45289, -81.47276, 36.23436, 36.27251, 36.27359, 36.34069, 36.39178, 36.47178, 36.51934, 36.58965, 36.57286, 36.53791, 36.51368, 36.48070, 36.43721, 36.40504, 36.37942, 36.36536, 36.35241, 36.36350, 36.35316, 36.33905, 36.29972, 36.27870, 36.28505, 36.26729, 36.26072, 36.23959, 36.23436
#> 2 -81.23989, -81.24069, -81.26284, -81.26624, -81.31332, -81.32478, -81.34754, -81.34530, -80.90344, -80.93355, -80.96577, -80.94967, -80.95639, -80.97795, -80.98284, -81.00278, -81.02464, -81.04280, -81.08425, -81.09856, -81.11331, -81.12938, -81.13840, -81.15337, -81.17667, -81.23989, 36.36536, 36.37942, 36.40504, 36.43721, 36.48070, 36.51368, 36.53791, 36.57286, 36.56521, 36.49831, 36.46722, 36.41473, 36.40380, 36.39138, 36.37183, 36.36668, 36.37783, 36.41034, 36.42992, 36.43115, 36.42285, 36.42633, 36.41763, 36.42474, 36.41544, 36.36536
#> 3 -80.45634, -80.47639, -80.53688, -80.54501, -80.55415, -80.59060, -80.62431, -80.66744, -80.69666, -80.72404, -80.73437, -80.75256, -80.76630, -80.78270, -80.87438, -80.87086, -80.88892, -80.92457, -80.95639, -80.94967, -80.96577, -80.93355, -80.90344, -80.83816, -80.61105, -80.43531, -80.45301, -80.45634, 36.24256, 36.25473, 36.25674, 36.27666, 36.27843, 36.26828, 36.27310, 36.24610, 36.25909, 36.25847, 36.26476, 36.25830, 36.26184, 36.24858, 36.23388, 36.32462, 36.35443, 36.37275, 36.40380, 36.41473, 36.46722, 36.49831, 36.56521, 36.56344, 36.55730, 36.55104, 36.25709, 36.24256
#> 4 -76.00897, -76.01735, -76.03288, -76.04395, -76.09509, -76.16093, -76.15815, -76.16829, -76.33025, -76.12740, -76.04596, -76.03321, -76.09106, -75.97607, -75.96976, -76.00161, -75.95126, -75.92812, -75.92459, -75.80006, -75.79885, -75.85516, -75.91376, -75.95751, -75.94193, -76.00897, 36.31960, 36.33773, 36.33598, 36.35359, 36.34892, 36.39190, 36.41269, 36.42709, 36.55606, 36.55716, 36.55695, 36.51437, 36.50357, 36.43621, 36.41512, 36.41891, 36.36547, 36.42324, 36.35095, 36.11282, 36.07282, 36.10567, 36.24480, 36.25945, 36.29434, 36.31960, -76.02717, -75.99866, -75.91192, -75.92480, -75.97728, -75.97629, -76.02717, 36.55672, 36.55665, 36.54253, 36.47398, 36.47802, 36.51793, 36.55672, -75.90199, -75.87817, -75.77316, -75.78317, -75.90199, 36.55620, 36.55587, 36.22926, 36.22519, 36.55620
#> 5 -77.21767, -77.23461, -77.29861, -77.29351, -77.30948, -77.33499, -77.38903, -77.37862, -77.41347, -77.41885, -77.45412, -77.53808, -77.55743, -77.57195, -77.58008, -77.55968, -77.56042, -77.63597, -77.65099, -77.69888, -77.74940, -77.90121, -77.89886, -77.76393, -77.32005, -77.17735, -77.15442, -77.09213, -77.07531, -77.08327, -77.12733, -77.13932, -77.14196, -77.21767, 36.24098, 36.21460, 36.21153, 36.16286, 36.16277, 36.18285, 36.20310, 36.24009, 36.25582, 36.28180, 36.31976, 36.30246, 36.30421, 36.31450, 36.32827, 36.37595, 36.40636, 36.44054, 36.47259, 36.47896, 36.47357, 36.50989, 36.55294, 36.55344, 36.55392, 36.55629, 36.52625, 36.50752, 36.48352, 36.47010, 36.47071, 36.45648, 36.41706, 36.24098
#> 6 -76.74506, -76.98069, -76.99475, -77.13007, -77.21767, -77.14196, -77.13932, -77.12733, -77.08327, -77.07531, -77.09213, -77.15442, -77.17735, -76.92413, -76.90842, -76.94577, -76.95367, -76.94351, -76.92408, -76.74135, -76.70750, -76.74506, 36.23392, 36.23024, 36.23558, 36.23346, 36.24098, 36.41706, 36.45648, 36.47071, 36.47010, 36.48352, 36.50752, 36.52625, 36.55629, 36.55415, 36.50428, 36.45896, 36.41923, 36.40173, 36.39244, 36.31517, 36.26613, 36.23392
tar_read(by_row)
#> Error in `stop_vctrs()`:
#> ! Input must be a vector, not a <sfc_MULTIPOLYGON/sfc> object.
library(sf)
#> Warning: package 'sf' was built under R version 4.1.3
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
tar_read(by_row)
#> Simple feature collection with 6 features and 1 field
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -81.74107 ymin: 36.23436 xmax: -81.23989 ymax: 36.58965
#> Geodetic CRS: NAD27
#> AREA geometry
#> 1 0.114 MULTIPOLYGON (((-81.47276 3...
#> 2 0.061 MULTIPOLYGON (((-81.23989 3...
#> 3 0.143 MULTIPOLYGON (((-80.45634 3...
#> 4 0.070 MULTIPOLYGON (((-76.00897 3...
#> 5 0.153 MULTIPOLYGON (((-77.21767 3...
#> 6 0.097 MULTIPOLYGON (((-76.74506 3...Created on 2022-03-18 by the reprex package (v2.0.1.9000) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to dynamically branch over an sf object to use each individual row's geometry as an input to a web query. I don't understand why there is an error when doing this? In my mind, it should operate very similarly to mapping over a data.frame. I want to branch over the geometries so that I only re-download data if the polygon updates.
Here is an example
_targets.Rfile for this use case.When running
tar_make(state_map_error), I get the following error:Beta Was this translation helpful? Give feedback.
All reactions