5

I've written a macro in imageJ that spits out a data frame which I then analyze in R. I'd love to be able to have the whole procedure work in R without having to first run the macro manually in imageJ. Currently the macro prompts the user for the input and output directories and then does it's thing. I'm thinking there must be a function in R that will allow me to specify the macro and the input and output directories (I could then recode these variables in the macro to somehow take these arguments through the R script?)

I gather that I can use the system() command and found this tantalizing clue from somewhere else on the web:

system("./JavaApplicationStub  directory file[i] -batch zmacro") 

but I am not sure how to implement this (my macro already utilizes batch processing so that part would be unnecessary).

nograpes
  • 17,804
  • 1
  • 39
  • 62
ACG
  • 139
  • 1
  • 7
  • I am unfamiliar with imageJ, but it appears that it has a [command-line interface](http://rsbweb.nih.gov/ij/docs/guide/146-18.html) (CLI). Try getting it to work with the CLI for some arbitrary input and output directory. Once you have that, you will be able to construct the commands (substituting in whatever directory you want) within R. – nograpes Sep 24 '13 at 19:00
  • Indeed, looking closer, it appears that you can just cut and paste your macro code as the `eval` argument. – nograpes Sep 24 '13 at 19:02
  • Sorry, I'm missing something important. The `eval` argument where? (thanks for helping, by the way). – ACG Sep 24 '13 at 19:12
  • Did you check out [the page I linked](http://rsbweb.nih.gov/ij/docs/guide/146-18.html)? It lists an argument: `-eval"macrocode" Evaluates macro code. Examples:` – nograpes Sep 24 '13 at 19:18
  • I know how to run the macro in imageJ, but I am still missing the link of how to run the macro code through R. – ACG Sep 24 '13 at 20:14
  • First, you have to learn to run your imageJ macro at the command line. Then, you can run it through R, by calling `system`, which just runs something at the command line. The link I pointed to shows how to run imageJ at the command line. – nograpes Sep 24 '13 at 20:52
  • Also, you could check out the old package [RImageJ](http://romainfrancois.blog.free.fr/index.php?category/R-package/RImageJ) that is no longer being maintained. – nograpes Sep 24 '13 at 21:02
  • I tried RImageJ, had no luck. I think you solved my problem though - sorry for making you spell it out - sadly still fumbling my way through all of this even after a fair number of years. Thanks a ton! – ACG Sep 24 '13 at 23:29
  • No problem. If you figured out how to do this, you can either accept the answer below, or you can post (and accept) your own answer so that others can learn how to do this. – nograpes Sep 25 '13 at 07:55

3 Answers3

3

Thanks to both nograpes & Kota (and more google searching) the problem is solved.

To call an imageJ macro through R is as follows from Kota:

system("/Applications/ImageJ/ImageJ.app/Contents/MacOS/JavaApplicationStub -batch 
/Users/xxxx/Desktop/testmacro.txt")

The particular macro I am using requires both input and output directories. To code this in R, I added an argument onto the system call:

system("/Applications/ImageJ/ImageJ.app/Contents/MacOS/JavaApplicationStub -batch 
/Users/acgerstein/Desktop/testmacrobatch.txt 
/Users/acgerstein/Desktop/130829Pos_24h/*/Users/acgerstein/Desktop/temp/")

As far as I can tell imageJ only supports one argument being passed in. So I separated my input directory and output directories by " * ".

The code in imageJ then looks like this:

folders = getArgument;
delimiter = "*";
parts=split(folders, delimiter);
dir1 = parts[0];
dir2 = parts[1];

The nicest slightly unexpected thing is that the log files that are usually printed through the macro in imageJ now show up in the R console.

Mischief managed.

ACG
  • 139
  • 1
  • 7
2

Here is an example to run a macro file "test.ijm" from CL (in osx). You could probably wrap this command in R (not tested). The path to the macro file should be a full path, not a relative path.

/Applications/ImageJ/ImageJ.app/Contents/MacOS/JavaApplicationStub -batchpath /tmp/test.ijm
Kota
  • 181
  • 3
1

Have you tried Bio7? It is a distribution of ImageJ, embedded in an Eclipse RCP application, which features lots of great R integration using Rserve.

For what it's worth, we are also working on R scripting integration in ImageJ2.

ctrueden
  • 6,394
  • 3
  • 32
  • 64