2

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
Kelly N.
  • 51
  • 5
  • Can you provide an example the actual full command intended to be run from the command line? `/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx ...` – Ramiro Magno Aug 17 '22 at 21:09
  • Sure, so I have the function defined as follows: `macro <- function(i) { system2('/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx', args=c(batchline, i)) }` Then I call that function and apply it to list of parameters needed by the ImageJ macro with the following line: 'runme<- lapply(list, macro)' – Kelly N. Aug 17 '22 at 21:32
  • I meant the full command in the terminal, not in R. So I am asking for the full command that `system2()` is supposed to be calling. – Ramiro Magno Aug 17 '22 at 21:37
  • 1
    Ah I see, okay in the terminal an example of a command I am trying to pass would be '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' Is that what you're looking for? – Kelly N. Aug 17 '22 at 21:55
  • I can only pass one argument to the ImageJ Macro, but I need it to contain several pieces of information, so I have them delimited by asterisks. My comment above was formatting some asterisks to make the text italic, so I added spaces- hopefully still legible! – Kelly N. Aug 17 '22 at 21:57
  • I am sorry, but I am confused... First, if you're the one defining the function `macro()`, then you could make it to accept all pieces of information necessary. Then do you realise that having spaces in your paths leads to problems? So please just put your command in your post inside a command block, exactly as you'd run it in the console. Do not add formating tags such as asterisks which have a special meaning, i.e. wildcard expansion. Otherwise it's very confusing. – Ramiro Magno Aug 17 '22 at 22:05
  • Okay, I've edited my original post to include the information you requested. – Kelly N. Aug 17 '22 at 23:38

0 Answers0