On Wed, 25 Jul 2018, Bert Gunter wrote:> "Within R one can use source() to run a batch file of R commands, while R > CMD BATCH and Rscript are run from the command line. Is this correct?" > > Yes.Bert, Thanks for confirming.> I think your query answers your query: You use Rscript when you want to use > R in the command environment, perhaps as part of an analytical pipeline; > and you source an R file when you want to work within R.That's a given. Why would I prefer Rscript over R CMD BATCH, or vice-versa? I did not see much difference between the two in their help files. Regards, Rich
On Wed, 25 Jul 2018, Rich Shepard wrote:> That's a given. Why would I prefer Rscript over R CMD BATCH, or > vice-versa? I did not see much difference between the two in their help > files.Digging deeper into the Web I read that R CMD BATCH is an older approach to automating R processing from the command line and Rscript is the newer approach. My question's answered. Rich
From my perspective, which is a unix-alike perspective, Rscript makes R useable
in exactly the same way as other unix style scripting languages such as perl,
tcsh, bash, etc. This is useful, and a good thing. If I remember (and
understood) correctly, it is why Rscript was introduced, later in R's
history than BATCH.
Scripts run using Rscript can be (slightly) more self-contained, which I suppose
might or might not be an advantage, depending on one's needs:
To run an R script using Rscript, I type
./myRscript.r
at the command line (less typing), whereas with batch mode, it has to be
R CMD BATCH myRscript.r
(more typing) but if I want any R options applied, such as --no-restore or
--no-save, I include them within the script when using Rscript, but have to put
them on the command line outside the script when using BATCH.
One can actually execute an R command at the shell prompt using Rscript, which
can't be done with BATCH:
[296]% Rscript -e 3+4
[1] 7
[298]% Rscript -e 'sqrt(2)'
[1] 1.414214
If I want to pass custom parameters to the script (script-specific parameters of
my own that I put on the command line), the syntax for either supplying them or
parsing them might be different. I'm not sure, since I don't do this
very often, and never use CMD BATCH. But it would be worth checking.
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
?On 7/25/18, 8:22 AM, "R-help on behalf of Rich Shepard"
<r-help-bounces at r-project.org on behalf of rshepard at appl-ecosys.com>
wrote:
On Wed, 25 Jul 2018, Bert Gunter wrote:
> "Within R one can use source() to run a batch file of R commands,
while R
> CMD BATCH and Rscript are run from the command line. Is this
correct?"
>
> Yes.
Bert,
Thanks for confirming.
> I think your query answers your query: You use Rscript when you want to
use
> R in the command environment, perhaps as part of an analytical
pipeline;
> and you source an R file when you want to work within R.
That's a given. Why would I prefer Rscript over R CMD BATCH, or
vice-versa? I did not see much difference between the two in their help
files.
Regards,
Rich
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Some additional comments that might be relevant to people interested in these topics. 1. For R scripts you should also consider the package littler developed by Dirk Eddelbuettel, Highly recommended. For info http://dirk.eddelbuettel.com/code/littler.html or the github repository. 2. Scripts can be useful both for short calculations, extending the shell, or for large R jobs that are not interactive and can run unsupervised. e.g. I have a script that is run automatically on a daily schedule. It performs a number of calculations and updates a database with the results. 3. When creating a large-ish R project that will run as a script, I still break the task into smaller tasks which are implemented as R scripts which are source()'ed into the batch job. The smaller R scripts are usually developed and debugged in an interactive environment, such as RStudio. Best, Eric On Wed, Jul 25, 2018 at 7:36 PM, MacQueen, Don via R-help < r-help at r-project.org> wrote:> From my perspective, which is a unix-alike perspective, Rscript makes R > useable in exactly the same way as other unix style scripting languages > such as perl, tcsh, bash, etc. This is useful, and a good thing. If I > remember (and understood) correctly, it is why Rscript was introduced, > later in R's history than BATCH. > > Scripts run using Rscript can be (slightly) more self-contained, which I > suppose might or might not be an advantage, depending on one's needs: > > To run an R script using Rscript, I type > ./myRscript.r > at the command line (less typing), whereas with batch mode, it has to be > R CMD BATCH myRscript.r > (more typing) but if I want any R options applied, such as --no-restore or > --no-save, I include them within the script when using Rscript, but have to > put them on the command line outside the script when using BATCH. > > One can actually execute an R command at the shell prompt using Rscript, > which can't be done with BATCH: > > [296]% Rscript -e 3+4 > [1] 7 > [298]% Rscript -e 'sqrt(2)' > [1] 1.414214 > > If I want to pass custom parameters to the script (script-specific > parameters of my own that I put on the command line), the syntax for either > supplying them or parsing them might be different. I'm not sure, since I > don't do this very often, and never use CMD BATCH. But it would be worth > checking. > > -Don > > -- > Don MacQueen > Lawrence Livermore National Laboratory > 7000 East Ave., L-627 > Livermore, CA 94550 > 925-423-1062 > Lab cell 925-724-7509 > > > > ?On 7/25/18, 8:22 AM, "R-help on behalf of Rich Shepard" < > r-help-bounces at r-project.org on behalf of rshepard at appl-ecosys.com> wrote: > > On Wed, 25 Jul 2018, Bert Gunter wrote: > > > "Within R one can use source() to run a batch file of R commands, > while R > > CMD BATCH and Rscript are run from the command line. Is this > correct?" > > > > Yes. > > Bert, > > Thanks for confirming. > > > I think your query answers your query: You use Rscript when you want > to use > > R in the command environment, perhaps as part of an analytical > pipeline; > > and you source an R file when you want to work within R. > > That's a given. Why would I prefer Rscript over R CMD BATCH, or > vice-versa? I did not see much difference between the two in their help > files. > > Regards, > > Rich > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Wed, 25 Jul 2018, MacQueen, Don wrote:> From my perspective, which is a unix-alike perspective, Rscript makes R > useable in exactly the same way as other unix style scripting languages > such as perl, tcsh, bash, etc. This is useful, and a good thing. If I > remember (and understood) correctly, it is why Rscript was introduced, > later in R's history than BATCH.Don, As a linux-only user for more than two decades I really appreciate the value of running scripts from the command line. For a current project, after extracting 29 years of data from PDF forms I ran them through a bash shell script (passing the name of the source file as $1) that calls two sed and six awk scripts. The output is ready to be read into R.> If I want to pass custom parameters to the script (script-specific > parameters of my own that I put on the command line), the syntax for > either supplying them or parsing them might be different. I'm not sure, > since I don't do this very often, and never use CMD BATCH. But it would be > worth checking.This is what I need to work out now for Rscript: how to set positional or named parameters on the command line with the source file name and the R data.frame name. Thanks for your comments. Best regards, Rich