简体   繁体   中英

problem with loop through a list in snakemake R script

I tried to loop through a list in a R script in a snakemake rule as in the following snakefile, but got errors.

from snakemake.utils import R

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

Here are the errors.

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.

The code gave no errors when I ran it directly in R. Anyone have any ideas what's wrong? Thanks.

{ and } are used to call variables in snakemake, even in run command.
You have to double them to escape them.

The error message is informative:

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, ie {{print $1}}

So your code should like:

from snakemake.utils import R

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM