diff --git a/R/get.R b/R/get.R index 92f4ec7..4ab8830 100644 --- a/R/get.R +++ b/R/get.R @@ -155,6 +155,13 @@ get_masked_functions <- function(){ #' get_used_functions <- function(file){ + if (grepl("*.Rmd$", file, ignore.case = TRUE)){ + tmpfile <- tempfile(fileext = ".R") + on.exit(unlink(tmpfile)) + knitr::purl(file, tmpfile) + file <- tmpfile + } + # catch error retfun <- safely(parse, quiet = FALSE, diff --git a/R/interact.R b/R/interact.R index 959d052..5852004 100644 --- a/R/interact.R +++ b/R/interact.R @@ -110,6 +110,13 @@ set_log_name_path <- function(log_name = NA, log_path = NA) { #' @noRd run_safely <- function(file) "dummy" +#' Is this a R Markdown file#' +#' @param file String. Path to file to execute +#' @noRd +is_rmarkdown <- function(file) { + grepl("*.Rmd$", file, ignore.case = TRUE) +} + #' Dummy function for running a file #' @noRd run_file <- function(file){ @@ -118,7 +125,13 @@ run_file <- function(file){ } else{ exec_env <- getOption("log.rx.exec.env") } - source(file, local = exec_env) + + if (is_rmarkdown(file)) { + rmarkdown::render(file, envir = exec_env) + } else ( + source(file, local = exec_env) + ) + } #' Safely run an R script and record results, outputs, messages, errors, warnings diff --git a/R/log.R b/R/log.R index 7e10223..b420b73 100644 --- a/R/log.R +++ b/R/log.R @@ -276,7 +276,7 @@ log_write <- function(file = NA, } if ("result" %in% to_report){ cleaned_log_vec <- c(cleaned_log_vec, - write_result()) + write_result(file)) } cleaned_log_vec <- c(cleaned_log_vec, diff --git a/R/writer.R b/R/writer.R index f10ac82..2b5268b 100644 --- a/R/writer.R +++ b/R/writer.R @@ -255,14 +255,19 @@ write_output <- function() { #' Format result attribute for writing #' +#' @param file String. Path to file to execute #' @return A formatted vector of results #' #' @noRd #' -write_result <- function() { +write_result <- function(file) { result <- get_log_element("result") - c("\nResult:", paste0("\t", capture.output(result$value))) + if (is_rmarkdown(file)) { + c("\nResult:", paste0("\t", capture.output(result))) + } else { + c("\nResult:", paste0("\t", capture.output(result$value))) + } } #' Format lint results for writing diff --git a/tests/testthat/test-writer.R b/tests/testthat/test-writer.R index dcd6d98..2ff04e3 100644 --- a/tests/testthat/test-writer.R +++ b/tests/testthat/test-writer.R @@ -176,7 +176,7 @@ test_that("write_result will return a formatted log result element", { run_safely_loudly(fp) - expect_identical(write_result(), + expect_identical(write_result(fp), c("\nResult:", paste0("\t", capture.output(data.frame(test = c(8, 6, 7, 5, 3, 0, 9)))))) log_remove()