From 7b5437e80f31094b7c7449432e767ea2382a4d01 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Thu, 7 Jul 2022 14:16:45 +1200 Subject: [PATCH 1/3] Set tidyverse standard knitr options (#1382) Fixes #1380. Fixes #1376 --- NEWS.md | 3 ++- R/markdown.R | 4 ++- man/markdown_pass1.Rd | 12 +++------ tests/testthat/_snaps/markdown-code.md | 25 ++++++++++++++++++ tests/testthat/test-markdown-code.R | 35 ++++++++++++++++++++------ 5 files changed, 62 insertions(+), 17 deletions(-) diff --git a/NEWS.md b/NEWS.md index 728a0b50..3b9340b6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,7 +5,8 @@ * Code evaluated in inline markdown code chunks and `@eval`/`@evalRd`/ `@evalNamespace` is now evaluated in an environment designed to be more reproducible and to suppress output that won't work in Rd (e.g. turning - off colour and unicode support in cli) (#1351). + off colour and unicode support in cli) (#1351). They now also set + knitr options `comment = #>` (#1380) and `collapse = TRUE` (#1376). # roxygen2 7.2.0 diff --git a/R/markdown.R b/R/markdown.R index 2f4638da..b78e41d3 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -116,7 +116,9 @@ eval_code_node <- function(node, env) { knitr_chunk_defaults <- list( error = FALSE, fig.path = "man/figures/", - fig.process = function(path) basename(path) + fig.process = function(path) basename(path), + comment = "#>", + collapse = TRUE ) str_set_all_pos <- function(text, pos, value, nodes) { diff --git a/man/markdown_pass1.Rd b/man/markdown_pass1.Rd index 86ae2fcc..63ef0540 100644 --- a/man/markdown_pass1.Rd +++ b/man/markdown_pass1.Rd @@ -33,20 +33,16 @@ The \code{iris} data set has 5 columns: \if{html}{\out{
}}\preformatted{# Code block demo x + 1 -}\if{html}{\out{
}} - -\if{html}{\out{
}}\preformatted{## [1] 101 +#> [1] 101 }\if{html}{\out{
}} Chunk options: \if{html}{\out{
}}\preformatted{names(mtcars) nrow(mtcars) -}\if{html}{\out{
}} - -\if{html}{\out{
}}\preformatted{## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" -## [11] "carb" -## [1] 32 +#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" +#> [11] "carb" +#> [1] 32 }\if{html}{\out{
}} Plots: diff --git a/tests/testthat/_snaps/markdown-code.md b/tests/testthat/_snaps/markdown-code.md index 9f2f3fbc..a1eee718 100644 --- a/tests/testthat/_snaps/markdown-code.md +++ b/tests/testthat/_snaps/markdown-code.md @@ -24,3 +24,28 @@ 1: 1 + ^ +# interleaving fences and inline code + + Code + cat(out1$get_value("details")) + Output + Details 10 + + \if{html}{\out{
}}\preformatted{y <- x + 10 + y + #> [1] 20 + }\if{html}{\out{
}} + +# preserves white space + + Code + cat(out1$get_value("details")) + Output + \if{html}{\out{
}}\preformatted{a <- 1 + + b <- 2 + }\if{html}{\out{
}} + + \if{html}{\out{
}}\preformatted{c <- 3 + }\if{html}{\out{
}} + diff --git a/tests/testthat/test-markdown-code.R b/tests/testthat/test-markdown-code.R index 44821300..e4878a2f 100644 --- a/tests/testthat/test-markdown-code.R +++ b/tests/testthat/test-markdown-code.R @@ -5,7 +5,7 @@ test_that("can eval inline code", { #' @description Description `r 2 + 2` #' @md foo <- function() NULL - + ")[[1]] expect_equal(out1$get_value("title"), "Title 2") expect_equal(out1$get_value("description"), "Description 4") @@ -21,7 +21,7 @@ test_that("can eval fenced code", { #' ``` #' @md foo <- function() NULL - + ")[[1]] expect_match(out1$get_value("details"), "2") }) @@ -35,7 +35,7 @@ test_that("use same env within, but not across blocks", { bar <- function() NULL #' Title - #' + #' #' Description `r exists('baz', inherits = FALSE)` #' @md zap <- function() NULL @@ -56,7 +56,7 @@ test_that("appropriate knit print method for fenced and inline is applied", { ) out1 <- roc_proc_text(rd_roclet(), " #' @title Title `r structure('default', class = 'foo')` - #' + #' #' @details Details #' #' ```{r} @@ -153,9 +153,30 @@ test_that("interleaving fences and inline code", { #' @name dummy NULL")[[1]] - details <- out1$get_value("details") - expect_match(details, "Details 10", fixed = TRUE) - expect_match(details, "## [1] 20", fixed = TRUE) + expect_snapshot(cat(out1$get_value("details"))) +}) + +test_that("preserves white space", { + out1 <- roc_proc_text(rd_roclet(), " + #' Title + #' + #' @details + #' + #' ```{r} + #' a <- 1 + #' + #' b <- 2 + #' ``` + #' + #' ```{r} + #' c <- 3 + #' ``` + #' + #' @md + #' @name dummy + NULL")[[1]] + + expect_snapshot(cat(out1$get_value("details"))) }) test_that("fence options are used", { From f701e142fa4e75cc622f16ced73f116caebf53c7 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 6 Jul 2022 19:17:31 -0700 Subject: [PATCH 2/3] Use .empty() to test empty strings (#1356) See https://clang.llvm.org/extra/clang-tidy/checks/readability-container-size-empty.html --- src/parser2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser2.cpp b/src/parser2.cpp index 5f6e1597..765b7a16 100644 --- a/src/parser2.cpp +++ b/src/parser2.cpp @@ -118,7 +118,7 @@ cpp11::list tokenise_block(cpp11::strings lines, std::string file, if (!line.consumeRoxygenComment()) { // Incremenet curRow for non-roxygen comments at start of block - if (curVal == "") + if (curVal.empty()) curRow++; continue; } From 85c6e8ad0314fda13c1b00be82d9a3053fabca99 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 6 Jul 2022 19:17:48 -0700 Subject: [PATCH 3/3] typo in NEWS (#1346) --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 3b9340b6..b3f42b99 100644 --- a/NEWS.md +++ b/NEWS.md @@ -50,7 +50,7 @@ `\ifelse{}{}{}` tags are now inserted correctly (without additional `{}`) (#1062). -* `@inhert` now supports inheriting "Notes" with `@inherit pkg::fun note` +* `@inherit` now supports inheriting "Notes" with `@inherit pkg::fun note` (@pat-s, #1218) * Automatic `@usage` now correctly wraps arguments containing syntactically