Skip to content

Commit

Permalink
Merged origin/master into abhsarma-master
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Nov 1, 2024
2 parents a13640c + 47267f5 commit ce1dad8
Show file tree
Hide file tree
Showing 37 changed files with 250 additions and 1,350 deletions.
1 change: 0 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ Makefile
^codecov\.yml$
^NEWS\.md$
^cran-comments\.md$
vignettes/knitr-refcard.lyx
1 change: 0 additions & 1 deletion .Rinstignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
knitr-intro.lyx
knitr-refcard.lyx
2 changes: 0 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ jobs:
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-tinytex@v2-branch
env:
# install full prebuilt version
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/knitr-examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
make integration
shell: bash

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: failure()
with:
name: knitr-examples
Expand Down
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.48.1
Version: 1.48.7
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down Expand Up @@ -120,7 +120,7 @@ Imports:
highr (>= 0.11),
methods,
tools,
xfun (>= 0.44),
xfun (>= 0.48),
yaml (>= 2.1.19)
Suggests:
bslib,
Expand All @@ -134,6 +134,7 @@ Suggests:
jpeg,
JuliaCall (>= 0.11.1),
magick,
litedown,
markdown (>= 1.3),
png,
ragg,
Expand All @@ -156,7 +157,7 @@ License: GPL
URL: https://yihui.org/knitr/
BugReports: https://github.com/yihui/knitr/issues
Encoding: UTF-8
VignetteBuilder: knitr
VignetteBuilder: litedown, knitr
SystemRequirements: Package vignettes based on R Markdown v2 or reStructuredText require Pandoc (http://pandoc.org).
The function rst2pdf() requires rst2pdf (https://github.com/rst2pdf/rst2pdf).
Collate:
Expand Down
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ examples:
cd inst/examples;\
Rscript knit-all.R

vignettes:
cd vignettes;\
lyx -e knitr knitr-refcard.lyx;\
sed -i '/\\usepackage{breakurl}/ d' knitr-refcard.Rnw;\
mv knitr-refcard.Rnw assets/template-refcard.tex

clean:
cd ..;\
$(RM) -r $(PKGNAME).Rcheck/
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ import(utils)
importFrom(xfun,attr)
importFrom(xfun,file_ext)
importFrom(xfun,file_string)
importFrom(xfun,html_escape)
importFrom(xfun,is_R_CMD_check)
importFrom(xfun,is_abs_path)
importFrom(xfun,is_windows)
Expand Down
27 changes: 26 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# CHANGES IN knitr VERSION 1.49

## NEW FEATURES

- In-chunk references of the form `<<label>>` can be disabled via the chunk option `ref.chunk = FALSE` now (thanks, @jennybc @gadenbuie, #2360).

- Added support for `fig.alt` for LaTeX output, i.e., using `\includegraphics[alt={alt text}]` (thanks, @capnrefsmmat, #2378).

## BUG FIXES

- In-chunk references of the form `<<label>>` should not be resolved if `label` is not found in the document (thanks, @jennybc @gadenbuie, #2360).

- `asis_output()` was not passed to the `output` hook (thanks, @cderv, #2332).

- Avoid partial matching of the `Date/Publication` field when generating `citation('knitr')`, otherwise R will emit a warning when `options(warnPartialMatchDollar = TRUE)` (thanks, @fkohrt, #2361).

## MAJOR CHANGES

- Unbalanced chunk delimiters (fences) in R Markdown documents are strictly prohibited now.

- For code chunks with `error = TRUE`, `purl()` and `hook_purl()` will wrap the code in `try({...})` (thanks, @bastistician #2338, @jeroen #2368).

## MINOR CHANGES

- Changed the format of the reference card from PDF to HTML so building this package will not require LaTeX. See `vignette('knitr-refcard', package = 'knitr')`.

- Switched the vignette engine from **knitr** to `litedown::vignette` for some package vignettes.

# CHANGES IN knitr VERSION 1.48

Expand Down Expand Up @@ -2165,7 +2190,7 @@

## DOCUMENTATION

- added a simple reference card: http://cran.rstudio.com/web/packages/knitr/vignettes/knitr-refcard.pdf
- added a simple reference card: `vignette('knitr-refcard', package = 'knitr')`

# CHANGES IN knitr VERSION 0.7

Expand Down
13 changes: 10 additions & 3 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ call_block = function(block) {

if (opts_knit$get('progress')) print_block(block)

params[['code']] = parse_chunk(params[['code']]) # parse sub-chunk references
if (!isFALSE(params$ref.chunk))
params[['code']] = parse_chunk(params[['code']]) # parse sub-chunk references

ohooks = opts_hooks$get()
for (opt in names(ohooks)) {
Expand Down Expand Up @@ -255,7 +256,7 @@ eng_r = function(options) {
res = if (is_blank(code)) list() else if (isFALSE(ev)) {
as.source(code)
} else if (cache.exists && isFALSE(options$cache.rebuild)) {
fix_evaluate(cache$output(options$hash, 'list'), options$cache == 1)
cache$output(options$hash, 'list')
} else in_input_dir(
evaluate(
code, envir = env, new_device = FALSE,
Expand Down Expand Up @@ -603,12 +604,18 @@ tangle_block = function(x) {
eval(parse_only(unlist(str_extract(code, 'read_chunk\\(([^)]+)\\)'))))
}
code = parse_chunk(code)
if (isFALSE(ev)) code = comment_out(code, params$comment, newline = FALSE)
code = tangle_mask(code, ev, x$params$error)
if (opts_knit$get('documentation') == 0L) return(one_string(code))
# e.g. when documentation 1 or 2 with purl()
label_code(code, x)
}

tangle_mask = function(code, eval, error) {
if (isFALSE(eval)) code = comment_out(code, '#', newline = FALSE)
if (isTRUE(error)) code = c('try({', code, '})')
code
}

tangle_inline = function(x) {
output = if (opts_knit$get('documentation') == 2L) {
output = paste("#'", gsub('\n', "\n#' ", x$input, fixed = TRUE))
Expand Down
2 changes: 1 addition & 1 deletion R/highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ hilight_source = function(x, format, options) {
} else {
res = try(highr::hi_andre(x, options$engine, format))
if (inherits(res, 'try-error')) {
if (format == 'html') highr:::escape_html(x) else highr:::escape_latex(x)
if (format == 'html') html_escape(x) else highr:::escape_latex(x)
} else {
highlight_header()
n = length(res)
Expand Down
4 changes: 3 additions & 1 deletion R/hooks-asciidoc.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ hooks_asciidoc = function() {
hook.error = function(x, options) {
sprintf('\n[CAUTION]\n====\n.Error\n%s\n====\n', gsub('^.*Error: ', '', x))
}
hook.output = function(x, options) sprintf('\n----\n%s----\n', x)
hook.output = function(x, options) {
if (output_asis(x, options)) x else sprintf('\n----\n%s----\n', x)
}
list(
source = hook.source, output = hook.output, message = hook.message,
warning = hook.warning, error = hook.error, plot = hook_plot_asciidoc
Expand Down
6 changes: 4 additions & 2 deletions R/hooks-extra.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ hook_purl = function(before, options, ...) {
.knitEnv$tangle.params = NULL
}

code = options$code
if (isFALSE(options$eval)) code = comment_out(code, '# ', newline = FALSE)
# `options` contains merged chunk options, but we need to check if
# `error=TRUE` in local chunk options, so retrieve options from knit_code
error = attr(knit_code$get(options$label), 'chunk_opts')[['error']]
code = tangle_mask(options$code, options$eval, error)
if (is.character(output)) {
code = c(
if (file.exists(output)) read_utf8(output),
Expand Down
5 changes: 3 additions & 2 deletions R/hooks-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ hook_animation = function(options) {
if (length(cap) == 0) cap = ''
if (alt) {
alt = options$fig.alt %n% cap
return(if (escape) escape_html(alt) else alt)
return(if (escape) html_escape(alt) else alt)
}
if (is_blank(cap)) return(cap)
paste0(create_label(
Expand Down Expand Up @@ -252,9 +252,10 @@ hooks_html = function() {
hook = function(name) {
force(name)
function(x, options) {
if (name == 'output' && output_asis(x, options)) return(x)
x = if (name == 'source') {
c(hilight_source(x, 'html', options), '')
} else escape_html(x)
} else html_escape(x)
x = one_string(x)
sprintf('<div class="%s"><pre class="knitr %s">%s</pre></div>\n', name, tolower(options$engine), x)
}
Expand Down
7 changes: 4 additions & 3 deletions R/hooks-latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ hook_plot_tex = function(x, options) {
# maxwidth does not work with animations
if (animate && identical(ow, '\\maxwidth')) ow = NULL
if (is.numeric(ow)) ow = paste0(ow, 'px')
size = paste(c(sprintf('width=%s', ow),
sprintf('height=%s', options$out.height),
options$out.extra), collapse = ',')
size = paste(c(
sprintf('width=%s', ow), sprintf('height=%s', options$out.height),
sprintf('alt={%s}', escape_percent(options$fig.alt)), options$out.extra
), collapse = ',')

paste0(
fig1, align1, sub1, resize1,
Expand Down
10 changes: 7 additions & 3 deletions R/hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ hooks_markdown = function(strict = FALSE, fence_char = '`') {
hook.o = function(class) {
force(class)
function(x, options) {
if (class == 'output' && output_asis(x, options)) return(x)
hook.t(x, options[[paste0('attr.', class)]], options[[paste0('class.', class)]])
}
}
Expand Down Expand Up @@ -277,19 +278,22 @@ hooks_jekyll = function(highlight = c('pygments', 'prettify', 'none'), extra = '
}, prettify = {
hook.r = function(x, options) {
paste0(
'\n\n<pre><code class="prettyprint ', extra, '">', escape_html(x),
'\n\n<pre><code class="prettyprint ', extra, '">', html_escape(x),
'</code></pre>\n\n'
)
}
hook.t = function(x, options) paste0(
'\n\n<pre><code>', escape_html(x), '</code></pre>\n\n'
'\n\n<pre><code>', html_escape(x), '</code></pre>\n\n'
)
})
source = function(x, options) {
x = one_string(hilight_source(x, 'markdown', options))
hook.r(x, options)
}
merge_list(hook.m, list(
source = source, output = hook.t, warning = hook.t, message = hook.t, error = hook.t
source = source, warning = hook.t, message = hook.t, error = hook.t,
output = function(x, options) {
if (output_asis(x, options)) x else hook.t(x, options)
}
))
}
4 changes: 3 additions & 1 deletion R/hooks-rst.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ hooks_rst = function(strict = FALSE) {
(if (strict) hook.s else hook.t)(x, options)
},
warning = hook.s, error = hook.s, message = hook.s,
output = hook.s, inline = hook.i, plot = hook_plot_rst
inline = hook.i, plot = hook_plot_rst, output = function(x, options) {
if (output_asis(x, options)) x else hook.s(x, options)
}
)
}

Expand Down
1 change: 1 addition & 0 deletions R/hooks-textile.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ hooks_textile = function() {
function(x, options) {
if (name == 'source') x = c(hilight_source(x, 'textile', options), '')
x = one_string(x)
if (name == 'output' && output_asis(x, options)) return(x)
sprintf('bc(knitr %s %s#%s).. %s\np(knitr_end). \n\n',
tolower(options$engine), name, options$label, x)
}
Expand Down
7 changes: 3 additions & 4 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ knit = function(
oconc = knit_concord$get(); on.exit(knit_concord$set(oconc), add = TRUE)

if (child_mode()) {
setwd(opts_knit$get('output.dir')) # always restore original working dir
setwd(opts_knit$get('output.dir') %n% '.') # always restore original working dir
# in child mode, input path needs to be adjusted
if (in.file && !is_abs_path(input)) {
input = paste0(opts_knit$get('child.path'), input)
Expand Down Expand Up @@ -512,8 +512,7 @@ sew.knit_asis = function(x, options, inline = FALSE, ...) {
}
}
x = wrap_asis(x, options)
if (!out_format('latex') || inline) return(x)
# latex output need the \end{kframe} trick
if (inline) return(x)
options$results = 'asis'
knit_hooks$get('output')(x, options)
}
Expand Down Expand Up @@ -646,7 +645,7 @@ sew.knit_embed_url = function(x, options = opts_chunk$get(), inline = FALSE, ...
if (length(extra <- options$out.extra)) extra = paste('', extra, collapse = '')
add_html_caption(options, sprintf(
'<iframe src="%s" width="%s" height="%s" data-external="1"%s></iframe>',
escape_html(x$url), options$out.width %n% '100%', x$height %n% '400px',
html_escape(x$url), options$out.width %n% '100%', x$height %n% '400px',
extra %n% ''
))
}
Expand Down
2 changes: 1 addition & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' \code{knit} + \code{R} (while \code{Sweave} = \code{S} + \code{weave}).
#' @references Full documentation and demos: \url{https://yihui.org/knitr/};
#' FAQ's: \url{https://yihui.org/knitr/faq/}
#' @importFrom xfun attr file_ext is_windows loadable parse_only
#' @importFrom xfun attr file_ext html_escape is_windows loadable parse_only
#' sans_ext try_silent with_ext read_utf8 write_utf8 file_string
#' is_R_CMD_check is_abs_path
'_PACKAGE'
Expand Down
12 changes: 5 additions & 7 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,13 @@ strip_white = function(x, test_strip = is_blank) {
parse_chunk = function(x, rc = knit_patterns$get('ref.chunk')) {
if (length(x) == 0L) return(x)
x = c(x) # drop attributes of code (e.g. chunk_opts)
if (!group_pattern(rc) || !any(idx <- grepl(rc, x))) return(x)
if (!group_pattern(rc) || length(idx <- grep(rc, x)) == 0) return(x)

labels = sub(rc, '\\1', x[idx])
code = knit_code$get(labels)
code = knit_code$get()
i = labels %in% names(code)
idx = idx[i]; code = code[labels[i]]
indent = gsub('^(\\s*).*', '\\1', x[idx])
if (length(labels) <= 1L) code = list(code)
code = mapply(indent_block, code, indent, SIMPLIFY = FALSE, USE.NAMES = FALSE)

x = as.list(x)
Expand Down Expand Up @@ -469,17 +470,14 @@ match_chunk_end = function(pattern, line, i, b, lines) {
if (!any(match_chunk_begin(pattern, lines[i + 1:(k - 1)], '^\\1`*\\\\{')))
return(FALSE)
}
# TODO: clean up the exceptions here (although perhaps some may never update again)
signal = if (getOption('knitr.unbalanced.chunk', FALSE)) warning2 else stop2
signal(
stop2(
'The closing fence on line ', i, ' ("', line, '") in ', current_input(),
' does not match the opening fence "',
gsub('\\^(\\s*`+).*', '\\1', pattern), '" on line ', b, '. You are recommended to ',
'fix either the opening or closing fence of the code chunk to use exactly ',
'the same numbers of backticks and same level of indentation (or blockquote). ',
'See https://yihui.org/en/2021/10/unbalanced-delimiters/ for more info.'
)
TRUE
}

#' Get all chunk labels in a document
Expand Down
32 changes: 0 additions & 32 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,38 +281,6 @@ reduce_plot_opts = function(options) {
options
}

# the memory address of a NativeSymbolInfo object will be lost if it is saved to
# disk; see http://markmail.org/message/zat2r2pfsvhrsfqz for the full
# discussion; the hack below was stolen (with permission) from RStudio:
# https://github.com/rstudio/rstudio/blob/master/src/cpp/r/R/Tools.R
fix_recordedPlot = function(plot) {
# restore native symbols for R >= 3.0
for (i in seq_along(plot[[1]])) {
# get the symbol then test if it's a native symbol
symbol = plot[[1]][[i]][[2]][[1]]
if (inherits(symbol, 'NativeSymbolInfo')) {
# determine the dll that the symbol lives in
name = symbol[[if (is.null(symbol$package)) 'dll' else 'package']][['name']]
pkgDLL = getLoadedDLLs()[[name]]
# reconstruct the native symbol and assign it into the plot
nativeSymbol = getNativeSymbolInfo(
name = symbol$name, PACKAGE = pkgDLL, withRegistrationInfo = TRUE
)
plot[[1]][[i]][[2]][[1]] <- nativeSymbol
}
}
attr(plot, 'pid') = Sys.getpid()
plot
}

# fix plots in evaluate() results
fix_evaluate = function(list, fix = TRUE) {
if (!fix) return(list)
lapply(list, function(x) {
if (evaluate::is.recordedplot(x)) fix_recordedPlot(x) else x
})
}

# remove the plots from the evaluate results for the case of cache=2; if we only
# want to keep high-level plots, we need MD5 digests of the plot components so
# that we will be able to filter out low-level changes later
Expand Down
Loading

0 comments on commit ce1dad8

Please sign in to comment.