Run Rscript from Rstudio


I am a new investigator and I am starting working with Rstudio to automatize my process.

I want to run a r script in with there is a loop. Inside this, I would like to open another R session (different program to the original code runing) to run a second process. Once, this second process is finished, I get results in the first R session and the second R must be close.

I have searched about it and a read about Batch files, Rscript.exe and so on. The problem is that everything I tried I failed.

Anybody knows how could I do it? When I coded "Rscript myscript.R" either with and without the complete path, the program gives me error.

Thank you!

It's hard to give you any advice without a concrete example of what you are trying to do and what have you tried already, but from what you have said so far, it seems to me that you don't need another R session, even if you are parallelizing your code, you could simply source another script or even easier, define a function.
Can you explain why you need another R session?


Hello Andres, thanks for answer.

My original code has 2 loops inside. The first one stablish the factor combination and the second one runs a bootstrap of 1000 repetition for each combination. The thing is that the program runs really fast at start, but in every combination is getting slowlier. For example, the first combination runs up to 50 simulation/second, but the process continues is slowing down little by little up to 1 simulation/second o less. The complete process has about 1,5million simulations, so you can imagin the time that it takes to finish it.

If I stop the process at any time, clean the memory ("rm(list=ls()") and run it again, R has the same speed of process that before, but if I close completely the program and restart from 0, R recoveries the highest speed. Probably, program saturates by save any kind of information, but I have made sure that the vectors in which I save the results are reset on each loop.

That is why I need to separate the bootstrap of 1000 simulation from the fisrt loop. That is to say, in each factor combination call to another session of R, run the bootstrap for that combination, take the results and close it. In this way the program is restarted and whatever is saturating the program is released for each combination.

I think source(mysecondscript.R) is running in the same R session as original code, so the problem persists.

Any idea?, Sorry if I explain me bad.

Thank you.

I can think on some things that might be affecting your code but again, without a concrete example (ideally a reprex), I'm just guessing.
Loops in R are not the most computationally efficient approach, Have you considered vectorizing your code instead? Are you growing vectors instead of prealocating memory? This could be very slow.
I would recommend profiling your code to find the bottle neck and making a reprex so you can get more specific advice.

That is the case if the code is run sequencially but I think your code would benefit greatly from parallelization, I'm not sure but I think the backends even do what you are after, that is opening a new R session on the background for each parallel thread.

Hello Andres,

thanks again for anwering. I have solve the problem. I have divided my code in 2 as I mentioned you yerterday. First code stablishes the factors combination (in total there are 800 posible combinations) and command to execute the second code through the windows console, which opens Rscript.exe. This second process do 1000 simulations, so in total there are about 1 million simulations in combitanation of code 1 and 2.

To execute Rscript.exe I used the following command:

system("cmd.exe", input = paste('"C:\Program Files\R\R-3.6.2\bin\Rscript.exe" "C:\Users\nobody\myRcodes\MySecondCode.R"'))

but \ in double!!!

That was my problem, I did not find the complete command till yesterday to execute Rscript.exe from another Rsession.

I hope this helps to anyone in my same situation.

Best regards and thank you so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.