Hi,
I would like to know whether there's way to add a progress bar to a general function with does not imply iterating over multiple elements .
I had a look at the package progressr which provides tools to add a progress bar but I couldn't find any examples on functions that do not require iterations.
E.g. adding a progress bar to the join_progress funcion hereunder
Thank you for your help
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.3.2, PROJ 7.2.1; sf_use_s2() is TRUE
#> WARNING: different compile-time and runtime versions for GEOS found:
#> Linked against: 3.10.2-CAPI-1.16.0 compiled against: 3.9.1-CAPI-1.14.2
#> It is probably a good idea to reinstall sf, and maybe rgeos and rgdal too
library(raster)
#> Loading required package: sp
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:raster':
#>
#> intersect, select, union
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(spData)
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
library(progressr)
# enable the global progress handler
handlers(global = TRUE)
#> Error in globalCallingHandlers(condition = global_progression_handler): should not be called with handlers on the stack
set.seed(2018) # set seed for reproducibility
(bb_world = st_bbox(world)) # the world's bounds
#> xmin ymin xmax ymax
#> -180.00000 -89.90000 179.99999 83.64513
#> xmin ymin xmax ymax
#> -180.0 -89.9 180.0 83.6
random_df = tibble(
x = runif(n = 10, min = bb_world[1], max = bb_world[3]),
y = runif(n = 10, min = bb_world[2], max = bb_world[4])
)
random_points = random_df %>%
st_as_sf(coords = c("x", "y")) %>% # set coordinates
st_set_crs(4326) # set geographic CRS
world_random = world[random_points, ]
join_progress<-function(x) {
Sys.sleep(10)
st_join(x, world["name_long"])
}
random_joined = join_progress(random_points)