Skip to content

Commit

Permalink
Merge pull request #207 from phuse-org/vt_file_interactive_knitting
Browse files Browse the repository at this point in the history
vt_file interactive knitting
  • Loading branch information
thebioengineer authored Oct 28, 2021
2 parents 98fa8c5 + 6f468c5 commit 069f946
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 31 additions & 10 deletions R/vt_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -91,6 +91,29 @@ 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
)))
cat("\n")
})
}

#' @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,
Expand All @@ -101,11 +124,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),")"),
Expand All @@ -116,14 +140,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
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-latex_dynamic_referencing.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 13 additions & 6 deletions tests/testthat/test-vt_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

})
Expand Down Expand Up @@ -115,26 +118,30 @@ test_that("evaluating Rmarkdown files works", {
sample_output <- capture.output({
file_parse.rmd(
file = "sample.Rmd",
interactive_output = FALSE,
envir = curr_env)
})

sample_output2 <- capture.output({
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)
})

sample_output4 <- capture.output({
vt_file(
file = "sample2.Rmd",
reference = referencer,
interactive_output = FALSE,
dynamic_referencing = TRUE,
envir = curr_env)
})
Expand Down

0 comments on commit 069f946

Please sign in to comment.