Skip to content

Instantly share code, notes, and snippets.

@brry
Created February 3, 2019 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brry/7728b9b2d35afad7f1fc5978c3315009 to your computer and use it in GitHub Desktop.
Save brry/7728b9b2d35afad7f1fc5978c3315009 to your computer and use it in GitHub Desktop.
if(!requireNamespace("devtools")) install.packages("devtools")
if(!requireNamespace("osmplotr")) install.packages("osmplotr")
dir.create("sfsubsettest/R", recursive=TRUE)
setwd("sfsubsettest")
cat("Package: sfsubsettest
Title: Check sf Subset
Version: 0.0.1
Author: Berry Boessenkool
Maintainer: Berry Boessenkool <berry-b@gmx.de>
License: GPL-2
Description: Check sf Subset.
Imports: osmplotr\n", file="DESCRIPTION")
cat(paste("#' dummy function\n#' @param map map\n#' @param obj obj",
"#' @importFrom osmplotr add_osm_objects\n#' @export",
"dummy <- function(map,obj)\n{",
" add_osm_objects(map, obj[1:10,], col='green')\n}", sep="\n"),
file="R/dummy.R")
devtools::document()
devtools::install()
library(sfsubsettest)
library(osmplotr)
bbox <- get_bbox(c(12.5, 53.1, 12.6, 53.2))
dat_F <- extract_osm_objects(key="landuse", bbox=bbox, quiet=TRUE, value="forest")
map <- osm_basemap(bbox=bbox, bg='gray95')
map <- dummy(map, dat_F) # class(obj$geometry) problem
class(dat_F$geometry) # sfc
class(dat_F[1:10,]$geometry) # list
# now change "[" to sf:::"[.sf"
cat(paste("#' dummy function\n#' @param map map\n#' @param obj obj",
"#' @importFrom osmplotr add_osm_objects\n#' @export",
"dummy <- function(map,obj)\n{",
" add_osm_objects(map, sf:::'[.sf'(obj, 1:10, 1:ncol(obj)), col='green')\n}", sep="\n"),
file="R/dummy.R")
devtools::document()
devtools::install()
library(sfsubsettest)
map <- dummy(map, dat_F) # now works fine
map
remove.packages("sfsubsettest")
setwd("..")
unlink("sfsubsettest", recursive=TRUE)
@mpadge
Copy link

mpadge commented Feb 4, 2019

See comment here - this will always fail without sf being explicitly loaded because (1) osmplotr does not, nor will, depend on sf, that's an uneccessarily "heavy" dependency; and (2) sub-setting is arguably an sf operation that lies beyond the remit of osmplotr, and so it is fair to expect sf to be loaded for that operation. There is unfortunately no place within osmplotr at which a suitably useful message might be issued regarding the necessity of loading sf for such operations, because the objects returned by extract_osm_objects() are intended to be indistinguishable from any sf objects generated with that package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment