Created
March 10, 2020 18:30
-
-
Save tim-salabim/e9672ecc696af70ef2dc87eee04ea840 to your computer and use it in GitHub Desktop.
Use {later} to watch for sf objects and automatically mapview them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This will watch your global environment for sf objects and mapview them automagically. | |
library(later) | |
library(mapview) | |
last_value = NULL | |
dir <- tempfile() | |
dir.create(dir) | |
htmlFile <- file.path(dir, "index.html") | |
get_sf_data = function() { | |
dat = ls(envir = .GlobalEnv) | |
cls = lapply(lapply(dat, get), class) | |
spatdat = dat[grep("sf", cls)] | |
lapply(spatdat, get) | |
} | |
mv_watch = function() { | |
spatdat_lst = get_sf_data() | |
if (length(spatdat_lst) > 0 && !identical(spatdat_lst, last_value)) { | |
m = mapview(spatdat_lst) | |
mapshot(m, htmlFile) | |
# (code to write some content to the file) | |
rstudioapi::viewer(htmlFile) | |
last_value <<- spatdat_lst | |
} | |
later::later(mv_watch, 0.25) | |
} | |
## initiate the watcher | |
mv_watch() | |
library(sf) | |
nc = st_read(system.file("shape/nc.shp", package="sf")) | |
nc = nc[, 1] |
Author
tim-salabim
commented
Mar 10, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment