Return argument from R to bash

I have the following bash script in which the R script is called

#!/bin/bash

declare -x a=33   
declare -x b=1       
declare -x c=0

Rscript --vanilla MWE.R $a $b $c

echo $a $b $c

I want to change bash variables in an R script and return their changed values ​​in a bash script, because then I pass the changed variables somewhere else. R script is

#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)

Rb = as.numeric(args[2])
Rc = as.numeric(args[3])

Rb = Rb + 1
Rc = Rc + 1

args[2]=Rb
args[3]=Rc

print(c(args[1],args[2],args[3]))

However, the conclusion printand, echoaccordingly:

[1] "33" "2"  "1" 
33 1 0

which shows that new values ​​are not passed from R to bash. What am I doing wrong?

+4
source share
1 answer

Since it Rscriptdoes not allow manipulation of the environment variable, you will need to write the output Rfrom the program bash.

One of the many possibilities is to use array:

#!/bin/bash

declare a=33   
declare b=1       
declare c=0

declare -a RESULT
RESULT=($(Rscript --vanilla MWE.R $a $b $c))

a=${RESULT[1]}
b=${RESULT[2]}
c=${RESULT[3]}
+3
source

All Articles