How can I call/execute an imageJ macro with R?
Asked Answered
A

3

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).

Abduction answered 24/9, 2013 at 18:52 Comment(9)
I am unfamiliar with imageJ, but it appears that it has a command-line interface (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.Protohuman
Indeed, looking closer, it appears that you can just cut and paste your macro code as the eval argument.Protohuman
Sorry, I'm missing something important. The eval argument where? (thanks for helping, by the way).Abduction
Did you check out the page I linked? It lists an argument: -eval"macrocode" Evaluates macro code. Examples:Protohuman
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.Abduction
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.Protohuman
Also, you could check out the old package RImageJ that is no longer being maintained.Protohuman
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!Abduction
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.Protohuman
A
4

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.

Abduction answered 25/9, 2013 at 16:46 Comment(0)
B
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
Betsey answered 25/9, 2013 at 0:18 Comment(0)
J
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.

Jenjena answered 26/9, 2013 at 16:58 Comment(1)
I've seen Bio7 but really wanted to stay within the R console environment.Abduction

© 2022 - 2024 — McMap. All rights reserved.