pkgdown icon indicating copy to clipboard operation
pkgdown copied to clipboard

Use svg graphics device for examples

Open ramiromagno opened this issue 2 years ago • 8 comments

Assuming I am using rmarkdown support, and that I have a code block in my documentation that generates a plot, is there a way of switching the device to SVG if the output is html?

My motivation is to have SVG plots in the website generated by pkgdown (looks better than the PNG counterpart), but still fallback to PNG when generating the PDF output.

I tried wrapping the R chunk in \if{html}{...} and \if{latex}{...} but that does not work (I guess obviously...).

ramiromagno avatar Feb 02 '22 14:02 ramiromagno

I am afraid you will need to show us a minimal example of what you are trying to do.

gaborcsardi avatar Feb 02 '22 14:02 gaborcsardi

Here's an example:

https://github.com/maialab/agvgd/blob/5dc9309b892ac9903b4f9e7ec59c1c4ac48c0923/R/dev.R#L1-L22

See lines 18 thru 21.

I would like the website documentation to render that plot as an SVG:

https://maialab.org/agvgd/reference/dev.html

I tried setting the dev = 'SVG in the rmarkdown chunk, and it works in the sense that the pkgdown page now uses SVG instead of a PNG. However, then I get the error below when knitting the PDF manual:

LaTeX Error: Unknown graphics extension: .svg

ramiromagno avatar Feb 02 '22 15:02 ramiromagno

This is a pkgdown issue, not an roxygen2 one, so I'll move it there.

hadley avatar Mar 29 '22 18:03 hadley

I just re-found this issue. :) I just want to say that my original issue was about plotting as SVG in sections other than the examples' section, and still have it work both in html and latex.

But actually now I have another case where it'd be really nice to make the plots in the examples' section to be rendered with SVG instead.

ramiromagno avatar Apr 21 '22 19:04 ramiromagno

:eyes:

cboettig avatar May 20 '22 19:05 cboettig

It would be very useful if someone would provide a minimal package that illustrates what you're trying to do. This will make it much more likely we look into this the next time we're working on pkgdown.

hadley avatar May 31 '22 21:05 hadley

@hadley I added this one to my example too, https://github.com/cboettig/pkgdowntest/blob/154d541bf77a615e800fba094b3b2b6f327f842d/vignettes/python.Rmd#L13

Note that setting dev="svg" like that avoids the error regarding ragg_png and allows pkgdown to render, but the resulting graphic renders as a missing image.

cboettig avatar May 31 '22 22:05 cboettig

Hi @hadley,

Here's a minimal package: https://github.com/ramiromagno/pkgdown.2067

There's only one function in that package, an example for you. Here's the pkgdown documentation: https://rmagno.eu/pkgdown.2067/reference/example.html. Notice how the plot is rendered as SVG.

Now, running devtools::build_manual() on that same package results in:

> devtools::build_manual()
Error: Failed to build manual

ramiromagno avatar May 31 '22 22:05 ramiromagno

@ramiromagno so it sounds svg is working for you in pkgdown? I don't know of a way to get around latex not supporting images (causing the manual to fail).

hadley avatar Apr 17 '24 22:04 hadley

Thanks for coming back on this! I guess then that having graphics as SVG in documentation pages is a no-go, right? I mean, for CRAN, the manual needs to be built.

ramiromagno avatar Apr 17 '24 22:04 ramiromagno

You can have SVGs in the HTML manual, just not in the LaTeX (=PDF) one. See e.g. the cli package that has many SVGs in the manual.

gaborcsardi avatar Apr 18 '24 06:04 gaborcsardi

Sounds like this mostly isn't a pkgdown issue, so I'm going to close.

hadley avatar Apr 18 '24 17:04 hadley

Hi @gaborcsardi: Thanks for the tip. Could you link an example help page from cli that is using SVGs?

ramiromagno avatar Apr 20 '24 01:04 ramiromagno

@ramiromagno https://github.com/search?q=repo%3Acran%2Fcli+path%3A%2F%5Eman%5C%2F%2F+svg+language%3AR&type=code

gaborcsardi avatar Apr 20 '24 04:04 gaborcsardi

Oh, thanks! So you don't show any graphics in PDF.

ramiromagno avatar Apr 20 '24 10:04 ramiromagno

Oh, thanks! So you don't show any graphics in PDF.

I don't, because I don't care about the PDF manual. But if you do, you can show a PNG or a PDF file there with

\if{latex}{\figure{get-spinner.png}}

See docs at https://cran.r-project.org/doc/manuals/R-exts.html#Figures-1

gaborcsardi avatar Apr 20 '24 10:04 gaborcsardi

Thank you @gaborcsardi. But there is no way that that graphics may come from the computation of an R chunk instead of a file, is there? Because this was actually my question initially, if you look at my first post.

In other words, there is no way that this can work gracefully both in html and pdf:

https://github.com/ramiromagno/pkgdown.2067/blob/ea202b06d48b7be91c35d160ae8d975a3261f30c/R/example.R#L1-L13

ramiromagno avatar Apr 20 '24 10:04 ramiromagno

That code is coming from an R code chunk, an R code chunk can generate any text (+ graphics files) with the right knitr options.

You can look at the roxygen chunks that generate it, although cli is not the best example, because it is more complicated than what you need.

gaborcsardi avatar Apr 20 '24 10:04 gaborcsardi

Thank you so much. I see your point!

BTW: I saw that you use Quarto-styled knitr options...? Is it? The special comments #|? Can that be used in roxygen2 now?

https://github.com/r-lib/cli/blob/bcb5c78a20122d62b9c84a0bfbf09900fe928036/R/spinner.R#L9C8-L23

ramiromagno avatar Apr 20 '24 10:04 ramiromagno

Can that be used in roxygen2 now?

Seems like.

Re. multiple output formats for graphs, knitr supports that natively, see e.g. https://bookdown.org/yihui/rmarkdown-cookbook/dev-vector.html and then I believe you need a hook to create the the Rd commands that use the SVG in html and the PDF in latex.

gaborcsardi avatar Apr 20 '24 10:04 gaborcsardi