Skip to content

Commit

Permalink
fixes #784: also provide vignette engines with tangle disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Jun 2, 2014
1 parent a80f48d commit 663466b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGES IN knitr VERSION 1.7

## NEW FEATURES

- added vignette engines with the suffix `_notangle`, which have the same weave functions as those engines without this suffix but have disabled the tangle function, meaning there will not be R scripts generated from the vignettes during `R CMD build` or `R CMD check` (thanks, Carl Boettiger and Michael Koohafkan, #784)

## BUG FIXES

- fixed #779: when the chunk options `tidy=FALSE` and `eval=FALSE`, `prompt=TRUE` did not work for R expressions of multiple lines (thanks, Qijie Zhao)
Expand Down
38 changes: 35 additions & 3 deletions R/utils-vignettes.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# Wrappers to use in vignette building for R 3.0.0
#' Package vignette engines
#'
#' Since R 3.0.0, package vignettes can use non-Sweave engines, and \pkg{knitr}
#' has provided a few engines to compile vignettes via \code{\link{knit}()} with
#' different templates. See \url{http://yihui.name/knitr/demo/vignette/} for
#' more information.
#' @name vignette_engines
#' @examples library(knitr)
#' vig_list = if (getRversion() > '3.0.0') tools::vignetteEngine(package = 'knitr')
#' str(vig_list)
#' vig_list[['knitr::knitr']][c('weave', 'tangle')]
#' vig_list[['knitr::knitr_notangle']][c('weave', 'tangle')]
#' vig_list[['knitr::docco_classic']][c('weave', 'tangle')]
NULL

vweave = vtangle = function(file, driver, syntax, encoding = '', quiet = FALSE, ...) {
opts_chunk$set(error = FALSE) # should not hide errors
Expand Down Expand Up @@ -27,6 +40,16 @@ body(vweave_rmarkdown)[5L] = expression(getFromNamespace('render', 'rmarkdown')(
file, encoding = encoding, quiet = quiet, envir = globalenv()
))

# do not tangle R code from vignettes
untangle_weave = function(weave) {
body(weave)[3L] = expression({})
weave
}
vtangle_empty = function(file, ...) {
unlink(sub_ext(file, 'R'))
return()
}

Rversion = getRversion()

register_vignette_engines = function(pkg) {
Expand All @@ -38,8 +61,17 @@ register_vignette_engines = function(pkg) {
vig_engine('rmarkdown', if (has_package('rmarkdown')) {
vweave_rmarkdown
} else vweave, '[.][Rr](md|markdown)$')
# vignette engines that disable tangle
vig_list = tools::vignetteEngine(package = 'knitr')
engines = grep('_notangle$', names(vig_list), value = TRUE, invert = TRUE)
for (eng in engines) vig_engine(
paste(sub('^knitr::', '', eng), 'notangle', sep = '_'),
untangle_weave(vig_list[[c(eng, 'weave')]]),
tangle = vtangle_empty,
pattern = vig_list[[c(eng, 'pattern')]]
)
}
# all engines use the same tangle and package arguments, so factor them out
vig_engine = function(...) {
tools::vignetteEngine(..., tangle = vtangle, package = 'knitr')
vig_engine = function(..., tangle = vtangle) {
tools::vignetteEngine(..., tangle = tangle, package = 'knitr')
}

0 comments on commit 663466b

Please sign in to comment.