Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart R within Rstudio

Tags:

r

rstudio

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?

like image 275
Ben Hunter Avatar asked Mar 27 '13 18:03

Ben Hunter


People also ask

How do I restart R?

You can restart R by clicking on Session -> Restart R (top menu).

What does restarting R Session do?

r() restarts you R session. Loaded packages will not reload. Your environment wont be saved.

How do I reset my workspace in R?

You can do both by restarting your R session in RStudio with the keyboard shortcut Ctrl+Shift+F10 which will totally clear your global environment of both objects and loaded packages.


2 Answers

In Rstudio, you can restart the R session by:

command/ctrl + shift + F10  

You can also use:

.rs.restartR() 
like image 155
mgoldwasser Avatar answered Oct 08 '22 15:10

mgoldwasser


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.

like image 33
Arthur Avatar answered Oct 08 '22 15:10

Arthur