Skip to content
Back to formatted view

Raw Message

Message-ID: <d8ad40b50904220855l5b1d715bg9a94290f15b2b4ba@mail.gmail.com>
Date: 2009-04-22T15:55:10Z
From: Barry Rowlingson
Subject: Returning Variables in R to Linux Shell
In-Reply-To: <FFFA004FC961264DB07D5FA80F85EDAF079939@exchange.KLS.corp>

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

> For example, if I had a simple one line R script that just did
>
> ? ? ? ? ? ?string <- 'TEST',
>
>
>
> when I call
>
> /usr/local/bin/R -no-save < MY_R_FILE,
>
>
>
> how can I put the value TEST into a shell variable?

 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