2

I'm having trouble building a package that uses plotly figures as htmlwidgets. It appears that the supporting files containing the figures and related resources are either never created or are deleted at some point in the build/check process.

I have asked a related question here which used buildVignette to create just the vignette. While that question was resolved, using the solution there does not work when doing a full build and check.

I have created a minimal package for testing on Github. If one builds/checks/installs and then looks at the vignette, one sees:

enter image description here

and indeed none of that directory structure or files are present in the built package.

The odd thing is this worked fine around the beginning of February in the real context (i.e. not a test package, the documentation shows the working widgets here) suggesting something has changed, but I cannot figure out what changed.

I have looked carefully at combinations of recent versions of the dependent packages and that does not seem to be the problem. I have also looked at recent changes in r-source files build.R and check.R. The only relevant change is in this commit:

commit eed23bae244b17b9366bf7bf6863f6f51f17064d Author: smeyer <smeyer@00db46b3-68df-0310-9c12-caf00c1e9a41> Date: Sat Feb 19 01:10:34 2022 +0000

'R CMD Sweave --clean' no longer removes existing files (PR#18242)

but I don't think this is the problem. The build/check code is complex and I cannot figure out what is happening to these files.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • When I change output to `rmarkdown::html_vignette`, deleting the `htmlwidgets` part the vignette builds and package passed checks ([repo](https://github.com/D-Se/tstPkg)). However, the resulting vignette tar is ~ 1.6MB. Is using `htmlwidgets` necessary? – Donald Seinen Apr 14 '22 at 02:23
  • Your solution works @DonaldSeinen ! It is not necessary to use `htmlwidgets` that's just a method I found -- sounds like it was not even needed. I (honestly) forget why I was using `bookdown` but I think I brought it over from another project where it was needed for reasons forgotten. Please post this as an answer. – Bryan Hanson Apr 14 '22 at 02:42
  • Actually, one can stick with `bookdown` and just eliminate the widget stuff and it works, and has the desired css etc styling. – Bryan Hanson Apr 14 '22 at 03:42

1 Answers1

2

To embed interactive graphics in a vignette we could build it using output rmarkdown::html_vignette, a lightweight version of html_document.

---
title:  "Test Vignette"
output:
  rmarkdown::html_vignette:
      toc: yes
vignette: >
    %\VignetteIndexEntry{Vignette}
    %\VignetteEngine{knitr::rmarkdown}
    %\VignetteEncoding{UTF-8}
---
```{r SetUp, echo = FALSE, eval = TRUE, results = "hide"}
set.seed(13)
suppressPackageStartupMessages(library("knitr"))
suppressPackageStartupMessages(library("plotly"))
opts_chunk$set(eval = TRUE, echo = FALSE)
```

**Test Vignette**
Problem solved?

```{r testFig, results = "show"}
if (!is_latex_output()) {
  DF <- data.frame(
  x <- rnorm(50),
  y <- rnorm(50),
  z <- rnorm(50))
  plot_ly(
    name = "data", DF, x = ~x, y = ~y, z = ~z,
    marker = list(size = 2.0)) %>%
    add_markers()
}
```

html_vignette presets are slightly different from html_document. Because of this the graphic can appear quite small, some manual adjustment might be needed. Other stylistic changes can be made using CSS. The default file can be found using

system.file("rmarkdown", "templates", "html_vignette", "resources", "vignette.css",  package = "rmarkdown")

While it passes R CMD checks, it may hurt some feelings if submitted to CRAN:

neither data nor documentation should exceed 5MB

Donald Seinen
  • 4,179
  • 5
  • 15
  • 40
  • Thank you. For CRAN I submit the pdf version, html goes to Github. And per my comment above I can still use `bookdown`. I don't know why my original approach worked 5 weeks ago, but you found a workaround so I'm appreciative! – Bryan Hanson Apr 14 '22 at 04:13
  • @BryanHanson sadly a workaround != solution. I have been trying to find what changed in the last few months but have not found anything of substance yet. The latest commit to `learnPCA` may have messed up the pkgdown page, I see blank spots where the graphics were before – Donald Seinen Apr 14 '22 at 05:40
  • Yes, I just updated that prob. right before you checked it. So it builds fine locally as `html` using `plotly` simply by skipping the `htmlwidget` stuff. But that does not play nice with `pkgdown` on Github pages, leaving empty spaces you see (before it had place holders about can't find the html files). That's what I have to work on next; you have answered the immediate question I posted. – Bryan Hanson Apr 14 '22 at 06:30
  • After a little more experimentation`bookdown::html_document2`, `rmarkdown::html_vignette` or `rmarkdown::html_document` all produce a `pkgdown` site w/ empty spaces where the figures should be, no errors. So likely the problem is in how the widget is "saved". Current approach that does not work does not explicitly save, we just create and print the figure (which works fine locally). – Bryan Hanson Apr 14 '22 at 15:46
  • Another clue: the code for the widget is in the web page, but the widget is not displayed. It looks like the supporting libraries like `crosstalk` are not referenced in the document; maybe this is why the widget is not displayed. – Bryan Hanson Apr 15 '22 at 03:18
  • Replaced `.github/workflows/pgkdown.yaml` with the most current version (prev. was very stale). No change. Branch `gh-pages` has the supporting js libraries. – Bryan Hanson Apr 15 '22 at 03:56
  • from `?pkgdown::build_articles` *articles behave slightly differently to vignettes, particularly with respect to external files, and custom output formats. {...} Additionally, htmlwidgets do not work when as_is: true.* When I create a pkgdown page using the minimal example, using `pkgdown::init_site()` followed by `build_articles()` the graph renders fine – Donald Seinen Apr 15 '22 at 04:58
  • After forking `learnPCA`, simply removing the `pkgdown: as_is: TRUE` from the *Visualizing PCA in 3D* vignette renders the graphs. I don't think anything else broke on the page – Donald Seinen Apr 15 '22 at 05:10
  • And there it is! Great find @Donald! I have no idea why it worked before, or why it stopped working, but it works now, thanks to your sleuthing. I had read that part about "behave slightly differently" but can you post a link to the `as_is` note? I did not see that in spite of extensive searching. – Bryan Hanson Apr 15 '22 at 12:43
  • @BryanHanson it is the last line of the `?build_article` help file Output formats section, [link](https://github.com/r-lib/pkgdown/blob/main/man/build_articles.Rd#L180) – Donald Seinen Apr 15 '22 at 12:53
  • Well done, and much appreciated! – Bryan Hanson Apr 15 '22 at 13:03