rstudio / rmarkdown-cookbook

R Markdown Cookbook. A range of tips and tricks to make better use of R Markdown.
https://bookdown.org/yihui/rmarkdown-cookbook/
578 stars 224 forks source link

Issue with html, pdf2png, tikz, cache=TRUE... #396

Open JeffreyRacine opened 12 months ago

JeffreyRacine commented 12 months ago

Greetings!

I am trying out the option cache=TRUE to generate a tikz graphics file... all works as advertised as described in

https://bookdown.org/yihui/rmarkdown-cookbook/graphical-device.html https://bookdown.org/yihui/rmarkdown-cookbook/fig-process.html#fig-process

However, in the cookbook I read

"There are two major disadvantages of the tikz device. First [...] Second, it is often significantly slower to render the plots, because this device generates a LaTeX file and has to compile it to PDF. If you feel the code chunk is time-consuming, you may enable caching by the chunk option cache = TRUE."

The issue I am running into is that cache=TRUE does not appear to cache the plot and the code generating the figure gets processed with every run (quarto render foo.qmd, code below)... this seems to be at odds with my reading of the paragraph above?

Any suggestions most welcome, and thanks to you all for your incredible contributions to the community. Using latest version of R, RStudio, quarto etc. (session info at end of this report)

---
title: "Untitled"
format: html
---

```{r global,include=FALSE}
require(pdftools)
pdf2png <- function(path) {
  ## Only do the conversion for non-LaTeX output
  ## https://bookdown.org/yihui/rmarkdown-cookbook/fig-process.html#fig-process
  if (knitr::is_latex_output())
    return(path)
  path2 <- xfun::with_ext(path, "png")
  img <- magick::image_read_pdf(path)
  magick::image_write(img, path2, format = "png")
  path2
}
```

```{r plot,dev='tikz',fig.process=pdf2png,cache=TRUE}
hist(rnorm(1000),xlab="$X$")
```

Jeff

quarto --version
1.3.450

sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.5.2

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

time zone: America/Toronto
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_4.3.1