Skip to content

Commit

Permalink
fix #1766: consider the case of multiple devices per chunk when calcu…
Browse files Browse the repository at this point in the history
…lating all_figs() (need to iterate over devices, instead of assuming a single device)
  • Loading branch information
yihui committed Jan 15, 2020
1 parent d846ee7 commit 260b3ff
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

- When the chunk option `dev = 'tikz'` and a plot in the code chunk is generated by **ggplot2** with a legend on a continuous scale, **tikzDevice** will create a raster image for the legend. Now the path to the raster image in the `.tex` file is tweaked to include the dir name of the `.tex` file, so that the `.tex` file can be correctly compiled to PDF via LaTeX (thanks to @rstub for the debugging at https://stackoverflow.com/a/58410965/559676).

- `hook_pdfcrop()` does not crop all plots when multiple graphical devices are used in a code chunk (i.e., the chunk option `dev` takes a vector of devices) due to a bug in the internal function `all_figs()` identified by @bastistician in #1766, who also proposed a fix.

## MAJOR CHANGES

- `options('width')` no longer affects caching. With this change, your previous cache will be invalidated if you update **knitr** from 1.26 to 1.27. If you prefer that changes in the R global option `width` invalidate cache (as in previous versions of **knitr**), you may associate it with a chunk option, e.g., `cache.extra = getOption('width')`. The reason for this change is that it is too costly to invalidate the cache when the `width` option changes---the effect of this option is only cosmetic and the code chunk output may not really rely on this option (thanks, @jaburgoyne, #1781).
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ merge_list = function(x, y) {

# paths of all figures
all_figs = function(options, ext = options$fig.ext, num = options$fig.num) {
fig_path(ext, options, number = seq_len(num))
unlist(lapply(ext, fig_path, options = options, number = seq_len(num)))
}

# evaluate an expression in a diretory and restore wd after that
Expand Down
10 changes: 10 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ assert(
identical(fig_chunk('foo', '.pdf'), 'figure/foo-1.pdf')
)

assert('all_figs() generates all figure paths for a code chunk', {
opts = list(fig.path = 'abc/', label = 'foo', fig.num = 3)
(all_figs(opts, '.svg') %==% sprintf('abc/foo-%d.svg', 1:3))
(all_figs(opts, c('png', 'pdf')) %==% apply(
expand.grid(1:3, c('.png', '.pdf')), 1, function(x) {
paste0(c('abc/foo-', x), collapse = '')
}
))
})

f = file.path(R.home('doc'), 'html', 'logo.jpg')
assert(
'base64_encode() gets the same result as markdown:::.b64EncodeFile',
Expand Down

0 comments on commit 260b3ff

Please sign in to comment.