Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Windows executable file from within R with command line options

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!

like image 864
Tom Wenseleers Avatar asked Apr 26 '13 23:04

Tom Wenseleers


People also ask

How do I run an R program from the command line?

Starting R If R has been installed properly, simply entering R on the command line of a terminal should start the program. In Windows, the program is typically specified as the action performed when clicking on an icon. You can also use this method on a *NIX system that has a window manager such as KDE.

How do I run a Windows executable?

To begin, click Start and select the "Search" function. When you type the name of the EXE file you want to open, Windows displays a list of the files it finds. Double-click on the EXE filename to open it. The program starts and displays its own window.


1 Answers

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") ) , ... )
like image 114
Simon O'Hanlon Avatar answered Nov 02 '22 15:11

Simon O'Hanlon