43

I'm trying to call a simple python script from within R using system2(). I've read some information I found vague that said if 'too much' memory is used, it won't work.

If I load a large dataset and use some information in it to use as arguments to pass into system2(), it will only work if I manually click "Restart R" in call Rstudio.

What I want:

df <- read.csv('some_large_file.csv')
###extracting some info called 'args_vec'
for(arg in args_vec){
    system2('python', args)
}

This won't work as is. The for loop is simply passed over.

What I need:

df <- read.csv('some_large_file.csv')
###extracting some info called 'args_vec'
###something that 'restarts' R
for(arg in args_vec){
    system2('python', args)
}

This answer doesn't quite get what I want. Namely, it doesn't work for me within Rstudio and it calls "system" (which presents the same problem as "system2" in this case). In fact, when I put the answer referenced above in my Rprofile.site file, it just immediately closed rstudio:

I tried the suggestion as a normal function (rather than using "makeActiveBinding", and it didn't quite work.

##restart R in r session  -- doesn't work
makeActiveBinding("refresh", function() { system("R --save"); q("no") }, .GlobalEnv)

##nor did this:
refresh <- function() { system("R --save"); q("no") }

I tried a number of variations of these two options above, but this is getting long for what feels like a simple question. There's a lot I don't yet understand about the startup process and "makeActiveBinding" is a bit mysterious. Can anyone point me in the right direction?

Community
  • 1
  • 1
Ben Hunter
  • 876
  • 1
  • 8
  • 18
  • Are you sure that you really need to restart the whole R? If you use `rm(list=ls())` which cleans the workspace, and maybe manual garbage collection with `gc()` just in case, that should be enough? – Jouni Helske Mar 27 '13 at 19:09
  • @BenHunter it's immediately closing RStudio because you're calling `q` after restarting. Take out the call to `q` and it should do exactly the same thing as selecting Restart R in RStudio. – Matthew Plourde Mar 27 '13 at 19:42
  • @Hemmo, I know that's an option, and in this case it might be a good choice, but I'd really like to know how to solve this, as is, without losing my workspace (or saving, removing, and then reloading it). – Ben Hunter Mar 27 '13 at 22:00
  • @Matthew Plourde, that almost works, but Rstudio just reloads and doesn't give me a prompt. Specifically, all startup activity runs again, I can see packages from my Rprofile get loaded, but I don't see a ">" prompt. – Ben Hunter Mar 27 '13 at 22:01
  • @BenHunter, funny, I do get a prompt. Are you using the active binding or the function way? As a side note, using an active binding in this situation is playing with fire and unnecessary---I wouldn't advise it. – Matthew Plourde Mar 27 '13 at 22:04
  • @MatthewPlourde, just the function. Good to know about makeActiveBinding. But Rstudio just clams up when I call refresh(). I get the stop sign in the interpreter. I'm just letting it sit there for a while now, but it doesn't look like it's going to do anything. My Rprofile.site file is /usr/lib/R/etc. I'm going to try it on my machine at home and see how it works. Thanks for the advice. – Ben Hunter Mar 27 '13 at 22:25
  • If it doesn't seem to give you a prompt, try whacking Enter one time to see if it's just not being displayed. – bright-star Jan 17 '14 at 23:34

5 Answers5

91

In Rstudio, you can restart the R session by:

command/ctrl + shift + F10 

You can also use:

.rs.restartR()
mgoldwasser
  • 14,558
  • 15
  • 79
  • 103
15

RStudio has this undocumented rs.restartR() which is supposed to do just that: restarting R.

However, it does not unload the packages that were loaded, nor does it clean the environment, so that I have some doubts about if it restarts R at all.

Arthur
  • 1,208
  • 13
  • 25
  • 5
    It does free up whatever memory used to be used up and was later released though... so it does do something (very useful in some cases). – Patrick Coulombe Aug 04 '17 at 21:59
  • Indeed very useful for my use-case: I regularly read in a password-protected excel file on a shared drive using read.xlsx, which makes the excel file become read-only. I can then run the above line which prevents this consequence of read-only. Thus other members of my team can happily save any edits that are not important to my work. – its.me.adam Sep 28 '20 at 20:29
3

If you use RStudio, use the menu item Session > Restart R or the associated keyboard shortcut Ctrl+Shift+F10 (Windows and Linux) or Command+Shift+F10 (Mac OS). Additional keyboard shortcuts make it easy to restart development where you left off, i.e. to say “re-run all the code up to HERE”:

In an R script, use Ctrl+Alt+B (Windows and Linux) or Command+Option+B (Mac OS) In R markdown, use Ctrl+Alt+P (Windows and Linux) or Command+Option+P (Mac OS) If you run R from the shell, use Ctrl+D or q() to quit, then restart R.

Angel1988
  • 31
  • 3
2

For those not limited to a command that want something that actually resets the system (no prior state, no loaded packages, no variables, etc.) you can select Terminate R from the Session menu.

It is a bit awkward (asks you if you are sure). If anyone knows something like clear all or really clear classes in MATLAB let me know!

Jimbo
  • 2,886
  • 2
  • 29
  • 45
0

Have you tried embedding the function call within the apply function, rather than a for loop?

I've had some pieces of code that ran the system out of memory in a for loop run perfectly with apply. It might help?

Nathan Hatch
  • 109
  • 4