diff --git a/NEWS.md b/NEWS.md index 7207cc65dc..508cc27060 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,7 +2,7 @@ ## NEW FEATURES -- Added a function `convert_chunk_header()` to convert the old in-header chunk options to the new in-body chunk options (#2149). +- Added a function `convert_chunk_header()` to convert the old in-header chunk options to the new in-body chunk options (#2149 #2151). - Added support for a `php` engine like other engines for interpreted languages. It will call `php -r `, with `` being the chunk content (thanks, @ralmond, #2144). diff --git a/R/parser.R b/R/parser.R index 221f717241..45accb92a4 100644 --- a/R/parser.R +++ b/R/parser.R @@ -739,9 +739,8 @@ inline_expr = function(code, syntax) { #' line. Long chunk option values will be wrapped onto several lines, and you #' can use \code{width = 0} to keep one line per option only. \code{"wrap"} #' will wrap all chunk options together using -#' \code{\link[base:strwrap]{base::strwrap}()}. \code{"yaml"} is currently not -#' implemented and here as a placeholder for future support of YAML in-chunk -#' syntax for options. +#' \code{\link[base:strwrap]{base::strwrap}()}. \code{"yaml"} will convert +#' chunk options to YAML. #' @param width An integer passed to \code{base::strwrap()} for \code{type = #' "wrap"} and \code{type = "multiline"}. If set to \code{0}, deactivate the #' wrapping (for \code{type = "multiline"} only). @@ -807,8 +806,6 @@ convert_chunk_header = function( ) { type = match.arg(type) - if (type == 'yaml') stop('Convertion to YAML chunk header not implemented yet.') - # extract fenced header information text = xfun::read_utf8(input) ext = xfun::file_ext(input) @@ -858,7 +855,26 @@ convert_chunk_header = function( strwrap(params3, width, prefix = prefix) } } else { - # YAML + params3 = parse_params(params2, label = FALSE) + + # fix un-evaluated options for yaml by transforming to !expr val + params3 = lapply(params3, function(x) { + if (is.symbol(x) || is.language(x)) { + x = deparse(x, 500L) + attr(x, 'tag') = '!expr' + } + x + }) + # convert to yaml and add prefix + params3 = strsplit(yaml::as.yaml( + params3, handlers = list( + # true / false instead of no + logical = function(x) { + x = tolower(x) + class(x) = 'verbatim' + x + }), line.sep = '\n'), '\n')[[1]] + params3 = paste0(prefix, params3) } if (nzchar(opt_chars$end)) params3 = paste0(params3, opt_chars$end) diff --git a/R/utils.R b/R/utils.R index a727873d02..4e8902cbbf 100644 --- a/R/utils.R +++ b/R/utils.R @@ -650,10 +650,11 @@ escape_html = highr:::escape_html #' @return A character vector of the source code. #' @author Yihui Xie and Peter Ruckdeschel #' @export -#' @examples library(knitr) -#' \donttest{# relies on r-forge.r-project.org being accessible +#' @examplesIf interactive() +#' library(knitr) +#' # relies on r-forge.r-project.org being accessible #' read_rforge('rgl/R/axes.R', project = 'rgl') -#' read_rforge('rgl/R/axes.R', project = 'rgl', extra='&revision=519')} +#' read_rforge('rgl/R/axes.R', project = 'rgl', extra='&revision=519') read_rforge = function(path, project, extra = '') { base = 'http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg' read_utf8(sprintf('%s/%s?root=%s%s', base, path, project, extra)) diff --git a/man/convert_chunk_header.Rd b/man/convert_chunk_header.Rd index 906d8777aa..0f11e1a5e4 100644 --- a/man/convert_chunk_header.Rd +++ b/man/convert_chunk_header.Rd @@ -25,9 +25,8 @@ the default is \code{"yaml"}) will write each chunk option on a separate line. Long chunk option values will be wrapped onto several lines, and you can use \code{width = 0} to keep one line per option only. \code{"wrap"} will wrap all chunk options together using -\code{\link[base:strwrap]{base::strwrap}()}. \code{"yaml"} is currently not -implemented and here as a placeholder for future support of YAML in-chunk -syntax for options.} +\code{\link[base:strwrap]{base::strwrap}()}. \code{"yaml"} will convert +chunk options to YAML.} \item{width}{An integer passed to \code{base::strwrap()} for \code{type = "wrap"} and \code{type = "multiline"}. If set to \code{0}, deactivate the diff --git a/man/read_rforge.Rd b/man/read_rforge.Rd index 3450415e15..8fdcb192f0 100644 --- a/man/read_rforge.Rd +++ b/man/read_rforge.Rd @@ -20,13 +20,12 @@ A character vector of the source code. \description{ This function reads source code from the SVN repositories on R-Forge. } -\examples{ +\examples{\dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(knitr) -\donttest{ # relies on r-forge.r-project.org being accessible read_rforge("rgl/R/axes.R", project = "rgl") read_rforge("rgl/R/axes.R", project = "rgl", extra = "&revision=519") -} +\dontshow{\}) # examplesIf} } \author{ Yihui Xie and Peter Ruckdeschel