Restart R session without interrupting the for loop
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 in Rstudio but continue running script
You could break the code into 2 files and write a batch file (.bat
) that runs the first file through .rs.restartR()
and then the remainder of the code in the next file.
You could also skip the .bat
and just schedule both .R
scripts to run in Task Scheduler.
Also, please see my comment regarding garbage collection (gc()
).
restart the rsession using R code
Depending on what you are actually trying to accomplish, you may simply want something like
rm(list=ls())
Note: be careful this code actually delete your entire work/variable/data
Related Topics
Compute All Fixed Window Averages with Dplyr and Rcpproll
How to Make a Post Request with Header and Data Options in R Using Httr::Post
How to Change the Default Font Size in Ggplot2
How to Remove "Rows" with a Na Value
Name Columns Within Aggregate in R
R Package Xtable, How to Create a Latextable with Multiple Rows and Columns from R
How to Jitter Text to Avoid Overlap in a Ggplot2 Scatterplot
Running Cor() (Or Any Variant) Over a Sparse Matrix in R
Is There Any Other Package Other Than "Sentiment" to Do Sentiment Analysis in R
Sliding Time Intervals for Time Series Data in R
Merge Two Dataframes If Timestamp of X Is Within Time Interval of Y
R: Replace All Values in a Dataframe Lower Than a Threshold with Na
Effectively Debugging Shiny Apps
Figures Captions and Labels in Knitr
Any Way to Pause at Specific Frames/Time Points with Transition_Reveal in Gganimate
How to Replicate a Ddply Behavior That Uses a Custom Function with Dplyr