Skip to content

Commit 060b01a

Browse files
authored
Merge pull request #109 from opengeos/wd3
`wbt_wd()`: fix behavior and message for `wd=""`
2 parents 7eaff5d + 0621cc2 commit 060b01a

File tree

4 files changed

+44
-25
lines changed

4 files changed

+44
-25
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Add `wbt_runner_path()` (analog of `wbt_exe_path()` for `whitebox_runner` GUI executable) and `wbt_launch_runner()` a simple function to launch the GUI.
66

7+
* `wbt_wd("")` now sets the value of `working_directory` in the WhiteboxTools settings.json file to `""` and triggers background options to prevent `--wd` flag being added until a new working directory is set. This has been a long-standing issue, resolved following <https://github.com/opengeos/whiteboxR/issues/108>.
8+
79
# whitebox 2.3.0
810

911
* Updates for WhiteboxTools v2.3.0 (https://github.com/jblindsay/whitebox-tools/releases/tag/v2.3.0)

R/wbt.R

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ wbt_init <- function(exe_path = wbt_exe_path(shell_quote = FALSE),
4040
!is.null(wd) ||
4141
!is.null(verbose) ||
4242
!is.null(compress_rasters)) {
43+
44+
if (!is.null(wd) && length(wd) > 0 && (is.na(wd) || wd == "")) {
45+
.wbt_wd_unset()
46+
}
47+
4348
# set the path with wbt_options
4449
wbt_options(exe_path = exe_path, ...)
4550
}
@@ -288,43 +293,38 @@ wbt_data_dir <- function() {
288293
#'
289294
#' @return `wbt_wd()`: character; when working directory is unset, will not add `--wd=` arguments to calls and should be the same as using `getwd()`. See Details.
290295
#'
291-
#' @details `wbt_wd()`: Before you set the working directory in a session the default output will be in your current R working directory unless otherwise specified. You can change working directory at any time by setting the `wd` argument to `wbt_wd()` and running a tool. Note that once you have set a working directory, the directory needs to be set somewhere to "replace" the old value; just dropping the flag will not change the working directory back to the R working directory. To "unset" the option in the R package you can use `wbt_wd("")` which is equivalent to `wbt_wd(getwd())`.
296+
#' @details `wbt_wd()`: Before you set the working directory in a session the default output will be in your current R working directory unless otherwise specified. You can change working directory at any time by setting the `wd` argument to `wbt_wd()` and running a tool. Note that once you have set a working directory, the directory needs to be set somewhere to "replace" the old value; just dropping the flag will not change the working directory back to the R working directory. To "unset" the option in the R package you can use `wbt_wd("")` which removes the `--wd` flag from commands and sets the `working_directory` value in the WhiteboxTools _settings.json_ to `""`.
292297
#' @rdname wbt_init
293298
#' @export
294299
#' @keywords General
295300
#' @examples
296301
#' \dontrun{
297302
#'
298303
#' ## wbt_wd():
299-
#'
304+
#'
305+
#' # no working directory set
306+
#' wbt_wd(wd = "")
307+
#'
300308
#' # set WBT working directory to R working directory
301309
#' wbt_wd(wd = getwd())
302310
#' }
303311
wbt_wd <- function(wd = NULL) {
304312

305-
# system environment var takes precedence
306-
syswd <- Sys.getenv("R_WHITEBOX_WD")
307-
if (nchar(syswd) > 0 && dir.exists(syswd)) {
308-
return(syswd)
309-
}
310-
311313
if (length(wd) > 0 && (is.na(wd) || wd == "")) {
312-
curwd <- getwd()
313-
if(wbt_verbose()) {
314-
cat("Reset WhiteboxTools working directory to current R working directory:", curwd)
315-
}
316-
try(wbt_system_call(paste0("--wd=", curwd)), silent = TRUE)
317-
if (wbt_verbose()) {
318-
cat("Unset WhiteboxTools working directory flag `whitebox.wd` / `--wd`\n")
319-
}
320-
wd <- "" #structure(curwd, unset = TRUE)
314+
.wbt_wd_unset()
321315
}
322316

323317
if (is.character(wd)) {
324318
# if character input, set the package option "wd"
325319
wbt_options(wd = wd)
326320
}
327321

322+
# system environment var takes precedence
323+
syswd <- Sys.getenv("R_WHITEBOX_WD")
324+
if (nchar(syswd) > 0 && dir.exists(syswd)) {
325+
return(syswd)
326+
}
327+
328328
# package option checked next; if missing default is getwd() (unspecified should be same as getwd())
329329
res <- getOption("whitebox.wd")
330330

@@ -333,13 +333,24 @@ wbt_wd <- function(wd = NULL) {
333333
res <- ""
334334
# otherwise, if the option is invalid directory message
335335
} else if (!is.null(res) && !dir.exists(res)) {
336-
message("Invalid path for `whitebox.wd`: directory does not exist.\nDefaulting to current R working directory: ", getwd())
337-
res <- getwd()
336+
message("Invalid path for `whitebox.wd`: directory does not exist.\nDefaulting to \"\"")
337+
res <- ""
338338
}
339339

340340
invisible(res)
341341
}
342342

343+
.wbt_wd_unset <- function() {
344+
# this doesnt actually set the value ""
345+
# - try(wbt_system_call(paste0("--wd=", shQuote(""))), silent = TRUE)
346+
try({
347+
f <- file.path(dirname(wbt_exe_path(shell_quote = FALSE)), "settings.json")
348+
x <- readLines(f, warn = FALSE)
349+
x[grepl('^ *"working_directory": .*$', x)] <- ' "working_directory": "",'
350+
writeLines(x, f)
351+
})
352+
}
353+
343354
#' @description `wbt_verbose()`: Check verbose options for 'WhiteboxTools'
344355
#'
345356
#' @param verbose Default: `NULL`; if logical, set the package option `whitebox.verbose` to specified value
@@ -581,8 +592,9 @@ wbt_install <- function(pkg_dir = wbt_data_dir(), platform = NULL, force = FALSE
581592
}
582593
res <- -1
583594
for (method in c(if (os == "Windows") "internal", "libcurl", "auto")) {
584-
if (!inherits(try(res <- utils::download.file(url, exe_zip, method = method), silent = TRUE),
585-
"try-error") && res == 0)
595+
if (!inherits(try({
596+
res <- utils::download.file(url, exe_zip, method = method)
597+
}, silent = TRUE), "try-error") && res == 0)
586598
break
587599
}
588600

@@ -612,7 +624,7 @@ wbt_install <- function(pkg_dir = wbt_data_dir(), platform = NULL, force = FALSE
612624
cat(" wbt_version()\n")
613625

614626
# call wbt_init (sets package option)
615-
wbt_init(exe_path = exe_path_out)
627+
wbt_init(exe_path = exe_path_out, wd = "")
616628
}
617629

618630
} else if (!force) {

man/wbt_init.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-wbt.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ test_that("wbt setting and using working directories", {
108108
expect_length(list.files(pattern = basename(tf)), 0)
109109
expect_length(list.files(path = mywd, pattern = basename(tf)), 1)
110110

111-
# NA or "" are converted to getwd() with an attribute set to remove the flag after next
112-
expect_equal(wbt_wd(NA), "")
111+
# "" resets wd flag and returns ""
112+
expect_equal(wbt_wd(""), "")
113113

114114
# cleanup
115115
unlink(tf)
@@ -203,6 +203,8 @@ test_that("wbt raster compression (requires WhiteboxTools v2.1.0 or higher)", {
203203

204204
skip_if(dem == "")
205205

206+
wbt_wd(getwd())
207+
206208
wbt_geomorphons(sample_dem_data(), output = "test_compressed.tif", compress_rasters = TRUE)
207209

208210
wbt_geomorphons(sample_dem_data(), output = "test_no-compress.tif", compress_rasters = FALSE)

0 commit comments

Comments
 (0)