A couple of batch mode questions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

A couple of batch mode questions

Rich Shepard
   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?

   Given these choices, when would I want to run a script within R using
source(), and when would it be better for me to run it from the command
line?

   Reading Appendix B.4 Scripting with R in R-info.pdf, ?Rscipt, and ?BATCH I
get the impression that 'R CMD BATCH' reads from a named file and writes
output to a named file and Rscript reads/writes to stdin/stdout using
redirection. What are the pros and cons of each? When (or why) would I
select one over the other?

TIA,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Bert Gunter-2
"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.

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.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Wed, Jul 25, 2018 at 7:41 AM, Rich Shepard <[hidden email]>
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?
>
>   Given these choices, when would I want to run a script within R using
> source(), and when would it be better for me to run it from the command
> line?
>
>   Reading Appendix B.4 Scripting with R in R-info.pdf, ?Rscipt, and ?BATCH
> I
> get the impression that 'R CMD BATCH' reads from a named file and writes
> output to a named file and Rscript reads/writes to stdin/stdout using
> redirection. What are the pros and cons of each? When (or why) would I
> select one over the other?
>
> TIA,
>
> Rich
>
> ______________________________________________
> [hidden email] 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Rich Shepard
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions [ANSWERED]

Rich Shepard
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

R help mailing list-2
In reply to this post by Rich Shepard
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" <[hidden email] on behalf of [hidden email]> 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
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Eric Berger
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 <
[hidden email]> 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" <
> [hidden email] on behalf of [hidden email]> 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
>
>     ______________________________________________
>     [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Rich Shepard
In reply to this post by R help mailing list-2
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Rich Shepard
In reply to this post by Eric Berger
On Wed, 25 Jul 2018, Eric Berger wrote:

> 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.

Eric,

   I'll definintely look at that package.

> 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.

   My immediate need is to import 30 data files, change factors into dates
and datetimes, print a summary, then plot a PDF scatterplot. This is why I
need to learn Rscript. I should be able to wrap that in a bash shell
script's for ... do loop that runs the Rscript for all *.dat files in the
directory.

Much appreciated,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Eric Berger
You should be able to do all this within R.
From what you have written I don't see a compelling reason to use scripts
at the shell level.

Best,
Eric


On Wed, Jul 25, 2018 at 9:41 PM, Rich Shepard <[hidden email]>
wrote:

> On Wed, 25 Jul 2018, Eric Berger wrote:
>
> 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.
>>
>
> Eric,
>
>   I'll definintely look at that package.
>
> 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.
>>
>
>   My immediate need is to import 30 data files, change factors into dates
> and datetimes, print a summary, then plot a PDF scatterplot. This is why I
> need to learn Rscript. I should be able to wrap that in a bash shell
> script's for ... do loop that runs the Rscript for all *.dat files in the
> directory.
>
> Much appreciated,
>
>
> Rich
>
> ______________________________________________
> [hidden email] 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Rich Shepard
On Wed, 25 Jul 2018, Eric Berger wrote:

> You should be able to do all this within R. From what you have written I
> don't see a compelling reason to use scripts at the shell level.

Eric,

   The source() help page's last example calls a set of scripts to run
sequentially. I assume this is used to run the same .R script on each file,
with any file-specific variables used in each one. Perhaps I'm not seeing
how to pass parameters to the script call with source() so that the same .R
script can be automagically run for each imput file name. In my newness to
this it appears Rscript allows running the same script substituting inpu
file names based on arguments on the Rscript command line.

Regards,

Rich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Bert Gunter-2
Eric may have more to say, but the straightforward answer is: use functions
to do what you want and pass any file specific info to them as parameters
of function calls.

If this seems arcane to you, then you have some homework to do, as using
functions is a (maybe the) central programming paradigm in R. Numerous
tutorials elaborate on this.

If I have misunderstood, my apologies, and just disregard.

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Wed, Jul 25, 2018 at 1:02 PM, Rich Shepard <[hidden email]>
wrote:

> On Wed, 25 Jul 2018, Eric Berger wrote:
>
> You should be able to do all this within R. From what you have written I
>> don't see a compelling reason to use scripts at the shell level.
>>
>
> Eric,
>
>   The source() help page's last example calls a set of scripts to run
> sequentially. I assume this is used to run the same .R script on each file,
> with any file-specific variables used in each one. Perhaps I'm not seeing
> how to pass parameters to the script call with source() so that the same .R
> script can be automagically run for each imput file name. In my newness to
> this it appears Rscript allows running the same script substituting inpu
> file names based on arguments on the Rscript command line.
>
> Regards,
>
> Rich
>
> ______________________________________________
> [hidden email] 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/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: A couple of batch mode questions

Rich Shepard
On Wed, 25 Jul 2018, Bert Gunter wrote:

> Eric may have more to say, but the straightforward answer is: use
> functions to do what you want and pass any file specific info to them as
> parameters of function calls.

Bert,

   I was considering that functions would be the way to go. The functions can
call other functions in addition to doing calculations.

Thanks for re-inforcing that approach,

Rich

______________________________________________
[hidden email] 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.