Seeking Best Practices for Avoiding R temp dir Connection Issues on Windows

Hi everyone,

I'm reaching out regarding an issue I've encountered when trying to open R temp dir on Windows, similar to the one described in this Stack Overflow thread: Launching R Help - Error in file(out, "wt") : cannot open the connection.

In file(open = "w+b", encoding = "UTF-8") :
'C:/r_temp\RtmpgnMv59\Rf1510c48c31b70': No such file or directory

The error seems related to R's inability to create or write to temporary files after a long-running session. For context:

  • This issue only occurs for me on Windows.
  • I do not experience the same problem on Ubuntu.
  • Reestablishing the R temp dir resolves the issue temporarily, but it’s not ideal for workflows with long-running R sessions, such as Plumber APIs or data pipelines.

I’m curious if others have encountered this and whether there are best practices for avoiding or mitigating it. Some questions I have:

  1. Are there settings or configurations for Windows that better manage temporary files for R? Especially when running multiple concurrent r processes.
  2. Would it help to periodically clean or reset the temporary directory within the session?
  3. Are there alternative approaches for handling long-running sessions where R Help might be used intermittently?

I would appreciate any insights or solutions others have implemented in similar scenarios.

Thank you!
Laurens

From what I can tell (e.g. 1, 2, and my own computer), Windows doesn't usually delete temporary files itself, so that suggests it's some other program that deletes. Also, I think the temporary directory C:\r_temp is unusual (e.g. for me it's %AppData%\Local\Temp), so you might have a customized configuration, you can check the environment variables TMPDIR, TMP, and TEMP to see if it was explicitly set.

One other solution is to manage the cache yourself. You can choose a cache directory, create your "temporary" files in there (making sure they have a unique name), and delete them when not needed anymore (e.g. with on.exit()). And if you do that, you might also want to set up some monitoring to get an alert if the cache gets too big, or if files have been there for too long.