[R] one way to write scripts in R

Muhammad Rahiz muhammad.rahiz at ouce.ox.ac.uk
Mon Mar 29 20:51:21 CEST 2010


I've been calling R from shell using the following (as example) ...

#!/bin/bash
for dir in $(ls *.txt); do
    R CMD BATCH script.R
done

Muhammad

Tsjerk Wassenaar wrote:
> Hi,
>
> That seems quite neat. To make it a bit more flexible, and maybe do
> some argument acrobatics with bash, you could change the first few
> lines to something like
>
>
> #!/bin/bash
>
> exec R --vanilla -q --slave -e "source(file=pipe(\"sed -n
> /^##RSTART/,\$p $0\"))" --args $@
>
> ##RSTART
>
> # Script here
>
> Cheers,
>
> Tsjerk
>
> On Mon, Mar 29, 2010 at 6:31 PM, Jason E. Aten <j.e.aten at gmail.com> wrote:
>   
>> Thanks Gabor. I didn't realize you could.  Here is the scriptdemo.rsh  file
>> as a text attachment, in case the line wraps made it hard to read/use.
>>
>> - Jason
>>
>> On Mon, Mar 29, 2010 at 11:19 AM, Gabor Grothendieck <
>> ggrothendieck at gmail.com> wrote:
>>
>>     
>>> Thanks.
>>>
>>> You might want to repost it as a text attachment since many of the
>>> lines wrapped around.
>>>
>>> Another more permanent possibility would be to put it on the R wiki at
>>> http://rwiki.sciviews.org/doku.php
>>>
>>> Note that the gsubfn package has a facility for quasi-perl type string
>>> interpolation as well. Just preface any function with fn$ and the
>>> facility is applied to the arguments of the function (subject to
>>> certain heuristics which determine which args to apply it to).
>>>
>>>       
>>>> library(gsubfn)
>>>> today <- format(Sys.Date())
>>>> show <- list()
>>>> show$syntax <- 43
>>>> Sys.setenv(AN_ENV_VAR="greetings (I'm an env var!)")
>>>>
>>>> fn$cat('Getting `Sys.getenv("AN_ENV_VAR")` from the environment, on
>>>>         
>>> $today,
>>> + `show$syntax` is also possible.\n')
>>> Getting greetings (I'm an env var!) from the environment, on 2010-03-29,
>>> 43 is also possible.
>>>
>>>
>>>
>>> On Mon, Mar 29, 2010 at 11:41 AM, Jason E. Aten <j.e.aten at gmail.com>
>>> wrote:
>>>       
>>>> Dear R users,
>>>>
>>>> A colleague of mine asked me how to write a script (an executable text
>>>>         
>>> file
>>>       
>>>> containing R code) in R. After I showed
>>>> him, he said that after extensive searching of the R archives, he had not
>>>> found anything like these techniques.
>>>>
>>>> He suggested that I share these methods to enable others to leverage R as
>>>>         
>>> a
>>>       
>>>> better alternative to bash/perl scripts.
>>>>
>>>> So in the interest of giving back to the R community, and with all
>>>>         
>>> humility,
>>>       
>>>> I offer the
>>>> following small demonstration of one method for creating scripts of R
>>>>         
>>> code
>>>       
>>>> that are
>>>> executable from the (at least Linux) command line.
>>>>
>>>> I don't make any warrantees that this will work for you, but if it helps
>>>> somebody at least
>>>> get starting utilizing R effectively in scripts, then great!
>>>>
>>>> Best regards,
>>>>
>>>> Jason
>>>>
>>>> --
>>>> Jason E. Aten, Ph.D.
>>>>
>>>>
>>>> # file: scriptdemo.rsh
>>>>
>>>>         
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>>
>>
>>     
>
>
>
>



More information about the R-help mailing list