[d9ee58]: / R / basic-utils.R

Download this file

29 lines (28 with data), 853 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#' Create directory if it doesn't already exist, or
#' issue error if it does exist but the minimum access
#' requirements are not met, to avoid using directories
#' created for another purpose.
#'
#' @param file_path Location of directory to check/create
#' @param min_access Minimum access permissions ("rwx", "rx" etc.)
#'
#' @importFrom checkmate test_directory_exists assert_access
#'
makeifnot_dir <- function(file_path, min_access) {
# Set up directory if does not already exist
if (checkmate::test_directory_exists(file.path(
file_path
))) {
# Can we write to it?
checkmate::assert_access(file.path(
file_path, access = min_access
))
} else {
# Wrap me in a tryCatch
tryCatch(
dir.create(file_path),
# Convert warning to error
warning = function(w) rlang::abort(w$message)
)
}
}