将参数从R返回到bash(Returning an argument from R to bash)

编程入门 行业动态 更新时间:2024-10-24 10:23:38
参数从R返回到bash(Returning an argument from R to bash)

我有以下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 $c

I 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 0

which 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]}

更多推荐

本文发布于:2023-07-15 19:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117549.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   bash   argument   Returning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!