R.exe, Rscript.exe Explanation

Hi, I'm new to programming and I'm confused on the difference between the two. I have googled this and I am still confused on the difference after reading the responses.

Part of the reason I am confused is I am thinking in terms of running script in Batch files. For instance, lets say I have a script in R and I create a batch file that runs the script where I use R.exe. When I put this in the command prompt and run the batch file, it just takes the script I made and runs it in the console of R right?

I've seen that you can run batch files uses Rscript.exe, which confuses me because when if I take an R script I made and put it into the script part of R (above the console) how would this do anything since the script must be put into the console for it to run. (Unless Rscript.exe runs whatever it is in the script part of R?)

If anyone could please explain how this all works to me, I would greatly appreciate it.
Thanks!

Hello,

The R.exe is the windows executable that will launch R and its graphic interface so you can work with it (that's what happens when you click the shortcut on your windows desktop for example)

The Rscript.exe will take an existing script, run R in the background, execute the script and close afterwards. There is no graphical interface opened and no other human interaction needed.

If you're planning to execute an R script in batch as part of a longer pipeline (so without opening R interface), you need to use the Rscript.exe and a script you've written beforehand.

In this example, a batch script will provide an R script a folder path and a number, and R will generate a csv file in that folder with the specified number of random numbers.

R script (saved in C:/myRscript.R)

#!/usr/bin/env Rscript

#Assign the arguments passed from the batch file to R variables
args = commandArgs(trailingOnly = TRUE)
outputFolder = args[[1]]
value = as.integer(args[[2]])

#Run the R code
print("Start the R code")
myData = data.frame(random = runif(value))
write.csv(myData, paste0(outputFolder, "randomNrs.csv"), row.names = F)

Batch file (shell)

outputFolder=C:/Documents/project/

echo "This is the start of the batch script"

#Run the R script and pass it arguments
Rscript C:/myRscript.R $outputFolder 10

ls $outputFolder

Note that the Rscript command in batch sometimes needs an R module to be loaded or you have to specify the full path to the Rscript (depending on the machine you're working on)

I hope this helps clarify the use of Rscript a bit!
PJ

2 Likes

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