Search code examples
rplotlyr-markdownknitrhtmlwidgets

saveWidget works interactively, but no file saved when using buildVignette


When I run the following code interactively, the expected testFig.html is produced and functions correctly.

suppressPackageStartupMessages(library("plotly"))
suppressPackageStartupMessages(library("htmlwidgets"))

  DF <- data.frame(
  x <- rnorm(50),
  y <- rnorm(50),
  z <- rnorm(50))

  fig <- plot_ly(
    name = "data", DF, x = ~x, y = ~y, z = ~z,
    marker = list(size = 2.0)) %>% add_markers()
  saveWidget(partial_bundle(fig), "testFig.html", knitrOptions = list(results = "show"))

However, if the above code is placed in a vignette, and buildVignette is used, testFig.html is not produced and hence there is a placeholder in the vignette saying file not found. No errors or warnings are emitted. The vignette code is appended below. Save it as Vignette.Rmd and then do buildVignette("Vignette.Rmd") to see the result.

This is a minimal example of a larger project that worked fine about 5 weeks ago but has recently started acting as described above. I've checked against older versions of R and the packages in use and get the same behavior. As a result I am totally stumped.

---
title:  "Test Vignette"
date:  "`r Sys.Date()`"
output:
    bookdown::html_document2:
      toc: yes
      toc_depth: 2
      fig_caption: yes
      number_sections: false
vignette: >
    %\VignetteIndexEntry{Vignette}
    %\VignetteEngine{knitr::rmarkdown}
    %\VignetteEncoding{UTF-8}
pkgdown:
  as_is: true
---

```{r SetUp, echo = FALSE, eval = TRUE, results = "hide"}
# R options & configuration:
set.seed(13)
suppressPackageStartupMessages(library("knitr"))
suppressPackageStartupMessages(library("plotly"))
suppressPackageStartupMessages(library("htmlwidgets"))

# Stuff specifically for knitr:
opts_chunk$set(eval = TRUE, echo = FALSE)
```

**Test Vignette**

Trying to troubleshoot a problem with interactive diagrams not being included.

```{r testFig, results = "show"}

if (!is_latex_output()) {
  DF <- data.frame(
  x <- rnorm(50),
  y <- rnorm(50),
  z <- rnorm(50))

  fig <- plot_ly(
    name = "data", DF, x = ~x, y = ~y, z = ~z,
    marker = list(size = 2.0)) %>%
    add_markers()
  saveWidget(partial_bundle(fig), "testFig.html", knitrOptions = list(results = "show"))
  include_url("testFig.html")
}
> sessionInfo()
R version 4.2.0 alpha (2022-04-05 r82100)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.3.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

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

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

other attached packages:
[1] htmlwidgets_1.5.4 plotly_4.10.0     ggplot2_3.3.5     knitr_1.38       

loaded via a namespace (and not attached):
 [1] magrittr_2.0.3    tidyselect_1.1.2  munsell_0.5.0     viridisLite_0.4.0 colorspace_2.0-3 
 [6] R6_2.5.1          rlang_1.0.2       fastmap_1.1.0     fansi_1.0.3       httr_1.4.2       
[11] dplyr_1.0.8       tools_4.2.0       grid_4.2.0        data.table_1.14.2 gtable_0.3.0     
[16] xfun_0.30         utf8_1.2.2        cli_3.2.0         withr_2.5.0       htmltools_0.5.2  
[21] ellipsis_0.3.2    lazyeval_0.2.2    digest_0.6.29     tibble_3.1.6      lifecycle_1.0.1  
[26] crayon_1.5.1      tidyr_1.2.0       purrr_0.3.4       vctrs_0.4.0       glue_1.6.2       
[31] compiler_4.2.0    pillar_1.7.0      generics_0.1.2    scales_1.1.1      jsonlite_1.8.0   
[36] pkgconfig_2.0.3  

Solution

  • As noted in the comments to the question, the solution to the question asked is to put the file produced in a folder in the vignette directory. This protects the necessary files from being deleted when using buildVignette. However, this approach does not work when building and checking a package. I will ask a separate question on that.