rr-markdown

R-markdown `self_contained: yes` generates a `_files` directory when some chunks have `cache = TRUE`


I want to use R markdown and knitr to generate a self-contained html output. I've tried using the various strategies described here (R-markdown self_contained):

However, I still get an xxx.html file and a xxx_files directory with a figure-html subdirectory. Notably, I can delete the images in figure-html without affecting the figures in xxx.html, but when I delete xxx_files, xxx.html is also deleted.

How do I generate just xxx.html without generating xxx_files? Thanks

EDIT: A reproducible example was requested. The nature of my code (huge input files), and the limits of my knowledge, make generating a reproducible example a bit difficult. However, this request was helpful, as trying to comply led me to the answer. Setting params:evaluate:cache: FALSE or removing cache = params$evaluate$cache fixed my problem. It appears having some code chunks with cache = TRUE caused the xxx_files directory to be produced. Thanks again for the help.

---
params:
  experiment_id: 'JT001'
  analysis_type: 'paper'
  evaluate:
    value:
      cache: TRUE
      go: FALSE
  tstamp_knit: !r format(Sys.time(), "%Y%m%d_%H%M%S")
  top_dir: !r file.path(getwd(), "knit_out")
title: '`r paste0(params$analysis_type, " analysis of ", params$experiment_id)`'
author: 'Josh'
date: '`r params$tstamp_knit`'
output: 
  html_document:
    code_folding: hide
    code_download: yes
    self_contained: TRUE
    mode: selfcontained
editor_options:
  chunk_output_type: console
knit: |
  (function(input, ...) {
    rmd_lines <- readLines(input)
    yaml_delim <- grep("---", rmd_lines)
    yaml_lines <- rmd_lines[yaml_delim[1] + 1:yaml_delim[2] - 1]
    yaml <- yaml::read_yaml(text = yaml_lines)
    
    output_file <- eval(parse(text = yaml$params$tstamp_knit))
    output_dir <- file.path(
      eval(parse(text = yaml$params$top_dir)), 
      yaml$params$experiment_id,
      yaml$params$analysis_type,
      paste(yaml$params$keep$value$treatment, collapse = "_")
    )
    
    rmarkdown::render(
      input,
      output_file = output_file,
      output_dir = output_dir, 
      envir = globalenv()
    )
  })
---

```{r plot-paired, results = "asis", cache = params$evaluate$cache}
## long code block with large inputs that outputs images     
```#

Solution

  • The issue was some of my code chunks that produced plots were set to cache = TRUE. When I removed that argument the xxx_html directory was no longer produced.