'shQuote() breaks system() command rather than escaping spaces

I'm running the GCMS software AMDIS inside an R script to analyze GCMS data. The basic syntax is:

system("AMDIS_PATH GCMS_FILE_PATH /S /GC /GE /E"

With "/S /GC /GE /E" referring to functionalities within the AMDIS program.

I want to make the code 'fool-proof', so I'm allowing paths with "/" instead of "\\". And I'm also allowing spaces in the paths, escaping them with shQuote(..., type = "cmd")

Which means the full code looks like

system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), shQuote(gsub("/", "\\", GCMS_FILE_PATH, fixed = T), type = 'cmd'), "/S /GD /GS /E"))

This code does not work (output = 65535), but there's a catch:

If I remove the shQuotes from my GCMS_FILE_PATH. Then everything works like it should (so long as the FILE_PATH does not contain any spaces). So executing the code like this does work:

system(paste(shQuote(gsub("/", "\\", AMDIS_PATH, fixed = T), type = 'cmd'), gsub("/", "\\", GCMS_FILE_PATH, fixed = T), "/S /GD /GS /E"))

Any idea why shQuote works fine for the AMDIS_PATH, but not for the GCMS_FILE_PATH? And how I can run my code and still escape spaces in the GCMS_FILE_PATH?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source