2

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  
Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • 1
    I copied the code for the vignette verbatim. The plot was rendered and was saved. Are you looking for the saved file in the same folder location as where the RMD is saved? Even though I know this, sometimes I forget that RMD ignores the set directory. – Kat Apr 09 '22 at 14:32
  • Thank you Kat for testing. I was afraid that might be the case. I just re-ran buildVignette and happened to have the directory window visible and I noticed that testFig.html is created and then deleted. This is bizarre to say the least, but it rules out any of the R packages being the problem (I think). OK, just determined that buildVignette("Vignette.Rmd", clean = FALSE) works fine. 1/2 – Bryan Hanson Apr 09 '22 at 18:01
  • This problem was originally observed when doing R CMD build/check/install from a make file. I am going to see if I can find a recent change in the defaults for these functions. 2/2 – Bryan Hanson Apr 09 '22 at 18:03
  • Check permissions for the folder. Does R or RStudio have permission to save files there? – Kat Apr 09 '22 at 18:30
  • I'm using plain old R. Folder permissions are drwxr-xr-x@ which I think means no writing. Did chmod 777 and I can now do buildVignette(..., clean = TRUE) and the files stay put, everything works. So I guess the problem is upstream somewhere. I'm not sure even where to look. – Bryan Hanson Apr 09 '22 at 18:59
  • Upstream meaning when doing R CMD build etc from a make file, the needed file gets deleted. If this is because the permissions are wrong, something changed in R, knitr, or ? I guess. – Bryan Hanson Apr 09 '22 at 19:00
  • I tentatively believe this may be related to changes around 19 Feb to .../r-source/src/library/tools/R/Vignettes.R, specifically commit eed23bae244b17b9366bf7bf6863f6f51f17064d where the cleaning process was modified to fix one problem and may have caused this one. Still studying though. – Bryan Hanson Apr 10 '22 at 03:11
  • With the current R version (sessionInfo above) if one puts the testFig.html file in a folder then the files are not removed, they are "protected" from the current clean process *when running buildVignette*. E.g. create Vignettes/graphics and change the 2 paths in Vignette.Rmd to graphics/testFig.html and everything works correctly. However, if one puts all this in minimal package and runs R CMD build/check on this, one gets an error that the folder graphics doesn't exist. I think strictly speaking the original question has been answered, but there's still a problem... – Bryan Hanson Apr 10 '22 at 04:23

1 Answers1

0

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.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78