How does Knime call R functions using R plugin?

Hi to all,

I'm trying to develop my personal node which uses R functions.

 I know that there is a local R version in knime's plugins folder, but I don't know how I can call and use it.

I guess Knime uses a BundleContext object in RCorePlugin class, but I still don't understand how.

Best regards

 

Lato

You can call org.knime.ext.r.RCorePlugin.getRExecutable() to get the path to a bundled R executable. But be aware that for Linux there is no bundled R. In order to access the R path from the preferences page use org.knime.ext.r.preferences.RPreferenceInitializer.getRProvider().getRPath().

Thank you very much.

Hi!
I’m having a similar issue with the development of a R node.
As you suggested, everything seems to work fine when you call org.knime.ext.r.RCorePlugin.getRExecutable. Then I copied the source code of RCorePlugin in my NodePlugin class, so that my NodePlugin now is identical to RCorePlugin, save for some minor adjustments (changing the package name, etc…). Nonetheless, in this way it can’t find the R executable. Why?

It's hard to say, you probably need to make some debugging (especially the RCorePlugin#start method) where the KNIME executable is set.

Ok. Thanks! I'll try!

In the meantime, may I post here a snippet of my RCorePlugin#start method?

public void start(final BundleContext context) throws Exception {
        super.start(context);

        // This is the only line I changed, so that my NodeFactory is loaded instead of RScriptingNodeFactory.class
        NodeFactory.addLoadedFactory(Test_1NodeFactory.class);

        Bundle bundle = context.getBundle();
        Enumeration<URL> e = bundle.findEntries("/R-Inst/bin", "R.exe", true);
        URL url = null;
        if ((e != null) && e.hasMoreElements()) {
            url = e.nextElement();
        } else {
            e = bundle.findEntries("/R-Inst/bin", "R", true);
            if ((e != null) && e.hasMoreElements()) {
                url = e.nextElement();
            }
        }
        // default path on linux systems (no R binary plugin available)
        String[] searchPaths = {"/usr/bin/R", "/usr/local/bin/R"};
        if (url == null) {
            for (String s : searchPaths) {
                File f = new File(s);
                if (f.canExecute()) {
                    url = f.toURI().toURL();
                    break;
                }
            }
        }
        if (url != null) {
            try {
                rExecutable = new File(FileLocator.toFileURL(url).getFile());
            } catch (Exception ex) {
                LOGGER.info("Could not locate default R executable", ex);
            }
        }
    }

 

What exactly are you trying to achieve? Why don't you use the function for getting the R path from our R plug-in? Copying code is in most cases a bad idea. Also, you can safely ignore (and in fact remove) the addLoadedFactory-call. This is only present in our plug-in for legacy reasons.

I am trying to develop my own function for getting the R path for 3 reasons:
a) It seems that using RCorePlugin.getRExecutable() only works when the R extension is installed. I’m not sure about it (it seems quite odd, I know), but all my tests gave the same result. Obviously, in every test a R executable had been previously installed.
b) I would prefer to have an instance of the getRExecutable method in my node: I am planning to modify / personalize it.
c) Curiosity! Why the same code gives two different outputs? There must be something in the R Node mechanism that can explain it, but I can’t get it.

Our code looks for the R binary in a fragment that belongs to the R plug-in. You won't be able to find these binaries from another plug-in without changing the code. The "bundle" in "bundle.findEntries(...)" must be the org.knime.ext.r bundle and not yours. You can get references to other bundles using Bundle.getBundle(name).