Skip to content

Returning Variables in R to Linux Shell

4 messages · Bierbryer, Andrew, Whit Armstrong, Barry Rowlingson +1 more

#
try littler:

warmstrong at linuxsvr2:/tmp$ export MYVALUE=`r -e 'cat(10)'`
warmstrong at linuxsvr2:/tmp$ env|grep MYVALUE
MYVALUE=10
warmstrong at linuxsvr2:/tmp$




On Wed, Apr 22, 2009 at 10:48 AM, Bierbryer, Andrew
<abierbryer at klsdiversified.com> wrote:
#
On Wed, Apr 22, 2009 at 3:48 PM, Bierbryer, Andrew
<abierbryer at klsdiversified.com> wrote:

            
You can use back-ticks (`) in most shells to capture output. So print
the value you want using R's cat() function, and capture it thus:

$ cat test.R
string <- 'TEST'
cat(string)

$ v=`R --slave --no-save < test.R `
$ echo $v
TEST

bash shell also allows $( ) notation:

$ v=$(R --slave --no-save < test.R )

 note the use of --slave to make R shut up about itself.

Barry
#
On 22 April 2009 at 11:42, Whit Armstrong wrote:
| try littler:
| 
| warmstrong at linuxsvr2:/tmp$ export MYVALUE=`r -e 'cat(10)'`
| warmstrong at linuxsvr2:/tmp$ env|grep MYVALUE
| MYVALUE=10
| warmstrong at linuxsvr2:/tmp$

Thanks to a suggestion by Paul Gilbert, littler supports the 'status'
argument to q() to pass result codes back to the shell --- in bash you can
pick that up via $? as shown here:

edd at ron:~> r -e 'q(status=10)'; echo "Status was $?"
Status was 10
edd at ron:~>

Dirk