16.5 Keep the plot files

Most R Markdown output formats use the option self_contained = TRUE by default. This causes R plots to be embedded in the output documents, so we do not need the intermediate plot files when viewing the output documents. As a consequence, the plot folder (which typically has a suffix _files) will be deleted after the Rmd document is rendered.

Sometimes you may want to keep the plot files. For example, some academic journals require authors to submit figures files separately. For R Markdown, there are three ways to avoid the automatic deletion of these files:

  1. Use the option self_contained = FALSE if the output format supports this option, e.g.,

    output:
      html_document:
        self_contained: false

    However, this means the plot files will not be embedded in the output document. If this is not what you want, you may consider the next two methods.

  2. Enable caching for at least one code chunk (see Section 11.4). When caching is enabled, R Markdown will not delete the plot folder.

  3. Use the option keep_md = TRUE if the output format supports this option, e.g.,

    output:
      word_document:
        keep_md: true

    When you ask R Markdown to preserve the intermediate Markdown output file, it will also preserve the plot folder.