Skip to content

Commit

Permalink
feat: #62 possible to change execute directory of the quarto process …
Browse files Browse the repository at this point in the history
…with an option
  • Loading branch information
akselthomsen committed Dec 13, 2024
1 parent 622714e commit a4f1d2d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
5 changes: 5 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ options::define_option(
desc = "The output directory of the log files. Default is the folder of the excuted script. log_dir can be a path as a character or it can be a function that takes the script path as input and returns the log directory. For more information see the examples of `run()` or `vignette('whirl')`."

Check warning on line 89 in R/options.R

View workflow job for this annotation

GitHub Actions / Megalinter / MegaLinter

file=/github/workspace/R/options.R,line=89,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 295 characters.
)

options::define_option(
option = "execute_dir",
default = NULL,
desc = "The working directory of the process executing each script. Defeault us to execute R files from the working directory when calling `run()` and all other functions from the directory of the script. To change provide a character path (used for all scripts) or a function that takes the script as input and returns the execution directory."

Check warning on line 95 in R/options.R

View workflow job for this annotation

GitHub Actions / Megalinter / MegaLinter

file=/github/workspace/R/options.R,line=95,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 347 characters.
)
23 changes: 18 additions & 5 deletions R/whirl_r_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ wrs_check_status <- function(self, private, super) {
wrs_log_script <- function(script, self, private, super) {
private$current_script <- script

# Set the execute directory of the Quarto process calling the script
quarto_execute_dir <- options::opt("execute_dir", env = "whirl")
if (is.null(quarto_execute_dir)) {
quarto_execute_dir <- switch(
get_file_ext(script),
"R" = getwd(),
normalizePath(dirname(script))
)
} else if (is.function(quarto_execute_dir)) {
quarto_execute_dir <- quarto_execute_dir(script)
}

if (!file.exists(quarto_execute_dir)) {
cli::cli_abort("Script {.val {script}} cannot be run because execute directory {.val {quarto_execute_dir}} does not exist")
}

# Execute the script

if (private$verbosity_level != "quiet") {
private$pb <- pb_script$new(
script = private$current_script,
Expand All @@ -239,11 +257,6 @@ wrs_log_script <- function(script, self, private, super) {

self$pb_update(status = "Running script")

quarto_execute_dir <- switch(get_file_ext(script),
"R" = getwd(),
normalizePath(dirname(script)) # TODO: Should this default be changed?
)

self$call(
func = \(...) quarto::quarto_render(...),
args = list(
Expand Down
18 changes: 12 additions & 6 deletions man/options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/options_params.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion tests/testthat/test-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ test_that("Run yaml config file", {

})


test_that("Change the log_dir to a path", {
#Custom path
custom_path <- withr::local_tempdir()
Expand Down Expand Up @@ -120,3 +119,28 @@ test_that("Change the log_dir with a function", {
expect_true()
})

test_that("Change the execute_dir to a path", {

custom_path <- withr::local_tempdir()
withr::local_options(whirl.execute_dir = custom_path)

test_script("success.R") |>
run() |>
expect_no_error()

withr::local_options(whirl.execute_dir = "this/path/does/not/exist")

test_script("success.R") |>
run() |>
expect_error()
})

test_that("Change the execute_dir to a function", {

withr::local_options(whirl.execute_dir = \(x) dirname(x))

test_script("success.R") |>
run() |>
expect_no_error()
})

0 comments on commit a4f1d2d

Please sign in to comment.