Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ipeaGIT/r5r
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Apr 5, 2024
2 parents e5eaf50 + 16d8a70 commit 29048bd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
2 changes: 2 additions & 0 deletions r-package/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ Depends:
R (>= 3.6)
Imports:
checkmate,
cli,
concaveman,
data.table,
jsonlite,
rJava (>= 0.9-10),
rlang,
sf (>= 1.0-12),
sfheaders,
utils,
Expand Down
2 changes: 1 addition & 1 deletion r-package/R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}

# package global variables
r5r_env <- new.env(parent = emptyenv()) # nocov start
r5r_env <- new.env(parent = emptyenv())

.onLoad <- function(lib, pkg) {

Expand Down
26 changes: 26 additions & 0 deletions r-package/R/setup_r5.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ setup_r5 <- function(data_path,
message("\nUsing cached network.dat from ", dat_file)

} else {
# check if the user has permission to write to the data directory. if not,
# R5 won't be able to create the required files and will fail with a
# not-that-enlightening error
error_if_no_write_permission(data_path)

# stop r5 in case it is already running
suppressMessages( r5r::stop_r5() )
Expand Down Expand Up @@ -178,3 +182,25 @@ setup_r5 <- function(data_path,
return(r5r_core)

}

error_if_no_write_permission <- function(data_path) {
write_permission <- file.access(data_path, mode = 2)

normalized_path <- normalizePath(data_path)

if (write_permission == -1) {
cli::cli_abort(
c(
"Permission to write to {.path {normalized_path}} denied.",
i = paste0(
"{.pkg r5r} needs write privilege to create the network files. ",
"Please make sure you have this privilege in the provided directory."
)
),
class = "dir_permission_denied",
call = rlang::caller_env()
)
}

return(invisible(TRUE))
}
20 changes: 20 additions & 0 deletions r-package/tests/testthat/test-setup_r5.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,23 @@ test_that("'overwrite' parameter works correctly", {
)

})

test_that("throws error if write access to given dir is denied", {
# this test only works correctly with unix OSes. not sure how to change
# permissions from inside R in windows
skip_if_not(.Platform$OS.type == "unix")

invisible(file.copy(path, tempdir(), recursive = TRUE))

tmpdir <- file.path(tempdir(), "poa")

data_files <- list.files(tmpdir, full.names = TRUE)
files_to_remove <- data_files[grepl("network|\\.pbf\\.mapdb", data_files)]
if (length(files_to_remove) > 0) invisible(file.remove(files_to_remove))

Sys.chmod(tmpdir, "555")

expect_error(setup_r5(tmpdir), class = "dir_permission_denied")

Sys.chmod(tmpdir, "755")
})
58 changes: 32 additions & 26 deletions r-package/vignettes/fare_structure.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,21 @@ points <- read.csv(system.file("extdata/poa/poa_hexgrid.csv", package = "r5r"))

# calculate travel times function
calculate_travel_times <- function(fare) {

ttm_df <- travel_time_matrix(r5r_core,
origins = points,
destinations = points,
departure_datetime = as.POSIXct("13-05-2019 14:00:00",
format = "%d-%m-%Y %H:%M:%S"),
mode = c("WALK", "TRANSIT"),
fare_structure = fare_structure,
max_fare = fare,
max_trip_duration = 40,
max_walk_time = 20)
ttm_df <- travel_time_matrix(
r5r_core,
origins = points,
destinations = points,
mode = c("WALK", "TRANSIT"),
departure_datetime = as.POSIXct(
"13-05-2019 14:00:00",
format = "%d-%m-%Y %H:%M:%S"
),
time_window = 1,
fare_structure = fare_structure,
max_fare = fare,
max_trip_duration = 40,
max_walk_time = 20
)

return(ttm_df)
}
Expand Down Expand Up @@ -426,24 +430,26 @@ and compare the results the accessibility unconstrained by monetary costs:
```{r}
# calculate accessibility function
calculate_accessibility <- function(fare, fare_string) {
access_df <- accessibility(r5r_core,
origins = points,
destinations = points,
departure_datetime = as.POSIXct("13-05-2019 14:00:00",
format = "%d-%m-%Y %H:%M:%S"),
opportunities_colname = "healthcare",
mode = c("WALK", "TRANSIT"),
cutoffs = 40,
fare_structure = fare_structure,
max_fare = fare,
max_trip_duration = 40,
max_walk_time = 20,
progress = FALSE)
access_df <- accessibility(
r5r_core,
origins = points,
destinations = points,
mode = c("WALK", "TRANSIT"),
departure_datetime = as.POSIXct(
"13-05-2019 14:00:00",
format = "%d-%m-%Y %H:%M:%S"
),
time_window = 1,
opportunities_colname = "healthcare",
cutoffs = 40,
fare_structure = fare_structure,
max_fare = fare,
max_trip_duration = 40,
max_walk_time = 20,
progress = FALSE)
access_df$max_fare <- fare_string
return(access_df)
}
Expand Down

0 comments on commit 29048bd

Please sign in to comment.