6

I am trying to call a Windows program called AMDIS from within R using the call

system("C:/NIST08/AMDIS32/AMDIS_32.exe /S C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF")

in order to carry out an analysis (specified using the /S switch) on a file called CI23_Q_120828_01.CDF, but it seems that no matter what I try the file is not loaded in correctly, presumably because the options are not passed along. Does anyone have a clue what I might be doing wrong?

Right now this command either

  1. doesn't do anything,
  2. makes AMDIS pop up, but it doesn't load the file I specify
  3. gives me the error

    Warning message:

running command 'C:/NIST08/AMDIS32/AMDIS_32.exe /S C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF' had status 65535

(I have no idea what results in these different outcomes of the same command)

(the AMDIS command line options are described here at the page 8)

Cheers,
Tom

EDIT:
Found it had to do with forward vs backslashes - running

system("C:\\NIST08\\AMDIS32\\AMDIS_32.EXE C:\\Users\\Ento\\Documents\\GCMS\\test_cataglyphis_iberica\\queens\\CI23_Q_120828_01.CDF /S /E")

seems to work - thank you all for the suggestions!

Mooncrater
  • 4,146
  • 4
  • 33
  • 62
Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
  • 2
    You say `/S` but have `/s` in your call. If you open a terminal and type your command as is, does it work? – flodel Apr 26 '13 at 23:56
  • changed it to /S, but still no luck unfortunately – Tom Wenseleers Apr 27 '13 at 08:28
  • but main question is how can I properly pass command line options? – Tom Wenseleers Apr 27 '13 at 08:33
  • still, it is is important you test that command directly in a terminal, so you can rule out tons of other things that could be wrong; i.e., maybe you are already passing command line options properly but it is failing for other reasons: wrong options, bad inputs, bad install, etc. – flodel Apr 27 '13 at 11:38
  • hmm still no luck, it seems that in the command prompt in Windows C:\NIST08\AMDIS32\AMDIS32$.EXE C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CDF /S /E works OK, but I just don't seem to be able to make this work in R... I think it has to do something with forward & back slashes. Any thoughts? – Tom Wenseleers Apr 27 '13 at 14:55

1 Answers1

8

You've heard of bquote , noquote , sQuote, dQuote , quote enquote and Quotes, well now meet shQuote!!! :-)

This little function call works to format a string to be passed to an operating system shell. Personally I find that I can get embroiled in backslash escaping hell, and shQuote saves me. Simply type the character string as you would on the command line of your choice ('sh' for Unix alikes like bash , csh for the C-shell and 'cmd' for the Windows shell ) wihtin shQuote and it will format it for a call from R using system:

shQuote("C:/NIST08/AMDIS32/AMDIS_32.exe /S C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF" , type = "cmd" )
#[1] "\"C:/NIST08/AMDIS32/AMDIS_32.exe /S C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF\""

More generally, you can use shQuote like this:

system( shQuote( "mystring" , type = c("cmd","sh") ) , ... )
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • hmm still no luck, it seems that in the command prompt in Windows C:\NIST08\AMDIS32\AMDIS32$.EXE C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CDF /S /E works OK, but I just don't seem to be able to make this work in R... I think it has to do something with forward & back slashes. Any thoughts? – Tom Wenseleers Apr 27 '13 at 14:54
  • My first would be does it work as expected typing it straight into the command line? If so my second thought would be is it something to do with permissions? Try running R with administrator privileges (right click... properties... security... run as... or something vaguely similar) to see if that helps. The forward slashes you see in R will be passed as back slashes to the command line so this isn't the problem. – Simon O'Hanlon Apr 27 '13 at 15:10
  • Yes C:\NIST08\AMDIS32\AMDIS32$.EXE C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CD‌​F /S /E typed directly in the Windows command prompt works OK. Also tried with administrator privileges, but no luck still. If I type in R string=shQuote("C:/NIST08/AMDIS32/AMDIS32$.EXE C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/CI23_Q_120828_01.CDF /S /E", type = "cmd") system(string) just nothing happens... – Tom Wenseleers Apr 27 '13 at 15:18
  • Ok, try surrounding the second argument with quotes? Also, the $ symbol is an odd thing to have in the name of the executable. I wonder if this is causing problems (but I see why you did this in the docs for calling it from athird party program) – Simon O'Hanlon Apr 27 '13 at 15:28
  • I note that the `/E` switch does not function with AMDIS32$. Have you tried `C:\NIST08\AMDIS32\AMDIS32$.EXE C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\CI23_Q_120828_01.CD‌​‌​F /S` only? – Simon O'Hanlon Apr 27 '13 at 15:30
  • Also you have to use the `/M` switch to bring AMDIS to the foreground. Currently you are running AMDIS in background mode (according to the docs - I have no experience running this program). – Simon O'Hanlon Apr 27 '13 at 15:35
  • Reverted to using AMDIS_32.exe, since that accepts the /E switch (the other one just ignores it) and also works just OK on the command prompt. In R I tried string=shQuote("C:/NIST08/AMDIS32/AMDIS_32.EXE 'C:/CI23_Q_120828_01.CDF' /S /E", type = "cmd") system(string) but then again it doesn't seem to do anything... – Tom Wenseleers Apr 27 '13 at 15:37
  • Try adding the argument `internal=TRUE` to the system call to see what is actually returned to R, might help to diagnose?, i.e. `system( shQuote(...) , internal = TRUE )` – Simon O'Hanlon Apr 27 '13 at 15:39
  • Sorry, `intern` not `internal` – Simon O'Hanlon Apr 27 '13 at 15:41
  • Ha then it says > system(string,intern=T) Error in system(string, intern = T) : '"C:/NIST08/AMDIS32/AMDIS_32.EXE C:/CI23_Q_120828_01.CDF /S /E /M"' not found even though AMDIS_32.EXE is in C:\NIST08\AMDIS32\AMDIS_32.EXE – Tom Wenseleers Apr 27 '13 at 15:42
  • But... therein lies the problem!! Try something simple like `cd C:/NIST08/AMDIS32/` does that work? From R? – Simon O'Hanlon Apr 27 '13 at 15:44
  • And the strange thing is that when I type string=shQuote("C:/NIST08/AMDIS32/AMDIS_32.exe", type = "cmd" ) system(string,intern=T) it opens AMDIS OK - it's only when I try to add the additional filename as an option that it doesn't work and says it can't find the file – Tom Wenseleers Apr 27 '13 at 15:49
  • Can you use `system( shQuote( "cd C:\Users\Ento\Documents\GCMS\test_cataglyphis_iberica\queens\" , type = "cmd" ) )` and access the data file directory ok? – Simon O'Hanlon Apr 27 '13 at 15:57
  • Well setwd("C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/") works OK - if I try system( shQuote( "cd C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/" , type = "cmd" ),intern=T) it says Error in system(shQuote("cd C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/", : '"cd C:/Users/Ento/Documents/GCMS/test_cataglyphis_iberica/queens/"' not found – Tom Wenseleers Apr 27 '13 at 18:15
  • 1
    I had one final thought - that perhaps since the file name is passed to the program as an argument perhaps you *do* need to use backslashes, in which case you can pass them from R by escaping them like this: `C:\\Users\\Ento\\Documents\\GCMS\\test_cataglyphis_iberica\\queens\\CI23_Q_120828_01.CD‌​F` Please let me know if this works! – Simon O'Hanlon Apr 27 '13 at 22:03