Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make quarto_render(as_job = TRUE) wrapable #105

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion R/publish.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ find_app_primary_doc <- function(dir) {
break
}
}
return (primary_doc)
return(primary_doc)
}
}
return(NULL)
Expand Down
15 changes: 12 additions & 3 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,18 @@ quarto_render <- function(input = NULL,
if (as_job && rstudioapi::isAvailable()) {
message("Rendering project as background job (use as_job = FALSE to override)")
script <- tempfile(fileext = ".R")
render_args <- as.list(sys.call()[-1L])
render_args <- mapply(
function(arg, arg_name) paste0(
arg_name,
"="[nchar(arg_name) > 0L],
deparse1(eval(arg, envir = parent.frame(n = 3L)))
),
render_args,
names(render_args)
)
Comment on lines +110 to +119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to handle this a bit differently for complex wrapping calls that would use ... for example. In this case, it will not work. rlang maybe be useful for this. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, I didn't test with a wrapper function that uses dots, I only tested simple named args. Feel free to refactor and expand on this PR as you see fit!

writeLines(
c("library(quarto)", deparse(sys.call())),
paste0("quarto::quarto_render(", paste0(render_args, collapse = ", "), ")"),
script
)
rstudioapi::jobRunScript(
Expand All @@ -96,10 +106,9 @@ quarto_render <- function(input = NULL,
workingDir = getwd(),
importEnv = TRUE
)
return (invisible(NULL))
return(invisible(NULL))
}


# build args
args <- c("render", input)
if (!missing(output_format)) {
Expand Down
18 changes: 17 additions & 1 deletion tests/testthat/test-render.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

test_that("An error is reported when Quarto is not installed", {
skip_if(!is.null(quarto_path()))
expect_error(quarto_render("test.Rmd"))
Expand All @@ -11,3 +10,20 @@ test_that("R Markdown documents can be rendered", {
expect_true(file.exists("test.html"))
unlink("test.html")
})


test_that("`quarto_render(as_job = TRUE)` is wrapable", {
skip_if(is.null(quarto_path()))
skip_if_not_installed("rstudioapi")
skip_if_not_installed("rprojroot")
skip_if_not(rstudioapi::isAvailable())
dir <- rprojroot::find_testthat_root_file()
input <- file.path(dir, "test.Rmd")
output <- file.path(dir, "test.html")
wrapper <- function(path) quarto_render(path, quiet = TRUE, as_job = TRUE)
wrapper(input)
# wait for background job to finish (10s should be conservative enough)
Sys.sleep(10)
expect_true(file.exists(output))
unlink(output)
})