Inkscape bridge extensions for ImageJ/R/Processing

Dear all,
With Martin Owens, we’ve created Inkscape extensions that can request figure panels from ImageJ, R or Processing code that are supposed to help with making figure content more flexible and reproducible. I’ve put some slides here:

See this thread for a few examples:

Jerome.

Success reports on different systems:
OsX: 10.11.6; 10.15; 10.17 (Inkscape1.1-dev, Inkscape1.1rc)
Ubuntu 20.04 (Inkscape 1.2-dev)

15 Likes

For those who’re looking for how to use the extensions, and for the source code: the repository is mentioned on the first slide (but doesn’t seem clickable on the f1000research embedded viewer).

Here’s the quick link:

3 Likes

Thanks @imagejan for the link.
Here is the installation details:

Requirements

Configuring the ImageJ link, this is what my setup info shows

Configuring the R link, this is what my setup info shows
image

And this is what my Processing setup info looks like

Hello @jerome.

This is very nice.

Since I can also create R plots with ImageJ images in Inkscape by changing the R plot device i would see a benefit in also transferring the R (or IJ macro) code directly to Inkscape to make a reproducible self running Inkscape instance with your plugin (just like rmarkdown).

Is there an API (command line args at Inkscape startup) available to transfer the code to your panels?

Thanks Marcel,
Yes Bio7 already has such a good IJ/R integration!

The panel code is just part of the description node of the object so the easiest I could think of woul be to copy valid svg in the clip board in bio7 and paste it in inkscape.


Then the code is simply wrapped into a template and passed to RScript, see:

Inkscape also has a command line interface that definitely makes it possible to modify an existing svg document, but, to my limited knowledge, not modify the currently open document.
https://wiki.inkscape.org/wiki/index.php/Using_the_Command_Line

1 Like

Thanks for the explanation @jerome

I think this becomes easier than I thought in R by simply saving an *.svg template with the R code as description:

And thanks to the new Raw String literals in Java 15 to write *svg in Java.

2 Likes

Hello @jerome,

this is my first Bio7 script attempt for your plugin. A simple Groovy script which writes the XML with the Object description (the R code from the current opened Bio7 R editor).

In addition it writes the current R workspace to the *svg file location (for images as vector or matrices) and adds the load statement as the first line:

/*
Export script for R code to *.svg!
*/
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.swt.widgets.Display;
import com.eco.bio7.util.Util;
import com.eco.bio7.reditors.REditor;

Display display = Util.getDisplay();
display.asyncExec(new Runnable() {
	public void run() {
		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if (editor instanceof REditor) {
			String path = Bio7Dialog.saveFile("*.svg");
			String pathRData = path.replace(".svg", ".RData").replace("\\", "/");
			if (RServe.isAlive()) {
				RServeUtil.evalR("save(file=\"" + pathRData + "\")", null);
			}
			else{
				System.out.println("Rserve not running. R workspace not saved!");
			}
			IDocumentProvider dp = editor.getDocumentProvider();
			IDocument doc = dp.getDocument(editor.getEditorInput());
			String rCode = doc.get();
			String svgTemplate = svgString(pathRData, rCode);
			//System.out.println(svgTemplate);
			if (path != null) {
				File fil = new File(path);
				FileWriter fileWriter = null;
				try {
					fileWriter = new FileWriter(fil);
					BufferedWriter buffWriter = new BufferedWriter(fileWriter);
					buffWriter.write(svgTemplate, 0, svgTemplate.length());
					buffWriter.close();
				} catch (IOException ex) {

				} finally {
					try {
						fileWriter.close();
					} catch (IOException ex) {

					}
				}
			}
		} else {
			Bio7Dialog.message("Please open a R file!");
		}

	}

});

public String svgString(pathRData, rCode){
String svgTemplate = """
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="210mm" height="297mm" viewBox="0 0 210 297"
	version="1.1" id="svg5" xmlns="http://www.w3.org/2000/svg"
	xmlns:svg="http://www.w3.org/2000/svg">
	<defs id="defs2" />
	<g id="layer1" transform="matrix(1,0,0,1.0000046,0,-1.1750488e-4)">
		<desc id="desc9">load(file='"""+pathRData+"')\n"+rCode+"""
		</desc>
		<rect style="fill:#f9f9f9;stroke-width:0.264583" id="rect31"
			width="104.66726" height="63.756706" x="19.658318" y="25.502682">
			<desc id="desc113">#RCode</desc>
		</rect>
	</g>
</svg>
""";
return 	svgTemplate;
}

Resulted Inkscape (development version):

1 Like

Marcel!
That looks excellent, it’s all that’s needed to have this rect rendered, and your svg file can also be imported into, say, another figure in progress and keep its runnable code.
:star_struck: :star_struck:

It seems excellent, but I have issue with the installation. I work on Mac OS Big Sur with Anaconda distribution installed.
My issue is the following: when installing Extension Manager, I have an error from Inkscape that claims that virtualenv-python is not installed. I don’t know how to fix it.

There’s a lot of ongoing work for the extension manager (Activity · Inkscape / Inkscape Extras / extension-manager · GitLab).
In the meantime you don’t really need it, you can simply follow the installation steps and requirements at Martin Owens / inkscape-imagej-panel · GitLab

I have only tested the whole thing on 10.11 el capitan and 10.15 catalina, not on bigsur, so I would be interested in your feedback.

Jerome.

OK, I installed the extensions in the user extension folder, and it works now.
And It works on BigSur

1 Like

Hello @Jerome, just one thing I realized. When saving the XML embedded text it’s the pure editor text.

Inkscape however seems to load the XML-Text for the description.

In the sceenshot I sent you see that the < is not displayed. In the Inkscape panel it must be given as &lt;
So some conversions needs to be done:

http://xml.silmaril.ie/specials.html

Or there is an easier way to save the text for the Inkscape object description.

Hi Marcel,

Yes xml special characters are escaped in the svg as shown in the attached screenshots.
I found this article that shows an easy way of doing this from java: Escaping XML Special Characters in Java String - GeeksforGeeks

J.

ImageJ macro code snippet in inkscape descripion:

How it is saved in the svg file:

Thanks Eric, great to see you got it working.
Jerome.

Thank @jerome I already replaced the Strings:

/*XML replacements!*/
rCode = rCode.replace("&", "&#38;");
rCode = rCode.replace("<", "&#60;");
rCode = rCode.replace(">", "&#62;");
rCode = rCode.replace("\"", "&#34;");
rCode = rCode.replace("'", "&#39;");

I also had to change the XML template. But now it is running fine after a correct installation.

Result (with last loop plot only):

The only thing I noticed is that a *.png file is embedded. I think it would be nice if a *.svg could be optionally used for R plots which can be easily exported with the R device.

1 Like

Excellent, thanks for the screenshot!
I agree that svg should be an option and it’s definitely possible.

Jerome.