【问题标题】:How to qsub an R script as a heredoc?如何将 R 脚本 qsub 作为heredoc?
【发布时间】:2017-06-12 16:40:19
【问题描述】:

我可以像这样直接向qsub提交一个bash heredoc:

my_var="foobar"
qsub <<HERE
  #!/bin/bash
  echo "$my_var"
HERE

我可以像这样使用 bash heredoc 运行 R 脚本:

my_var="foobar"
Rscript - "arg1" "arg2" <<E0F
  ## R code
  cat("\nR loaded\n")
  args <- commandArgs(TRUE)
  cat("Script args are:\n")
  args
  print("$my_var")
  sessionInfo()
E0F

我可以直接将保存的 R 脚本提交到 qsub,而无需 bash 包装脚本,如下所示:

qsub -wd $PWD -o :${PWD}/ -j y -b y Rscript my_script.R "arg_1" "arg_2"

my_script.R 看起来像这样:

$ cat my_script.R
#!/usr/bin/env Rscript
cat("\nR loaded\n")
args <- commandArgs(TRUE)
cat("Script args are:\n")
args
sessionInfo()

但是,如果我尝试将其结合起来并直接将 R 脚本作为 heredoc 提交......

my_var="foobar"
qsub -wd $PWD -o :${PWD}/ -j y -b y Rscript - "foo" "bar" <<E0F
#!/usr/bin/env Rscript
cat("\nR loaded\n")
args <- commandArgs(TRUE)
cat("Script args are:\n")
args
print("$my_var")
sessionInfo()
E0F

...它不起作用。我没有任何输出。也没有错误消息。

有没有办法做到这一点?也对 Python 感兴趣。我正在考虑尝试嵌套的 heredocs,但需要文字 \t 缩进效果不佳,因为我的 IDE 都自动用空格替换它们。

这也是使用 Sun Grid Engine qsub

【问题讨论】:

  • 为什么?只需为/usr/bin/Rscript 或我最喜欢的littler 中的/usr/bin/r 编写正确的#! 脚本(以及在CRAN here 上)。最后一个也是stdin,所以你可以试着把你的hereddocs硬塞进去。
  • 再现性和文档。我希望我的 R 和 Python 代码嵌入到周围的 bash 脚本和 Markdown 笔记本中。当我有多个脚本文件来跟踪事情时,很容易变得混乱和混乱。
  • “仅凭愿望是无法做到的。”看/usr/bin/R的源码,它本身就是一个shell脚本。因此,我对替代方案的暗示......
  • 不起作用。我认为问题在于qsub 在二进制模式下如何处理标准输入。

标签: python r bash heredoc qsub


【解决方案1】:

看起来我对缩进要求有误,嵌套的 heredocs 实际上确实可以解决这个问题。

# find files I need to process
files_I_want="$(find /my/dir/ -type f -name "*.txt")"
output_dir="/other/dir"

# iterate over files
for some_file in $files_I_want; do
    # set output file path
    output_file="${output_dir}/$(basename "$some_file")"

    # submit bash heredoc qsub job
    qsub -wd $PWD <<E0F
# R example
# start R heredoc
Rscript - "${some_file}" "${output_file}" <<E0F2
args <- commandArgs(TRUE)

my_crazy_function <- function(input_file, output_file){
    print("input file is:")
    print(input_file)
    print("output file is:")
    print(output_file)
}
print("This is R")
my_crazy_function(input_file = args[1], output_file = args[2])

E0F2

# Python example
python - "${some_file}" "${output_file}" <<E0F3
import sys

def my_crazy_function(input_file, output_file):
    print "Input file is: ", input_file
    print "Output file is: ", output_file

input_file = sys.argv[1]
output_file = sys.argv[2]
print "This is Python"
my_crazy_function(input_file, output_file)
E0F3
E0F

done

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多