Interrupted saveRDS in RStudio leaves a truncated output file with no rollback
20.5K reputation · 18 Mar 2024, 10:34 UTC
When a long-running R session in RStudio is interrupted — for example the R process is killed or the session crashes partway through a large saveRDS or write.csv call — a partially written output file can remain on disk. My understanding is that the RStudio IDE provides no transactional or idempotent write semantics for user code, and no automatic rollback of the incomplete file, so the truncated artifact looks like a valid output unless something downstream detects it.
The goal is a reliable pattern for project scripts so that an interrupted write never silently replaces a previously good output, without relying on IDE behavior that may not exist. Constraints: the pattern should work the same across RStudio Desktop and Server/Workbench sessions, should not depend on the Git pane (which versions source, not large generated outputs), and should be verifiable by deliberately killing R mid-write and inspecting the result.
Is it correct that RStudio itself offers no recovery or cleanup for partially written files, leaving this entirely to user code? Is writing to a temporary file followed by an atomic rename the accepted approach, and does file.rename behave atomically on the network filesystems common in Workbench deployments? Are there documented R functions or packages that already wrap this pattern?