Kelly N.
Kelly N.

Reputation: 51

How can I feed a string that includes quotes to a system2 call in R?

I have a somewhat niche question that I'm hoping someone can answer for me or help me find a work-around to:

I've written a script in R that will run an ImageJ macro for sets of images I produce as a part of my workflow.

Because this is work that I may publish at some point or may be used by other researchers in the lab after me, I like to keep a copy of the R script and ImageJ macro within each dataset's folder as I sometimes modify the script a little for a certain series of images and this makes it very clear which version of code I used to process which set of images.

I am somewhat new to coding so I'm slowly trying to make this piece of code more streamlined and to have fewer places within the script that need to be modified each time I copy it to a new datafile, which is where I'm running into an issue.

In the script, I call the macro using the following code:

macro <- function(i) {
  system2('/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx', args=c('-batch "/Users/xxxx/yyyy/zzzz/current experiment/ImageJ Macro.ijm"', i))
}

For each new project I need to edit the filepath manually. I would love a way to define a variable at the beginning of the script which could be passed into the arguments as a string, but I can't figure out how to do it.

I've tried creating a variable just for the filepath, but R can't recognize a variable as part of the string that includes '-batch...'

I've also tried creating a variable containing the entire string that needs to be passed to args, but that doesn't work either. Here's what I coded for that attempt:

ImageJMacro <- paste(getwd(),"/ImageJ Macro.ijm",sep="")
batch1 = sprintf('-batch "%s"', ImageJMacro)
batchline = sprintf("'%s'", batch1)

As you can see, I had to do this in two steps because the single quotes outside of double quotes was giving an error. I thought this would work because when I run:

cat(batchline)

The string looks correct, but when passed into the arguments clause of the system command like so:

macro <- function(i) {
  system2('/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx', args=c(batchline, i))
}

it still throws an error.

Any ideas? Other solutions I should try? Thanks in advance for your help, I appreciate it!

Editing to add additional clarification as requested by @rmagn0:

ImageJ is an image analysis software which allows you to write 'macros', hard-coded scripts of repetitive analyses and apply them across many images. I'm not including the ImageJ macro code here because it's not relevant to my question. Suffice it to say that it expects to receive a string argument input, which it then parses out into several components used in the image processing. These string components are parsed out using an asterisk as a delimiter as described in this stack overflow: Calling an ImageJ Macro from R

I am trying to use R to pass a list of arguments to my ImageJ macro, one for each data file I need analyzed, as demonstrated in the code above. Note on above: I named the function in R 'macro', but it is really just calling the command line instance of my ImageJ macro.

If I were to run one instance of the list in the command line, it would look like this:

Contents/MacOS/ImageJ-macosx -batch "/Users/xxxx/yyyy/zzzz/current experiment/ImageJ Macro.ijm" ImageName.tif*Xcoord*Ycoord*/Users/xxxx/yyyy/zzzz/InputDirectory*/Users/xxxx/yyyy/zzzz/OutputDirectory*Suffix

Upvotes: 2

Views: 87

Answers (0)

Related Questions