我有以下bash脚本,其中调用R脚本
#!/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我想修改R脚本中的bash变量并在bash脚本中返回它们的修改后的值,因为我随后将修改后的变量传递给其他地方。 R脚本是
#!/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]))但是, print和echo的输出分别是:
[1] "33" "2" "1" 33 1 0这表明新值不会从R传递给bash。 我究竟做错了什么?
I have the following bash script in which an 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 $cI want to modify the bash variables in the R script and return their modified values in the bash script because I am then passing the modified variables somewhere else. The 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 output of the print and echo respectively are:
[1] "33" "2" "1" 33 1 0which shows that the new values aren't passed from R to bash. What am I doing wrong?
最满意答案
由于Rscript不允许环境变量操作,因此您需要从bash程序中捕获R输出。
许多可能性之一是使用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]}As Rscript does not allow environment variable manipulation you will need to capture the R output from the bash program.
One of the many possibilities is to use an 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]}更多推荐
发布评论