R.exe and Rscript.exe - r

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!

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.

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.

Related

executing file contents as commands under R from Linux terminal

I've written a text file containing script for R. I've gotten it to run under Windows from a .bat file running a .txt file under R with CMD BATCH.
I'm trying to replicate that (minus the clickability) in the Terminal
I've changed the permissions for program execution, I've set the file to have the shebang, and have tried rewriting for it a few different programmes such as
#!/usr/bin/R
library(rvest)
library(plyr)
which returns an error "Syntax error near unexpected symbol 'rvest'
and
#!/home/robert/Téléchargements/R-3.2.3/src/unix/Rscript.c
library(rvest)
library(plyr)
which also returns an error "Syntax error near unexpected symbol 'rvest'
Separately, on both of these I changed the file extension from nothing to .R
In one case it gave the same error, in the other it started a session of R but didn't execute the commands.
I realise it's a messy question, but I'm having difficulty getting these ducks in a row.
Ultimately, this was what worked:
R < /home/robert/R/scraper1.R --no-save
But here's the rest of my answer in case that doesn't work for someone else:
I'm not sure if you've seen any of these, but here's some stuff to try:
Dupe?
The top answer from a very similar post: I'm going to assume you googled your question before posting it, so I'm sure you've already seen this, but it's not referenced in your question, so here it is [Source]:
Content of script.r:
#!/usr/bin/Rscript
cat("Hello")
Invocation from command line:
./script.r
?Rscript
?Rscript allows you to run R scripts in a Unix-esque system [Source]:
## example #! script for a Unix-alike
#! /path/to/Rscript --vanilla --default-packages=utils
args <- commandArgs(TRUE)
res <- try(install.packages(args))
if(inherits(res, "try-error")) q(status=1) else q()
Batch
Here's something from an old R mail pipe [Source]:
Place the line: R --vanilla < foo.txt foo.results into a file named foo.batch. No other text should be in the file.
Make this file executable via chmod 755 foo.batch
At the command line try at -f foo.batch now or perhaps, batch -f foo.batch.
If this does not work, ask your system administrator how to set up a batch process.
The advantage of the batch process is 1. you need not be logged in, 2.
your job will take a lower priority than interactive jobs.
R -e
Loading two libraries and running an R command [Source]
R -e 'library("rmarkdown");library("knitr");rmarkdown::render("NormalDevconJuly.Rmd")'
R -e 'library("markdown");rpubsUpload("normalDev","NormalDevconJuly.html")'
Other
R < scriptname.R --no-save [Source]
$ source("scriptname.R") [Source]

How do I pass the environment of one R script to another?

I'm effectively trying to tack save.image() onto the end of a script without modifying that script.
I was hoping something like Rscript target_script.R | saveR.R destination_path would work, where saveR.R reads,
args.from.usr<-commandArgs(TRUE)
setwd(args.from.usr[1])
save.image(file=".RData")
But that clearly does not work. Any alternatives?
You can write an R script file that takes two parameters: 1, the script file you want to run, and 2, the file you want to save the image to.
# runAndSave.R ------
args.from.usr <- commandArgs(trailingOnly=TRUE)
source(args.from.usr[1])
setwd(args.from.usr[2])
save.image(file=".RData")
And then run it with
Rscript runAndSave.R target_script.R destination_path
You could try to program a task to be done within the OS of that computer. In Linux you will be using the terminal and there is this tool called CRON. In Windows you can use Task Scheduler. If you program the OS to open a terminal and load an script and later save the image, you maybe will get what you need, save the data generated from the script without actually modifying it.

Shiny script from Ubuntu bash

I wanted to run with crontab (the system daemon used to execute desired tasks at certain times) a shiny script.
I first tried running sh Autorun.sh, being in the file:
R
shiny::runApp(...)
but that didn't work
I also tried writting directly Rscript shiny::runApp(...) but it also doesn't work
Any ideas?
To run code using R on shell, you must use the -e option, which stands for expression. The same thing can be done via Rscript.
The correct syntax is then:
R -e 'shiny::runApp(...)'
Care must be taken with the quotes if there are any in the expression being used.
For more information on other command-line options, check An Introduction to R - Appendix B.

Rscript logfile with input, errors and output

Is there a way to fully log (stdin, stdout, stderror) a R session executed via Rscript in batch mode?
I know, that I can use R CMD BATCH to log the full session of a batch job. I also know how to write errors and output into a logfile using Rscript but since it is often said that R CMD BATCH is a relict from the old days and one should use Rscript, I was wondering if it can also log everything.
I suppose it is impossible but I am not sure - neither do I know why.

interactive R startup script

I'm trying to run interactive R (Windows XP) with an input script that runs a few commands, and then leaves me at the R command line prompt. However, when I run it, it exits.
For example, here's the input file:
test.r:
x = 1
x
Here's what happens when I run it with the input file as a parameter:
C:\>R --file test.t
>x = 1
>x
[1] 1
C:\> <--- exits and returns to prompt
Is there any way to run this without R exiting?
I'd do it the other way around.
Just write a script that starts the normal R GUI or terminal application (per your choice), but then also place a file .Rprofile in the same directory which contains the code you want executed each and every time.
See help(Startup) on details about the files R looks up at startup and this may become clearer.

Resources