生成和bash脚本内执行R,Python和等,剧本 [英] Generate and execute R, Python, etc.., script from within bash script

查看:162
本文介绍了生成和bash脚本内执行R,Python和等,剧本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到这一段时间的解决方案,但没有发现任何令人满意呢。我写了很多的bash脚本的,但有时我想用R或Python作为脚本的一部分。现在,我最终不得不写两个剧本;原始bash脚本执行的任务的前半部分,并且R或Python脚本执行的任务的第二个一半。我所说的R / Python脚本在bash脚本中。

I have been trying to find a solution for this for a while but haven't found anything satisfactory yet. I write a lot of bash scripts, but sometimes I want to use R or Python as part of the script. Right now, I end up having to write two scripts; the original bash script to perform the first half of the task, and the R or Python script to perform the second half of the task. I call the R/Python script from within the bash script.

我不满意这个解决方案,因为它分裂了我的两个文件的程序,这增加了的事情不同步的机会,更多的文件来跟踪等..是否有编写一个包含文本块的方式我的R / Python脚本的全部,再有bash的吐出到一个文件中并传递参数给资讯科技及电讯;执行呢?是否有一个简单的解决方案?这比简单的传递单行到R / Python的,因为它通常涉及创建和几个步骤操作对象更加复杂。

I am not satisfied with this solution because it splits my program over two files, which increases the chances of things getting out of sync, more files to track, etc.. Is there a way to write a block of text that contains the entirety of my R/Python script, and then have bash spit it out into a file and pass arguments to it & execute it? Is there an easier solution? This is more complicated than passing simple one-liners to R/Python because it usually involves creating and manipulating objects over several steps.

推荐答案

有可能是很多的解决方案,但这个作品:

There are probably lots of solutions, but this one works:

#!/bin/bash
## do stuff
R --slave <<EOF
  ## R code
  set.seed(101)
  rnorm($1)
EOF

如果您希望能够灵活地传递附加的bash参数R,我建议:

If you want the flexibility to pass additional bash arguments to R, I suggest:

#!/bin/bash
## do stuff
R --slave --args $@ <<EOF
  ## R code
  set.seed(101)
  args <- as.numeric(commandArgs(trailingOnly=TRUE))
  do.call(rnorm,as.list(args))
EOF


  • 这允许参数灵活一些,但假定他们都将是数字

  • 它也假定所有参数将通过从bash脚本传递到R子脚本

  • 显然,你可以放宽这些,例如参考参数位置上。

    obviously you could relax these, e.g. refer to parameters positionally

    这篇关于生成和bash脚本内执行R,Python和等,剧本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆