0

Im trying to pass a python command from R (on Windows x64 Rstudio) to a python script via the command promt. It works if I type directly into cdm but not if I do it via R using the R function system(). The format is (this is how I EXACTLY would write in the windows cmd shell/promt):

pyhton C:/some/path/script <C:/some/input.file> C:/some/output.file

This works in the cmd promt, and runs the script with the input file (in <>) and gives the output file. I thought I in R could do:

system('pyhton C:/some/path/script <C:/some/input.file> C:/some/output.file')

But this gives an error from python about

error: unparsable arguments: ['<C:/some/input.file>', 'C:/some/output.file']

It seems as if R or windows interpret the white spaces different than if I simply wrote (or copy-paste) the line to the cmd promt. How to do this.

4
  • can you give the real command to investigate? Commented Apr 29, 2015 at 13:28
  • Im trying to get the RWebLogo::weblogo R function to work. It throws from R a python command. Works fine in cmd promt, but I would like to have it work via R (as intended). The script says: Usage: weblogo [options] < sequence_data.fa > sequence_logo.eps Commented Apr 29, 2015 at 14:22
  • Which script says that? on the RWebLogo readme I can see only weblogo(file.in=fpath, file.out='mylogo_msf.pdf'). Anyway the input file is after < and the output is before the > Commented Apr 29, 2015 at 14:38
  • Yes but if I run the R function weblogo it throws an error. From this error you can see that it call the weblogo script. If you do this in cmd promt exactly like I write above it works. So I thought to use system() to send the exact same command to the console. Commented Apr 29, 2015 at 14:59

1 Answer 1

1

From ?system

This interface has become rather complicated over the years: see system2 for a more portable and flexible interface which is recommended for new code.

System2 accepts a parameter args for the arguments of your command.

So you can try:

system2('python', c('C:\\some\\path\\script', 'C:\\some\\input.file', 'C:\\some\\output.file'))

On Windows:

R documentation is not really clear on this point (or maybe it's just me), anyway it seems that on Windows the suggested approach is to use the shell() which is less raw than system and system2, plus it seems to work better with redirection operators (like < or >).

shell ('python C:\\some\\path\\script < C:\\some\\input.file > C:\\some\\output.file')

So what is this command doing is:

  1. Call python
  2. Telling python to execute the script C:\some\path\script. Here we need to escape the '\' using '\'.
  3. Then we passing some inputs to the script using a the '<' operator and the input.file
  4. We redirect the output (using '>') to the output file.
Sign up to request clarification or add additional context in comments.

15 Comments

did you escape the brackets with `\`?
If I write like you with <C:\\some\\input.file> (ie. < as is ) R doesnt recognize <: Error: unexpected '<' in "system2. What do you mean with 'escape brackets' then ig not the <'s ?
I put the < > just because you had it in your original question, I thought you used them just to indicate a placeholder. Just remove them and use the quotation marks
Ah I see. No the <> need to be there, its part of the argument, they need to "quote" the input file. How do you escape them, or rather: why would you escpae them?
If I write like in your answer (but with '<C:\\some\\input.file>' - brackets and quotes) it says unparsable arguments: ['<C:/some/input.file>', 'C:/some/output.file'].
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.