Skip to content

Commit

Permalink
Set tidyverse standard knitr options
Browse files Browse the repository at this point in the history
Fixes #1380. Fixes #1376
  • Loading branch information
hadley committed Jul 7, 2022
1 parent 5613b8e commit 0bcdfc9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 4 additions & 8 deletions man/markdown_pass1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions tests/testthat/_snaps/markdown-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,28 @@
1: 1 +
^

# interleaving fences and inline code

Code
cat(out1$get_value("details"))
Output
Details 10
\if{html}{\out{<div class="sourceCode r">}}\preformatted{y <- x + 10
y
#> [1] 20
}\if{html}{\out{</div>}}

# preserves white space

Code
cat(out1$get_value("details"))
Output
\if{html}{\out{<div class="sourceCode r">}}\preformatted{a <- 1
b <- 2
}\if{html}{\out{</div>}}
\if{html}{\out{<div class="sourceCode r">}}\preformatted{c <- 3
}\if{html}{\out{</div>}}

35 changes: 28 additions & 7 deletions tests/testthat/test-markdown-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -21,7 +21,7 @@ test_that("can eval fenced code", {
#' ```
#' @md
foo <- function() NULL
")[[1]]
expect_match(out1$get_value("details"), "2")
})
Expand All @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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", {
Expand Down

0 comments on commit 0bcdfc9

Please sign in to comment.