7

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

9
  • 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 5, 2018 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 5, 2018 at 10:52
  • 1
    @hrbrmstr actually it is shiny itself storing files in tempdir(). Here is a related GitHub issue. I'm having the same problem. Aug 1, 2019 at 6:11
  • 1
    Added a gif here showing how to reproduce the problem. Aug 1, 2019 at 9: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 stackoverflow.com/questions/17107206/change-temporary-directory. Then make another copy of the tempdir for the next time. (Copying the files failed for me.) Aug 1, 2019 at 14:35

2 Answers 2

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

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

0
+50

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.

6
  • 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. Aug 6, 2019 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. Aug 6, 2019 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. Aug 6, 2019 at 12:02
  • Awarding the bounty here because it's the only answer and otherwise it would simply disappear. Cheers Aug 8, 2019 at 4: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?) Aug 8, 2019 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.