Pkgdown: code externalisation errors

Created on 8 Oct 2019  ·  2Comments  ·  Source: r-lib/pkgdown

I'm running into problems when a vignette uses knitr's code externalisation, i.e the chunks are defined in an external R file. Consider this minimal example,

````

title: "Test: problem"

```{r include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

This vignette tests pkgdown output rendering for several problematic use cases.


![](bacon.jpg)

```{r nobacon}
knitr::include_graphics('bacon.jpg')

```{r externalise}
knitr::read_chunk("run.R")

```{r gg}

````

where run.R contains

````

----gg----

library(ggplot2)
qplot(1,1)
````

I'm getting the following error:

── Building articles ───────────────────────────────────────────────────────────
Reading 'vignettes/test/problem.Rmd'
Writing 'articles/test/problem.html'
Quitting from lines 20-21 (/var/folders/s1/d92rwnjd6y98_0xn7cxdl4p00000gn/T> //RtmpsnK6eg/file2684860d6c4.md)
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") : cannot open file 'run.R': No such file or directory
Error: callr subprocess failed: cannot open the connection
Execution halted

Curiously though, the output is just fine, so clearly the file was found and the code run.

(As a side-note, the bacon picture results in a broken link if included via knitr::include_graphics('bacon.jpg') and not with the md syntax. My current workaround is to use grid.raster(readJPG('bacon.jpg')) instead so not as problematic as the first issue (for which I have no good workaround).

Most helpful comment

I've found a workaround but it is very puzzling. Replacing
read_chunk(path = "_run.R") by
do.call(read_chunk, list(path = "_run.R"))

and for the other issue,

include_graphics(path = 'auto.png') by
do.call(include_graphics, list(path = 'auto.png'))

produces the desired results with no error. Most curiously though, having the previous code commented out within a chunk still produces errors. I am guessing pkgdown currently uses a regex of sorts to change file paths on a set of functions, and my use case happens to break the assumptions.

All 2 comments

I've found a workaround but it is very puzzling. Replacing
read_chunk(path = "_run.R") by
do.call(read_chunk, list(path = "_run.R"))

and for the other issue,

include_graphics(path = 'auto.png') by
do.call(include_graphics, list(path = 'auto.png'))

produces the desired results with no error. Most curiously though, having the previous code commented out within a chunk still produces errors. I am guessing pkgdown currently uses a regex of sorts to change file paths on a set of functions, and my use case happens to break the assumptions.

We have to jump through a lot of hoops to get vignettes rendered into pkgdown's template. The code is complicated and fragile, which unfortunately means I'm only willing to touch it for very common vignette use cases.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maelle picture maelle  ·  5Comments

rickhelmus picture rickhelmus  ·  3Comments

thierrygosselin picture thierrygosselin  ·  9Comments

cderv picture cderv  ·  9Comments

chuxinyuan picture chuxinyuan  ·  6Comments