Reputation: 896
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?
Upvotes: 44
Views: 97635
Reputation: 6931
Similar to .rs.restartR()
there is a documented function for RStudio:
rstudioapi::restartSession(clean = TRUE)
Documentation here of use ?restartSession
.
With clean = TRUE
loaded packages and data objects are removed. You could add this onto the end of your script to restart R once complete.
There is also a command
argument if you want to specify commands to execute immediately after restarting.
Upvotes: 0
Reputation: 3284
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!
Upvotes: 2
Reputation: 41
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.
Upvotes: 4
Reputation: 15394
In Rstudio, you can restart the R session by:
command/ctrl + shift + F10
You can also use:
.rs.restartR()
Upvotes: 95
Reputation: 1228
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.
Upvotes: 16
Reputation: 109
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?
Upvotes: 0