4

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!

Fleur De Lys
  • 480
  • 2
  • 9
fyb123
  • 43
  • 1
  • 5

2 Answers2

3

First, some terminology: even though the concept of batch processing is generic, and it means unassisted execution, the term batch file is usually reserved for MS-Windows files processed by cmd.exe, MS-Windows traditional script files. The term used for files containing R commands is usually R scripts, or Rscripts.

That said, please consider the following simple R script, named HelloFriend.R:

my.name <- readline(prompt="Enter name: ")
print(paste("Hello, ", my.name, "!"))

When run directly in R console, as

> source('HelloFriend.R')

it will show the output

Enter name: 

If the user types Some Name and hits Enter, the program will output

[1] "Hello, Some Name !"

If it's run in the command line as R --no-save --quiet < HelloFriend.R, it will generate the output

> my.name <- readline(prompt="Enter name: ")
Enter name: 
> print(paste("Hello, ", my.name, "!"))
[1] "Hello,   !"
> 

And finally, if run with Rscript --vanilla HelloFriend.R, it will generate the output

Enter name: 
[1] "Hello,   !"

In other words, when run inside the R console, the user input will be expected. When run under R, but in the command line, the program will not give the user the opportunity to type anything, but the command echo will be shown.

And finally, under Rscript, the user input will also not be expected, but the command echo will not be shown.

Rscript is the preferred form of running R scripts, as its name suggests. The passing of R scripts in the command line to R via redirection also gives batch processing but will echo the commands executed. Therefore it can help debug code, but it's not the preferred way of executing production code.

Hilton Fernandes
  • 559
  • 6
  • 11
1

The analogy with batch files is a good one. R.exe is for interacting with the language, entering one statement at a time, and evaluating the results before entering the next statement. Rscript.exe is for running an existing script (file) containing R commands. You generally invoke Rscript.exe with the name of the script.

Running Rscript.exe my_script.R from the command-line is sort of like running

source("my_script.R")
q("no")

from the R console.

mob
  • 117,087
  • 18
  • 149
  • 283
  • Ahh okay so basically R.exe and Rscript.exe produce the same result in terms of batch files? – fyb123 Mar 06 '20 at 04:46
  • Ahh okay so basically R.exe and Rscript.exe produce the same result in terms of batch files? – fyb123 Mar 06 '20 at 04:46
  • 1
    Unfortunately, the equivalence is not true, as Rscript prints the value of variables when they are alone in a line. For instance, for a script containing `a <- 1 ; a` will output `1` under Rscript, but not in a call to `source()` with echo turned FALSE. – Hilton Fernandes Dec 10 '20 at 23:02