@@ -17,9 +17,9 @@ get_hash <- function (graph, contracted = FALSE, force = FALSE) {
1717 return (hash )
1818}
1919
20- # ' Path to user cache directory
20+ # ' Path to user cache directory, optionally for specified city
2121# ' @noRd
22- m4ra_cache_dir <- function () {
22+ m4ra_cache_dir <- function (city = NULL ) {
2323
2424 # use envvar is set, so tempdir can be used on CRAN checks
2525 cache_dir <- Sys.getenv (" M4RA_CACHE_DIR" )
@@ -37,6 +37,18 @@ m4ra_cache_dir <- function () {
3737 }
3838 }
3939
40+ if (! is.null (city )) {
41+ checkmate :: assert_character (city , len = 1L )
42+ flist <- fs :: dir_ls (cache_dir , type = " directory" , recurse = TRUE )
43+ cache_dir <- flist [which (basename (flist ) == city )]
44+ if (length (cache_dir ) == 0L ) {
45+ cache_dir <- fs :: path (Sys.getenv (" M4RA_CACHE_DIR" ), city )
46+ }
47+ if (length (cache_dir ) > 1L ) {
48+ cli :: cli_abort (" Ambiguous city directories at {city_dir}." )
49+ }
50+ }
51+
4052 return (cache_dir )
4153}
4254
@@ -85,7 +97,7 @@ m4ra_cache_network <- function (graph, city) {
8597
8698 flist_out <- NULL
8799
88- cache_dir <- fs :: path ( m4ra_cache_dir (), city )
100+ cache_dir <- m4ra_cache_dir (city )
89101 city <- gsub (" \\ s+" , " -" , tolower (city ))
90102
91103 # Full graph:
@@ -119,7 +131,7 @@ cache_one_graph <- function (graph, city) {
119131 hash <- substring (a $ hash , 1 , 6 )
120132 mode <- a $ wt_profile
121133
122- cache_dir <- fs :: path ( m4ra_cache_dir (), city )
134+ cache_dir <- m4ra_cache_dir (city )
123135
124136 gname <- paste0 (
125137 " m4ra-" , city , " -" , mode , " -" ,
@@ -156,7 +168,7 @@ m4ra_load_cached_network <- function (city = NULL, mode = "foot",
156168 city <- dirs [length (dirs ) - 1L ]
157169 f <- filename
158170
159- flist <- fs :: dir_ls (fs :: path ( m4ra_cache_dir (), city ), regexp = mode )
171+ flist <- fs :: dir_ls (m4ra_cache_dir (city ), regexp = mode )
160172
161173 } else {
162174
@@ -166,11 +178,7 @@ m4ra_load_cached_network <- function (city = NULL, mode = "foot",
166178
167179 mode <- match.arg (tolower (mode ), c (" foot" , " bicycle" , " motorcar" ))
168180
169- flist <- fs :: dir_ls (m4ra_cache_dir (), type = " directory" , recurse = TRUE )
170- city_dir <- flist [which (basename (flist ) == city )]
171- if (length (city_dir ) > 1L ) {
172- cli :: cli_abort (" Ambiguous city directories at {city_dir}." )
173- }
181+ city_dir <- m4ra_cache_dir (city = city )
174182 flist <- fs :: dir_ls (city_dir , regexp = mode )
175183
176184 f <- grep (ptn , flist , value = TRUE )
0 commit comments