Hi All

I have just started using a subprocess.call()to call external commands. I want to call R from command line and that I can do it easily by subprocess.call('Rscript script.R') and it works fine. I want to pass the several arguments instead of using default arguments. I tried using Rscript script.R --args arg1 arg2 but it is not working. R is not recognising the aruments provided on command line,

Could anyone please help on this. How can I pass the parameters to R from windows command line? and How can I use subprocesss.call() with those parameters from python script.

Appreciate your help.

Cheers

Here are 2 expressions that you could try

subprocess.call("Rscript script.R --args arg1 arg2", shell=True)
subprocess.call(["Rscript", "script.R", "--args", "arg1", "arg2"])

Hi Gribouillis
Thanks for the reply. Its works perfectly, I was making mistakes in quotes.
For iterating over number of args I am doing

listofitems = [1,2,3,4,0.5,0.6]
listofitems2 = [a,b,c,d,e,f]
listofitems3 = [a1,s1,d3,4f,f4]

for arg1 in listofitems:
for arg2 in listofitems2:
for arg3 in listofitems3:
subprocess.call("Rscript script.R --args arg1 arg2", shell=True)

so that it can take each item from each list and iterate over it.

Does this make sense?


Here are 2 expressions that you could try

subprocess.call("Rscript script.R --args arg1 arg2", shell=True)
subprocess.call(["Rscript", "script.R", "--args", "arg1", "arg2"])
listofitems = [1,2,3,4,0.5,0.6]
listofitems2 = [a,b,c,d,e,f]
listofitems3 = [a1,s1,d3,4f,f4]

for arg1 in listofitems:
    for arg2 in listofitems2:
        for arg3 in listofitems3:
             subprocess.call("Rscript script.R --args arg1 arg2", shell=True)

so that it can take each item from each list and iterate over it.

Does this make sense?

Makes more sense at least if you remember to push that (code) button before pasting. Looks like it makes no sense for me script gets parameter 'args' fro example which even has no meaning in the python side and treats it as string. Also it is better to build directly list than have variables like a1,s1 etc. 4f is not also correct variable name.

Maybe you should check Learning R via Python ...or the other way around

Without that module you seem to like something like (I have not R so I have commented out it, and as you give no value for the variables I changed them to string to check the correctness):

listofitems = [1,2,3,4,0.5,0.6]
listofitems2 = ['a','b','c','d','e','f']
listofitems3 = ['a1','s1','d3','4f','f4']
debug = True 

for command in ("Rscript script.R --args %s %s %s" % (one, two, three)
                         for one in listofitems
                         for two in listofitems2
                         for three in listofitems3):

    if debug:
        print(command)
    else:
        subprocess.call(command , shell=True)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi Tonijv

thanks for the reply. It works perfect, faster than the usual for,for for loops
sorry for the bad way of posting code and variables.

cheers

start quote:

listofitems = [1,2,3,4,0.5,0.6]
listofitems2 = [a,b,c,d,e,f]
listofitems3 = [a1,s1,d3,4f,f4]

for arg1 in listofitems:
    for arg2 in listofitems2:
        for arg3 in listofitems3:
             subprocess.call("Rscript script.R --args arg1 arg2", shell=True)

so that it can take each item from each list and iterate over it.

Makes more sense at least if you remember to push that (code) button before pasting. Looks like it makes no sense for me script gets parameter 'args' fro example which even has no meaning in the python side and treats it as string. Also it is better to build directly list than have variables like a1,s1 etc. 4f is not also correct variable name.

Maybe you should check Learning R via Python ...or the other way around

Without that module you seem to like something like (I have not R so I have commented out it, and as you give no value for the variables I changed them to string to check the correctness):

listofitems = [1,2,3,4,0.5,0.6]
listofitems2 = ['a','b','c','d','e','f']
listofitems3 = ['a1','s1','d3','4f','f4']
debug = True 

for command in ("Rscript script.R --args %s %s %s" % (one, two, three)
                         for one in listofitems
                         for two in listofitems2
                         for three in listofitems3):

    if debug:
        print(command)
    else:
        subprocess.call(command , shell=True)

end quote.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi Tonijv

thanks for the reply. It works perfect, faster than the usual for,for for loops
sorry for the bad way of posting code and variables.

cheers

Also try

from itertools import product

for triple in product(listofitems, listofitems2, listofitems3):
    print triple

greattt.. thts perfect.:icon_surprised:

Also try

from itertools import product

for triple in product(listofitems, listofitems2, listofitems3):
    print triple

When thread is solved you can upvote any good answer or give reputation. Then mark your thread solved.

This question has already been answered. Start a new discussion instead.