[R] How to get 'R' to talk BACK to other languages / scripts??

Ista Zahn izahn at psych.rochester.edu
Fri Dec 3 21:11:25 CET 2010


Hi Mike,
Is this what you want?

#!/bin/bash
myTest=$(Rscript testing.R.r)
echo "myTest contains"
echo $myTest

Note that testing.R.r will have to cat() or print() myResult (e.g, my
testing.R.r contains

myResult <- paste("Hello World")
cat(myResult)

Best,
Ista

On Fri, Dec 3, 2010 at 1:23 PM, Mike Williamson <this.is.mvw at gmail.com> wrote:
> Hey everyone,
>
>    I know that I can call 'R' from other scripts, and that I can make
> command calls from 'R' (e.g., using system() ).  But how can I get 'R' to
> RETURN values to the script that called it.  E.g., I would like to be able
> to do something like the following (as a simpler example) from a bash
> script:
>
> #!/bin/bash
>
> myTest=echo /usr/local/bin/R --no-restore --no-save -f testing.R.r
>
> echo "myTest contains"
> echo $myTest
>
>
>    And ideally this should write out the results of the
> "testing.R.rscript".  So that if the testing.
> R.r script said something simple like:
>
> myResult <- paste("Hello World")
>
>    Then in the output of the bash script, it should say "myTest
> contains\nHello World" or something quite similar.  But instead it says
> myTest contains
> R version 2.11.1 (2010-05-31) Copyright (C) 2010 The R Foundation for
> Statistical Computing ISBN 3-900051-07-0 R is free software and comes with
> ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain
> conditions. Type 'license()' or 'licence()' for distribution details.
> Natural language support but running in an English locale R is a
> collaborative project with many contributors. Type 'contributors()' for more
> information and 'citation()' on how to cite R or R packages in publications.
> Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()'
> for an HTML browser interface to help. Type 'q()' to quit R. > > > myResult
> <- paste("Hello World") [1]
>
>    As you can see, the return from the bash call is really just capturing
> everything in the ST OUT once 'R' is called, which is not what I want.  I
> have tried several variations, using a function - return combination, etc.
> I have also looked through help pages & documentation.  As far as I can
> tell, it seems no one ever bothers to get information OUT of 'R' and into
> other scripts.  I could write to a csv file & read that file, but that seems
> a REALLY clunky way to handle variable passing.
>
>                                Thanks for any help!
>                                          Regards,
>                                                    Mike
>
>
>
>
> "Telescopes and bathyscaphes and sonar probes of Scottish lakes,
> Tacoma Narrows bridge collapse explained with abstract phase-space maps,
> Some x-ray slides, a music score, Minard's Napoleanic war:
> The most exciting frontier is charting what's already here."
>  -- xkcd
>
> --
> Help protect Wikipedia. Donate now:
> http://wikimediafoundation.org/wiki/Support_Wikipedia/en
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list