Repository navigation
Description
I was trying to figure out why I ended up with a fname_files directory, despite using self_contained: yes in my document. That led me to this issue which suggested this should be fixed, but I was still experiencing this, and I traced it to cache=T.
Here's a test Rmd.
With the ggplot chunk as-is, all is well. If I add cache=T to the options, I get a test_files directory which is not removed after rendering. The file really is self-contained. I can move it (after renaming and bypassing the annoying "this file will no longer be owned by the directory test_files" message) and open it fine, and the page source shows the png image embedded.
Apologies if this is known/expected; I'm pretty new to Rstudio/knitr and am not very familiar with caching behavior.
Update pre-submit: aaannnd like most things, after I write everything up, I realized I missed something. I'm going to submit anyway; if nothing else it might resolve someone else's confusion/curiosity down the road.
In this comment, I noted the condition ... & !dir_exists(cache_dir). Does this mean that even with standalone documents, if one is using cache=T, one should always expect the linked _files directory?
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042), RStudio 2022.7.1.554
Locale:
LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
LC_TIME=English_United States.utf8
Package version:
base64enc_0.1.3 bslib_0.4.0 cachem_1.0.6 digest_0.6.29 evaluate_0.16 fastmap_1.1.0 fs_1.5.2
glue_1.6.2 graphics_4.2.1 grDevices_4.2.1 highr_0.9 htmltools_0.5.3 jquerylib_0.1.4 jsonlite_1.8.0
knitr_1.40 magrittr_2.0.3 memoise_2.0.1 methods_4.2.1 R6_2.5.1 rappdirs_0.3.3 rlang_1.0.5
rmarkdown_2.16 sass_0.4.2 stats_4.2.1 stringi_1.7.8 stringr_1.4.1 tinytex_0.41 tools_4.2.1
utils_4.2.1 xfun_0.32 yaml_2.3.5
Pandoc version: 2.18
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsTo discuss / To plan
Activity
cderv commentedon Sep 5, 2022
Thanks for the report.
Yes when
cache = TRUE, it means for plots that the code chunk won't be reevaluated, and plot re saved to file. It means that the file where plot was saved the first time will be reused. As those files are written in the figure dir, the path to the figure is saved.What is saved from the plot chunk is in fact the all generated HTML like this
"<img src=\"test_files/figure-html/unnamed-chunk-2-1.png\" width=\"672\" />"This is because knitr options allows to tweak some HTML attributes, so it needs to be saved.
This is why we need to keep the
_filesfolder.self_contained = TRUEwill have an effect only after the knitr steps, when Pandoc will convert to HTML. This is were the files is encoded so that the HTML only depends on itself. The pandoc processing is not part of the cache, so we don't save the encoded plot, only the file in the knitr step.I hope this helps understand the behavior. I can understand the confusion though.
notes: for more advanced users, cache setting can be controlled so that file is not saved and plot results in R is saved only, so that plot is rewritten to file - however, we don't recommend that. More about cache: https://yihui.org/knitr/demo/cache/, https://bookdown.org/yihui/rmarkdown-cookbook/cache.html
Idea @yihui if we want to adapt this:
knitr could save also the file in the cache
_cachefolder and copy it in figure dir during the knit when cache is used. This way, ifself_contained = TRUE, figure dir would be removed and only the_cachedirectory would need to be saved.Currently, if someone wants to save the cache file (on CI for example, it will require to save the
*_cachedir AND the_filesdir. Not that complex but could be confusing.Was it something you tried in the past ?
Full example to reproduce with above file
yihui commentedon Sep 6, 2022
I haven't tried that idea before, but it sounds like a good idea. Users can do it by themselves, though, by setting
knitr::opts_chunk$set(fig.path = knitr::opts_chunk$get('cache.path')inside the Rmd document.You have explained the problem clearly, and I don't have anything to add. In this case, the
*_filesdirectory is indeed not needed anymore forself_contained = TRUE, only if the*.htmloutput file doesn't need to be regenerated in future. If we delete the*_filesdirectory and regenerate*.html, we will run into an error (plot files not found).jwhendy commentedon Sep 6, 2022
I tried... though I admit the nuances between what happens with
cache=Tandstandaloneare not so clear. That said, I don't necessarily need to understand :)The practical issue I found bothersome was the whole "this file is owned by this directory." I ran into this as I wanted to upload my output .html to my team site and Windows brought the directory along with it. If we could break that link, the need to copy files is covered, and I could just delete the
_filesdirectories when I happened to see them (if I wanted).If there's some additional improvement, I'm of course also all for it.
[-]_filed directory not removed after rendering if cache=T[/-][+]_files directory not removed after rendering if cache=T[/+]jwhendy commentedon Sep 13, 2022
Another nuance to this I wanted to bring up. I was using this trick so that I could maintain a
notebooksdirectory separate from my generated output. In my header, I have:Even with no caching, this results in a persisting
_filesdirectory. Not sure it's trivial to pass a different output directory around to the internals for cleanup, but wanted to mention it. My current idea for a workflow is not working due to this, as my hope was to keep my notebooks directory clean, but either (a) it gets littered with the output files and I have to move them manually or (b) my output directory has these htmls with inherited ownership I can't share easily as they want to bring the directory with them.allefeld commentedon Jun 23, 2024
@cderv, sorry for picking up this old thread, but I ran into the same issue and I am still confused after your explanation.
I understand that if the output of a cached chunk contains a reference to an external file, that file needs to be preserved. I would prefer it if it were preserved in the
_cachedirectory, but okay.However, I have the case that I have two subsequent R chunks in a qmd file. The first runs a simulation and is cached. The second creates a plot based on data generated in the simulation. The cached chunk does not create a plot, and the chunk creating the plot is not cached. However, I still get a
_filesdirectory despiteembed-resources: true.I don't see the need to preserve this image file, since it is created by code which runs always (based on data which is either generated or read from the cache).
cderv commentedon Jun 24, 2024
@allefeld this could be a Quarto issue and not a Rmarkdown issue. Can you share example with Quarto ?
Or did you reproduce also without Quarto and .Rmd file only ?
I am mentioning this, because Quarto is also handling intermediary folder deletion, and can take over R Markdown / Knitr in some cases. We would need to check this.
allefeld commentedon Jun 24, 2024
I encountered this problem with Quarto, then searched for a solution and found this issue. Example, file
cacheplot.qmd:Processing and result:
But the same happens with R Markdown; file
cacheplot.Rmdwith the same contents ascacheplot.qmd. Knitting from within RStudio prints to the Render tab:Files left:
I'd be happy to create an issue on
quarto-cliif that is more useful.Debian 12.5, Quarto 1.5.37, R Studio 2023.12.0 Build 369, R 4.4.1, knitr 1.47, rmarkdown 2.27
cderv commentedon Jun 25, 2024
So this is definitely related. Looking at the code, we do not clean the figure dir when a cache directory is existing.
rmarkdown/R/render.R
Lines 824 to 838 in 4b2f342
So this would be the part to improved. Probably based on some knitr information is the supporting directory can be deleted.
[-]_files directory not removed after rendering if cache=T[/-][+]_files directory not removed when cache is active for knitr[/+]AlbertoFabbri93 commentedon Jul 25, 2025
Should I open a new issue on the knitr repository for this?
mcaselli commentedon Apr 16, 2026
I'd like to give this issue a bump, and add a little extra info on impact/severity that i didn't see here yet: this _files directory is linked to (owned by? I'm not sure of the underlying architecture/terminology) the HTML file, so if a user deletes the
_filesdirectory, the HTML file gets deleted too. So it's not simply that there's a superfluous directory being left behind, that folder is forever tied to the report, which is no longer "self_contained" or "embed-resources"-ed.yihui commentedon Apr 16, 2026
@mcaselli You are right. In retrospect, I regret that we used
*_filesfrom the beginning. Originally we thought it was a good idea thatfoo.htmlandfoo_files/are automatically linked on Windows (if people move one, the other one can be moved automatically). Later I started hating this Windows behavior. It's a terrible idea that deleting one file/dir could delete another file/dir automatically. For that reason, when I reimagined R Markdown in 2024 as the litedown package, I intentionally changed the one underscore to two by default, i.e., using*__files/, to break this automatic link.Back to the original issue, I'm not sure what we should really do on our side. A conservative approach would be to provide an option that users can set explicitly to delete
*_files/, i.e., "I understand what I'm doing; just delete it".mcaselli commentedon Apr 17, 2026
Given the expertise you guys have here (thanks @yihui and @cderv for all your incredible work over the years), it's with great humility that I offer this observation and suggestion--it's probably already occurred to you and is not viable for some reason, but here it goes: In @allefeld 's great example (which may admittedly be an edge case), the cached chunk isn't generating a plot, and the plot chunk isn't cached.
Two possible solutions occur to me:
The narrower fix
If the
_filesdirectory doesn't contain any figures, delete it, even if there's a_cachedirectory presentI think this is safe, but it'd still leave behind
_filesin many (most?) use cases.The broader fix
Do some copying and path translation when caching and retrieving from cache so that the cached content is pseudo-self-contained within the
_cachedirectory for cache purposes, but it's retrieved to the_filesworld at build time, so it behaves just like non-cached content at build time.When caching a code chunk that produces a relative path reference, use a
_cachepath for the reference, not a_filespath.When building the document, retrieve the cached output and their references, copy to
_files, and translate the paths from_cachereferences to_filesreferences.I think this means that cleaning up intermediates no longer needs to consider whether caching is happening (as long as it doesn't delete the
_cachedirectory, of course).I think this would support caching of even figure-producing chunks while still supporting a self-contained output, but it's admittedly much more convoluted, and my understanding of the present internals is really limited.
Thanks for your consideration!
yihui commentedon Apr 18, 2026
@mcaselli The narrow fix is certainly doable, and the broader fix sounds like a good idea, too. Actually I've used both ideas in bookdown before (not related to this problem, though).