通过snakemake R脚本中的列表循环的问题

时间:2019-05-21 01:58:26

标签: r for-loop snakemake

我试图像下面的snakefile一样,在snakemake规则中循环浏览R脚本中的一个列表,但是出现了错误。

from snakemake.utils import R

rule test:
    run:
        R("""
            print("hello!")
            a = c(1, 2, 3)
            for (i in a)
            {
                print(i)
            }
        """)

这是错误。

RuleException:
NameError in line 12 of Snakefile:
The name '\n    print(i)\n' is unknown in this context. Please make sure that you defined that variable. Also note that braces not used for variable access have to be escaped by repeating them, i.e. {{print $1}}
File "Snakefile", line 12, in __rule_test
File "~/miniconda/envs/py36/lib/python3.6/concurrent/futures/thread.py", line 56, in run
Exiting because a job execution failed. Look above for error message
Shutting down, this might take some time.

当我直接在R中运行该代码时,它没有给出任何错误。有人有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:4)

{}用于在snakemake中调用变量,即使在run命令中也是如此。
您必须将它们加倍才能逃脱它们。

该错误消息是有益的:

  

在这种情况下,名称'\ n print(i)\ n'是未知的。请做出来   确保已定义该变量。另请注意,未使用花括号   变量访问必须通过重复它们来避免,即{{print   $ 1}}

因此您的代码应如下:

from snakemake.utils import R

rule test:
    run:
        R("""
            print("hello!")
            a = c(1, 2, 3)
            for (i in a)
            {{
                print(i)
            }}
        """)