8

I created a R shiny app that automatically runs every day using a batch file. Everything works fine when lauching the app, but the next day it crashes and I get the following message:

Warning in file(open = "w+") :
  cannot open file
'C:\Users\bertin\AppData\Local\Temp\RtmpKiBPOU\Rf3f835d1a66' : No such file or directory
Warning: Error in file: cannot open the connection
  [No stack trace available]

Actually this issue is related to the tempdir() folder created by the R session executing the shiny app. This folder is automatically deleted after a certain time. Do I have to delete all Temp files on each refreshing? Or on the contrary is it needed to prevent R from deleting all shiny temp files on Temp folder? Thanks!


Edit - Here is how to intentionally generate the error:

tempdir()
dir.exists(tempdir())

library(shiny)

# Windows shell required
shinyApp(
    ui = fluidPage("Please reload to see me fail."),
    server = function(input, output) {
        shell(paste("rmdir", dQuote(
            normalizePath(tempdir(), winslash = "/", mustWork = FALSE), q = FALSE
        ), "/s /q"))
    }
)

result

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
JeanBertin
  • 633
  • 1
  • 7
  • 23
  • 4
    Anything created with `tempdir()` or `tempfile()` goes away after the R process associated with them goes away. If you are using those and expecting long-term storage you're going to be disappointed. If there are things you're storing in a "temp" dir that needs semi-permanance, you'll need to create a deliberate path to store the files and use that from now on and do the cleanup management on your own. – hrbrmstr Nov 05 '18 at 10:42
  • Thanks for your answer but I haven't created anything on tempdir() or tempfile(): this is automatically created by R when lauching the shiny app and is automatically deleted by the system after a certain time... – JeanBertin Nov 05 '18 at 10:52
  • 1
    @hrbrmstr actually it is `shiny` itself storing files in `tempdir()`. Here is a related [GitHub issue](https://github.com/rstudio/shiny/issues/2421). I'm having the same [problem](https://community.rstudio.com/t/shiny-app-unresponsive-after-r-sessions-tempdir-is-deleted/36507). – ismirsehregal Aug 01 '19 at 06:11
  • 1
    Added a gif [here](https://community.rstudio.com/uploads/default/original/2X/8/822e23c1a4e1b2b24820acb13f60b9d1c9bf55f6.gif) showing how to reproduce the problem. – ismirsehregal Aug 01 '19 at 09:50
  • 1
    Nice gif for reproducibility! From your github comment one can infer you are also looking for workarounds? The only thing i could think of (without including the shiny/rstudio team) would be the following: 1) Upon start of the shiny app call a function that makes a copy of the `tempdir()`, called `paste0(tempdir(), "copy")` 2) Also make a check upon starting shiny app if tempdir exists and if not change the tempdir to the copy path https://stackoverflow.com/questions/17107206/change-temporary-directory. Then make another copy of the tempdir for the next time. (Copying the files failed for me.) – Tonio Liebrand Aug 01 '19 at 14:35
  • Hmm, drawback would be that it would loose all updates to tempdir after tempdir was copied. So it would be more of a backup in case the tempdir will be "completely" deleted – Tonio Liebrand Aug 01 '19 at 16:07
  • @BigDataScientist thanks for sharing your thoughts on this! I was also thinking about a backup-approach (but it seemed to be quite some effort regarding the alleged "simple" problem). Another idea I had was catching the error and start a new R session. – ismirsehregal Aug 02 '19 at 06:45
  • How about paring tempdir with on.exit ? see if either of these posts help - https://stackoverflow.com/questions/53152076/temp-files-automatically-deleted-in-r-shiny-app https://stackoverflow.com/questions/54076077/is-it-possible-to-stop-rscript-cleaning-up-its-tempdir/54095374#54095374 – Mike Aug 02 '19 at 18:24
  • @Mike thanks for your response. Your 1st link refers back to this question. Concerning `on.exit` the problem is, that `runApp()` isn't exiting when the folder is deleted. I provided another rep. example [here](https://github.com/rstudio/shiny/issues/2542) which comes closer to the 'real problem'. – ismirsehregal Aug 05 '19 at 08:28

2 Answers2

2

By now I've found a setting in Windows 10 (Storage Sense) concerning the deletion of temporary files, which seems to be active by default.

Navigate as follows and uncheck:

  1. Settings
  2. System Storage
  3. Storage Sense
  4. Change how we free up space automatically
  5. Delete temporary files that my apps aren't using

Windows Storage Sense

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
0

With the deletion of your temp directory also session data gets lost. But if I understand your question correctly, this is not relevant for your Shiny Application. So if you don‘t need any session data from yesterday you could call ‘.rs.restartR()‘ to restart your R session and thus setting a new temporary directory. You will probably get an error that your last session could not be saved (as the directory doesn‘t exist anymore). After this you should be able to start your Shiny App again.

  • 1
    Thanks for your answer! To make it clear: the deletion of the temp directory doesn't happen intentionally. It is an unwanted effect on windows machines after the app is idling for some time (the above code is just an example to reproduce the error). `.rs.restartR()` seems to work only out of RStudio - The problem appears when using RScript.exe. Furthermore, the process of restarting R (and the app) isn't the problem. The problem is how to prevent the above error or as an alternative how to trigger that restart after the error occured. – ismirsehregal Aug 06 '19 at 11:06
  • I did understand that the deletion of the temp directory was not intentionally. But this is the nature of a temp dir: it will get deleted by the system some time in the future... I am not sure if you aim to prevent the deletion of the temp dir or if you want to set R into a state that it can (again) start the Shiny App. – Søren Schaffstein Aug 06 '19 at 11:49
  • I'm aware of the fact that one shouldn't expect persistent storage from `tempdir()`. I'm not the one incorporating `tempdir()` - it's used in the shiny source code (without using it's argument `check = TRUE`). For me both options would be okay (preventing or reacting to the error). The main point is not to end up with an undead shiny app which in fact is still running but can't serve anything to the user but `Error in file: cannot open the connection`. – ismirsehregal Aug 06 '19 at 12:02
  • Awarding the bounty here because it's the only answer and otherwise it would simply disappear. Cheers – ismirsehregal Aug 08 '19 at 04:30
  • Thanks a lot. I am still thinking about an approach, but don't have access to a Windows machine until Tuesday to test it... Idea is: what can we do to make the running App shutdown safely during idle-time so it can be started safely the next morning. (Have you tried running Rscript in vanilla mode already?) – Søren Schaffstein Aug 08 '19 at 13:31
  • Thanks for coming back here. I just started a test using vanilla mode. However it might take some days before it fails. Please let me know if you have any other ideas. – ismirsehregal Aug 09 '19 at 07:29