首页 > 解决方案 > 在snakemake R脚本中循环遍历列表的问题

问题描述

我试图在如下蛇文件中的 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 中运行代码时,代码没有出错。任何人都知道有什么问题吗?谢谢。

标签: rfor-loopsnakemake

解决方案


{并且}用于在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)
            }}
        """)

推荐阅读