From 8ff6d60cfb72114fe62a0b61c3b661b87daa8bb1 Mon Sep 17 00:00:00 2001 From: Ellis Hughes Date: Tue, 19 Oct 2021 13:58:23 -0700 Subject: [PATCH 1/2] Add interactive previewing of vt_file --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/vt_file.R | 40 ++++++++++++++++++++++++++--------- tests/testthat/test-vt_file.R | 21 ++++++++++++------ 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e7ecf82a..6081a9b1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,7 +35,7 @@ License: MIT + file LICENSE Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.2 Suggests: covr, XML, diff --git a/NAMESPACE b/NAMESPACE index 51309160..ec5f8e09 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -88,6 +88,7 @@ importFrom(kableExtra,kable_styling) importFrom(knitr,asis_output) importFrom(knitr,current_input) importFrom(knitr,kable) +importFrom(knitr,knit) importFrom(knitr,knit_child) importFrom(knitr,opts_knit) importFrom(lubridate,parse_date_time) diff --git a/R/vt_file.R b/R/vt_file.R index eb7bf038..18a12aa6 100644 --- a/R/vt_file.R +++ b/R/vt_file.R @@ -80,7 +80,7 @@ file_parse.default <- function(file, ..., dynamic_referencing = FALSE){ #' @importFrom knitr knit_child #' @importFrom withr with_options -file_parse.md <- function(file, ..., reference = NULL, envir = parent.frame(), dynamic_referencing = FALSE){ +file_parse.md <- function(file, ..., reference = NULL, envir = parent.frame(), interactive_output = interactive(), dynamic_referencing = FALSE){ if(dynamic_referencing){ text <- dynamic_reference_rendering(file, reference = reference) @@ -91,6 +91,28 @@ file_parse.md <- function(file, ..., reference = NULL, envir = parent.frame(), d ## remove roxygen comments text <- text[!grepl("^#'", text)] + if(interactive_output){ + file_parse.md.interactive(text, ..., envir = envir) + }else{ + file_parse.md.knitting(text, ..., envir = envir) + } + +} + +#' @importFrom knitr knit +file_parse.md.interactive <- function(text, ..., envir = parent.frame()){ + with_options(new = list(knitr.duplicate.label = "allow"), { + cat(asis_output(knit( + text = text, + envir = envir, + ..., + quiet = TRUE + ))) + }) +} + +#' @importFrom knitr knit_child +file_parse.md.knitting <- function(text, ..., envir = parent.frame()){ with_options(new = list(knitr.duplicate.label = "allow"), { cat(asis_output(knit_child( text = text, @@ -101,11 +123,12 @@ file_parse.md <- function(file, ..., reference = NULL, envir = parent.frame(), d }) } + file_parse.rmd <- file_parse.md -file_parse.r_test_code <- function(file, ..., reference = NULL, envir = parent.frame(), dynamic_referencing = FALSE){ +file_parse.r_test_code <- function(file, ..., reference = NULL, envir = parent.frame(), interactive_output = interactive(), dynamic_referencing = FALSE){ text <- c("```{r echo = FALSE}", paste0("results <- eval_test_code(path = ",bquote(file),")"), @@ -116,14 +139,11 @@ file_parse.r_test_code <- function(file, ..., reference = NULL, envir = parent.f text <- text[!is.na(text)] - with_options(new = list(knitr.duplicate.label = "allow"), { - cat(asis_output(knit_child( - text = text, - envir = envir, - ..., - quiet = TRUE - ))) - }) + if(interactive_output){ + file_parse.md.interactive(text, ..., envir = envir) + }else{ + file_parse.md.knitting(text, ..., envir = envir) + } } #' output to render kable to diff --git a/tests/testthat/test-vt_file.R b/tests/testthat/test-vt_file.R index 095582e1..3aa093de 100644 --- a/tests/testthat/test-vt_file.R +++ b/tests/testthat/test-vt_file.R @@ -28,45 +28,48 @@ test_that("evaluting markdown files works", { referencer <- vt_dynamic_referencer$new() sample_output <- capture.output({ - cat(file_parse.md(file = "sample.md")) + cat(file_parse.md(file = "sample.md", interactive_output = TRUE)) }) sample_output2 <- capture.output({ cat(file_parse.md( file = "sample2.md", reference = referencer, + interactive_output = TRUE, dynamic_referencing = TRUE)) }) + sample_output3 <- capture.output({ - vt_file(file = "sample.md") + vt_file(file = "sample.md", interactive_output = TRUE) }) sample_output4 <- capture.output({ vt_file( file = "sample2.md", reference = referencer, + interactive_output = TRUE, dynamic_referencing = TRUE) }) expect_equal( sample_output, - c("","## Header", "+ Content", " + more content", "+ Content 2") + c("## Header", "+ Content", " + more content", "+ Content 2") ) expect_equal( sample_output2, - c("","## Header", "+ 1.1 Reference", " + more content", "+ Content 2") + c("## Header", "+ 1.1 Reference", " + more content", "+ Content 2") ) expect_equal( sample_output3, - c("","## Header", "+ Content", " + more content", "+ Content 2") + c("## Header", "+ Content", " + more content", "+ Content 2") ) expect_equal( sample_output4, - c("","## Header", "+ 1.1 Reference", " + more content", "+ Content 2") + c("## Header", "+ 1.1 Reference", " + more content", "+ Content 2") ) }) @@ -115,6 +118,7 @@ test_that("evaluating Rmarkdown files works", { sample_output <- capture.output({ file_parse.rmd( file = "sample.Rmd", + interactive_output = FALSE, envir = curr_env) }) @@ -122,12 +126,14 @@ test_that("evaluating Rmarkdown files works", { file_parse.rmd( file = "sample2.Rmd", reference = referencer, + interactive_output = FALSE, dynamic_referencing = TRUE, envir = curr_env) }) sample_output3 <- capture.output({ vt_file(file = "sample.Rmd", + interactive_output = FALSE, envir = curr_env) }) @@ -135,10 +141,13 @@ test_that("evaluating Rmarkdown files works", { vt_file( file = "sample2.Rmd", reference = referencer, + interactive_output = FALSE, dynamic_referencing = TRUE, envir = curr_env) }) + browser() + expect_equal( sample_output, c("","","", From 6f468c5677eca80be70c8101a47609cd4d1e4848 Mon Sep 17 00:00:00 2001 From: Ellis Hughes Date: Tue, 19 Oct 2021 14:21:44 -0700 Subject: [PATCH 2/2] Allow interactive printing of md and Rmd files --- R/vt_file.R | 1 + tests/testthat/test-latex_dynamic_referencing.R | 4 ++++ tests/testthat/test-vt_file.R | 2 -- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/vt_file.R b/R/vt_file.R index 18a12aa6..1c339486 100644 --- a/R/vt_file.R +++ b/R/vt_file.R @@ -108,6 +108,7 @@ file_parse.md.interactive <- function(text, ..., envir = parent.frame()){ ..., quiet = TRUE ))) + cat("\n") }) } diff --git a/tests/testthat/test-latex_dynamic_referencing.R b/tests/testthat/test-latex_dynamic_referencing.R index 15ec3607..15e3435b 100644 --- a/tests/testthat/test-latex_dynamic_referencing.R +++ b/tests/testthat/test-latex_dynamic_referencing.R @@ -1,6 +1,8 @@ test_that("latex Number Referencing across rmarkdown chunks", { skip_if_not_installed("valtools") + skip_if_not(rmarkdown::pandoc_version() == numeric_version("2.7.3")) + withr::with_tempdir({ ## Create test files @@ -91,6 +93,8 @@ test_that("latex Number Referencing across rmarkdown chunks", { ## this test demonstrates how to use native latex + bookdown::render_book for dynamic labeling ## does not depend on valtools dynamic labeling skip_if_not_installed("valtools") + skip_if_not(rmarkdown::pandoc_version() == numeric_version("2.7.3")) + withr::with_tempdir({ ## Create test files diff --git a/tests/testthat/test-vt_file.R b/tests/testthat/test-vt_file.R index 3aa093de..ed3b559b 100644 --- a/tests/testthat/test-vt_file.R +++ b/tests/testthat/test-vt_file.R @@ -146,8 +146,6 @@ test_that("evaluating Rmarkdown files works", { envir = curr_env) }) - browser() - expect_equal( sample_output, c("","","",