Restart R session without interrupting the for loop

restart /r cmd
refresh r
r clear memory

In my for loop, I need to remove RAM. So I delete some objects with rm() command. Then, I do gc() but the RAM still the same

So I use .rs.restartR() instead of gc() and it works: a sufficient part of my RAM is removed after the restart of the R session.

My problem is the for loop which is interrupted after the R restart. Do you have an idea to automatically go on the for loop after the .rs.restartR() command ?

I just stumbled across this post as I'm having a similar problem with rm() not clearing memory as expected. Like you, if I kill the script, remove everything using rm(list=ls(all.names=TRUE)) and restart, the script takes longer than it did initially. However, restarting the session using .rs.restartR() then sourcing again works as expected. As you say, there is no way to 'refresh' the session while inside a loop.

My solution was to write a simple bash script which calls my .r file.

Say you have a loop in R that runs from 1 to 3 and you want to restart the session after each iteration. My bash script 'runR.sh' could read as follows:

  #!/bin/bash        

    for i in {1..3}
    do
      echo "Rscript myRcode.r $i" #check call to r script is as expected
      Rscript myRcode.r $i
    done

Then at the top of 'myRcode.r':

args <- commandArgs()
print(args) #list the command line arguments. 

myvar <- as.numeric(args[6])

and remove your for (myvar in...){}, keeping just the contents of the loop.

You'll see from print(args) that your input from your shell script is the 6th element of the array, hence args[6] in the following line when assigning your variable. If you're passing in a string, e.g. a file name, then you don't need as.numeric of course.

Running ./runR.sh will then call your script and hopefully solve your memory problem. The only minor issue is that you have to reload your packages each time, unlike when using .rs.restartR(), and may have to repeat other bits that ordinarily would only be run once.

It works in my case, I would be interested to hear from other more seasoned R/bash users whether there are any issues with this solution...

Restart R session without interrupting the for loop - r, restartR() instead of gc() and it works: a sufficient part of my RAM is removed after the restart of the R session. My problem is the for loop which is interrupted after  However, restarting the session using .rs.restartR() then sourcing again works as expected. As you say, there is no way to 'refresh' the session while inside a loop. My solution was to write a simple bash script which calls my .r file. Say you have a loop in R that runs from 1 to 3 and you want to restart the session after each iteration.

Do you have an idea to automatically go on the for loop after the .rs.restartR() command ?

It is not possible.


Okay, you could configure your R system to do something like this, but it sounds like a bad idea. I'm not really sure if you want to restart the for loop from the beginning or pick it up where left off. (I'm also very confused that you seem to have been able to enter commands in the R console while a for loop was executing. I think there's more than you are not telling us.)

You can use your rprofile.site file to automatically run commands when R starts. You could set it up to automatically run your for loop code whenever R starts. But this seems like a bad idea. I think you should find a different sort of fix for your problem.

Some of the things you could do to help the situation: have your for loop write output for each iteration to disk and also write some sort of log to disk so you know where you left off. Maybe write a function around your for loop that takes an argument of where to start, so that you can "jump in" at any point.

With this approach, rather than "restarting R and automatically picking up the loop", a better bet would be to use Rscript (or similar) and use R or the command line to sequentially run each iteration (or batch of iterations) in its own R session.

The best fix would be to solve the memory issue without restarting. There are several questions on SO about memory management - try the answers out and if they don't work, make a reproducible example and ask a new question.

RestartSession doesn't run the command after restarting in R3.5.1 , with the intention that a for loop will continue after restarting the R sessi. if I restart the code without manually restarting my RStudio session  1 Restart R session without interrupting the for loop Jan 24. 0 Hack to restart Rstudio session within a loop … from Rstudio Jan 24. Badges (1)

By saving the iteration as an external file, and writing an rscript which calls itself, the session can be restarted within a for loop from within rstudio. This example requires the following steps.

#Save an the iteration as a separate .RData file in the working directory. 

iter <- 1

save(iter, file="iter.RData")

Create a script which calls itself for a certain number of iterations. Save the following script as "test_script.R"

###load iteration
library(rstudioapi)

load("iter.Rdata")

###insert function here.
time_now <- Sys.time()

###save output of function to a file.
save(time_now, file=paste0("time_", iter, ".Rdata"))

###update iteration
iter <- iter+1
save(iter, "iter.Rdata")

###restart session calling the script again
if(iter < 5){
restartSession(command='source("test_script.R")')
}

Restart r session, Then shut down the computer force quit, R. The session was terminated' If your sudo rstudio-server restart rsession: no process found ~$ sudo 5 Feb 2018 Stopping and Restarting Oracle Restart for Maintenance Operations. group: The In this state can I then revert back to a clean In my for loop, I need to remove RAM. If you want to have everything run in a bash script, then R could just write one long script (i.e. output a string or series of strings to a file). This could be executed and the interrupt is guaranteed not to affect a loop, as you've unrolled the loop. Alternatively, you could write loops in bash. Here are examples.

You can make your script recursive by sourcing itself after restarting session.

Make sure the script will take into account the initial status of the loop. So you might have to save the current status of the loop in a .rds file before restarting session. Then call the .rds file from inside the loop after restarting session. This would help you start the loop where it was before restarting r session.

I just found out about this command 'restartSession'. I'm using it because I was also running into memory consumption issues as the garbage collector will not give back the RAM to the OS (Linux).

library(rstudioapi)
restartSession(command = "print('x')")

Managing the Server – RStudio Support, This utility enables the stopping, starting, and restarting of the server, enumeration and suspension of user sessions, taking the server offline,  Handle conditions with tryCatch () tryCatch() is a general tool for handling conditions: in addition to errors, you can take different actions for warnings, messages, and interrupts. You’ve seen errors (made by stop() ), warnings ( warning()) and messages ( message()) before, but interrupts are new.

B RStudio IDE Commands, buildAll, Install the package and restart R debugFinish, Execute the remainder of the current function or loop. debugHelp, Guide to interruptR, Interrupt R. Keyboard Shortcuts This information is available directly in the RStudio IDE under the Tools menu: Tools → Keyboard Shortcuts Help. Shortcuts in this article last updated for RStudio 1.3.957

[PDF] RStudio Server Pro Administrator's Guide, This utility enables the stopping, starting, and restarting of the server, When R sessions have been idle (no processing or user interaction) for a from forwarding to RStudio Server (otherwise it will end up in an infinite redirect loop). 3.4.2.2  Windows can be moved between session and even linked to multiple sessions; Windows can be split horizontally and vertically into panes; Support for UTF-8 and 256 colour terminals; Sessions can be controlled from the shell without the need to enter a session; Basic Functionality

Chapter 2 R basics and workflows, Chapter 2 R basics and workflows | STAT 545: Data wrangling, exploration, and This way of starting and stopping analytical work will not serve you well for you will find the PDF itself (no surprise) but also the script that created it ( toy-line.​r ). Good idea to do this, restart R (available from the Session menu), re-run your  Restart the equipment adjustment by holding down the power button for 7 seconds. Reactivate the automatic locking function and follow the instructions on the screen. The Gear Fit continuously restarts (bootloop) Many users complain that their Gear Fit suffers from sudden or continuous restarts (called bootloops).

Comments
  • This depends entirely on what is inside the for loop. If you have a way of "saving state" (periodically even, not necessarily every loop), then you can pick up where it left off when restarted. This would of course be a manual-restart (as @Gregor said, this will not happen), but at least you can preserve work.
  • This worked for me with one minor addition, the bash script needed to be modified to be executable with chmod 755 runR.sh.
  • In addition, to avoid restarting R every single run, I changed the bash sequence to step by 100 using this syntax for i in `seq 1 100 1000` and then running a loop within R for 100 iterations. This periodically clears memory in R but avoids the overhead of reloading libraries every run.
  • You can. And as you're suggesting, it is unconventional at best and more likely fragile/chaotic.