Search code examples
rknitrpandocquarto

How to create a self-contained HTML file without the embeded files being lost?


When I render my Quarto script as a self-contained HTML file using embed-resources: true in the YAML, all embedded files (.bib and .png) are deleted. Embedding them in the HTML works fine, but since the original files are deleted, I can't render the Quarto Script correctly a second time. This only happens if I have R-Code chunks within the script (see example below).

Is this a bug or am I missing something here?

I am using R version 4.3.1 and RStudio 2023.09.0+463

This works fine, all files are included in a standalone HTML file and kept as files within the working directory:

---
bibliography: test_files/references.bib
format: 
  html:
    embed-resources: true 
---

Test @test

![TestPNG](test_files/images/test.PNG)

This does not work as intended. Files are included in a standalone HTML file but deleted in the working directory:

---
bibliography: test_files/references.bib
format: 
  html:
    embed-resources: true 
---
Test @test

![TestPNG](test_files/images/test.PNG)

```{r}
1+1
```

Solution

  • Files to be embedded may not be placed within a folder named "<script_name>_files". When rendering, Quarto will create a folder with this name and overwrite any folder named as such.

    To solve the given example where the Quarto script is named test.qmd, the folder containing the files to be embedded (.bib and .png files) would need to be renamed (e.g. "testfiles").

    ---
    bibliography: testfiles/references.bib
    format: 
      html:
        embed-resources: true 
    ---
    Test @test
    
    ![TestPNG](testfiles/images/test.PNG)
    
    ```{r}
    1+1
    ```                                                                              .
    
    

    All credits for this answer go to nrennie!