0

I have run into what appears to be a fundamental R problem (bug?).

The problem is I am participating in R package development and there is a pretty sophisticated function. When I run this function it leaves a lot of garbage in memory, which is only partially cleared by gc(). As a result when I want to run this function within a loop what happens is in the 2nd-3rd iteration, R consumes 12-16gb and stops working due to insufficient memory.

This problem is Windows-specific since my collaborators cannot reproduce it under LINUX. Hence my question is this: can I perform an R restart within a loop.

I understand this will remove all the variables, but that's what I need.

E.g. a good solution will be to write a script that calls out R session, executes the function and then closes R session.

Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
Pavel Shliaha
  • 773
  • 5
  • 16
  • Do you need something like `rm(list = ls())`? – SamPassmore Jan 22 '17 at 14:01
  • Write an R script that takes an input variable `i`, which tells it to run the `ith` iteration from your loop only, and save the result to a file. Call this R script from the command line using Rscript. – konvas Jan 22 '17 at 14:34

1 Answers1

1

Your question is a duplicate and will probably get flagged soon, but until then, this is probably your best bet (from another question that is nearly identical to yours):

Try placing one of these lines into your .Rprofile file:

makeActiveBinding("refresh", function() { shell("Rgui"); q("no") }, .GlobalEnv)

makeActiveBinding("refresh", function() { system("R"); q("no") }, .GlobalEnv)

Then entering refresh in the R console. It will shut down the current session and start up a new one.

[Source]

Here's some things to try:

Restarting R from within R: .rs.restartR() however, this may not clean the unused files. You may need to use source() or gc().

You may need to run it from a command line

Here's a post asking the same question, and another. Here's a great article about memory management in R.

Always google before asking. All of this was found very quickly.

Community
  • 1
  • 1
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
  • I am terrbily sorry, but I do not follow this answer. If I understand correctly the mentioned command will close and start R again. However this will also stop all the procedures and remove the variables.What I need is smth like: for (i in 1:10){ callNewR; xx <- runBigFution (i); close new R} will makeActiveBinding do that? – Pavel Shliaha Jan 22 '17 at 21:35
  • I'm not sure - you didn't post any code. I just googled your question and read some articles/posts. In the future, try to avoid `Can I` questions on Stack Overflow, `Can I's` come from a lack of research. If you think you *need* to ask `Can I`, use Google instead. If you had tried a few things and said "I'm seeing this error, how do I fix it, or do you know of a better way?" That's closer to the purpose of SO. Odds are, if code isn't central to your question you should do more research before asking SO. If Google isn't helping, you can try [#R on Freenode](http://irc.lc/freenode/r). Good luck. – Travis Heeter Jan 22 '17 at 22:08