Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running R in the background

Hi this is a question which i am not sure how to frame.

I am running R from a remote server. My access to the remote server is via ssh@username and so on. After i access i have a command prompt which i invoke R and i am comfortable working on R.

Question 1 I have a large network (100k nodes) and would like to do community detection and would like to run it in the background such that if i close my terminal its keeps running until its finished saves the result in my R working directory.

I have tried using nohup R & but i am not sure the process completed successful.

Question 2 If i manage to implement Question 1, how could i continue to use R to perform other task? and

Question 3 How do i check the task in Q1 is still running.

my attempt on a script looks like this

#!/usr/bin/env Rscript
library(igraph)
library(data.table)
edgebetween <- function(x) {
  simplify(x, remove.multiple = F, remove.loops = T) 
   edge.betweenness.community(x, directed = T)
}
community.a.g3 <- edgebetween(a.g3)

i have saved the script in my working director as a.R

and used at the command prompt after changing to my working directory chmod +x a.R nohup ./a.R &

What would i need to correct. Thanks

like image 765
RLearner Avatar asked Aug 26 '16 17:08

RLearner


People also ask

Can R run in the background?

The callr package uses processx to start another R process, and run R code in it, in the foreground or background.

How do I run a background code in R?

Use job::job() to run chunks of R code in an RStudio job instead of the console. This frees your console while the job(s) go brrrrr in the background. By default, the result is returned to the global environment when the job completes.

How do I stop R from running on Mac?

If you're using R from the command line instead of from within RStudio, you need to use Ctrl + C instead of Esc to cancel the command. This applies to Mac users as well!

How do I run R all at once?

To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).


1 Answers

You want to execute R in "batch" mode. See ( https://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html )

The command below is an example from those docs. The "&" says "run this separate from the user's login session" so when you log out, it continues to run.

R CMD BATCH [options] infile [outfile] &

You can also use nohup as discussed here ( http://streaming.stat.iastate.edu/wiki/index.php/Running_Jobs_in_the_Background )

nohup R CMD BATCH ./myprog.R &
like image 59
Tod Casasent Avatar answered Oct 17 '22 23:10

Tod Casasent