Problem loading R own created libraries in Java/JRI code - java

I created my own new R library (called "Media"). There is no problem when I try to load it with RGui, and I can call the functions defined in the new package. This is how I load it:
> library(Media)
But, I'm also trying to call that functions from Java/JRI code, and when I load the new R package, Java doesn't seem to find the pacakge, throwing the message "Error in library(Media) : object 'Media' not found"
This is my current code using JRI:
REXP rexpSetFolder = re.eval("setwd('C:/Users/Albert/Documents')");
REXP rexpFolder = re.eval("getwd()");
System.out.println(rexpFolder.asString());
REXP rexpLoad = re.eval("library(Media)"); // fails
It also fails without the 'setwd' command, and simple calls to existing R functions work fine. I'm using R 2.10 and the latest JRI 0.5-0 under Windows.
Any help would be appreciated.
Thank you very much.
Edit:
The parameter lib.loc seems to work, at least this sentence does not return an error:
library("Media", lib.loc = "c:/Users/Albert/Documents")
But after that, calling a function in the package with re.eval("myfunction()"); still fails, as the function is not properly found.

You can modify the library path - see ?.libPaths in R, you simply want to add your private library to the path. The GUI does that for you, but if you are outside it doesn't happen. For example:
re.eval(".libPaths('c:/users/foo/Documents/R')");
Then load your package.

Did you install the library properly first? You might want to try using the lib.loc parameter.
library("Media", lib.loc = "c:/Users/Albert/Documents")

My work-around was to copy the package from my personal library (%USERPROFILE%\Documents\R) to the global library (%R_HOME%\library).
It's not the best because this requires Administrator privileges which not all users will have...

Related

Whats the syntax of making a string value generic inside something.execute(String[],String) with groovy inside build.gradle file?

Below is the command cmd that is going to get executed and is working fine.
def process = cmd.execute(['PATH=D:/Project/Node_Project/build/nodejs/node-v10.10.0-win-x64'],null)
"D:/Project/Node_Project" is my project root folder.
I am trying something like below to make the path generic but it didn't work. ${project.buildDir} also didn't work.
def process = cmd.execute(['PATH=${project.projectDir}/build/nodejs/node-v10.10.0-win-x64'],null)
I want to know what is the correct format to make it generic.
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/String.html#execute(java.lang.String[],%20java.io.File)
The above link contains the documentation for the syntax only but no examples to solve my problem.

Can't import model from MPS file to IloCplex - IBM ILOG CPLEX - Java - Intellij

I'm currently developing a project in java using the Ilog Cplex libraries, I'm using the Intellij-Idea IDE.
I'm having troubles importing a model from MPS file, this is the piece of code that gives me problems
IloCplex iloCplexInstance = new IloCplex();
iloCplexInstance.importModel(fileName);
It fires this exception:
ilog.cplex.CpxException: CPLEX Error 1423: Could not open file 'models\20_70_45_05_100.mps' for reading.
at ilog.cplex.CplexI.CALL(CplexI.java:5204)
at ilog.cplex.CplexI._readModel(CplexI.java:5584)
at ilog.cplex.CplexI.importModel(CplexI.java:1032)
at ilog.cplex.IloCplex.importModel(IloCplex.java:902)
at heuristics.ziround.LPUtils.fromMPS(LPUtils.java:34)
at heuristics.test.LPUtilsTestCompile.main(LPUtilsTestCompile.java:13)
I tried running it in unit tests using junit4, junit.runners.Parameterized, and in a simple class from its main method. Same result in each case. I've also tried to set the full path to the file and it gives the same result.
I know that the file I used is ok, I'm able to read it using the cplex terminal commands, I also tried other files.
The code that uses the Ilog's libraries can compile, I'm not sure if it can run though since I'm not able to import a model I can't try to solve one.
I'm using windows, launching the IDE as admin sorts no effects, and the file is not blocked from reading (nor writing).
I'm following the documentation from IBM:
https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.0/ilog.odms.cplex.help/refjavacplex/html/ilog/cplex/IloCplex.html#importModel(java.lang.String)
In the official support pages, I found this about the error: http://www-eio.upc.es/lceio/manuals/cplex-11/html/refcallablelibrary/html/macros/CPXERR_FAIL_OPEN_READ.html
But I can't find anything useful.
Also, the IBM forum is currently closed and no one seems to have had this kind of problem :(
Does anyone know what could be the trouble? What can I do? Do you know of any other alternative?
Thanks to anyone that will stop by!!
The exception message says:
ilog.cplex.CpxException: CPLEX Error 1423: Could not open file
'models\20_70_45_05_100.mps' for reading.
Try using an absolute path instead, like c:\path\to\your\models\20_70_45_05_100.mps.
You can also add code to your program to make sure the path exists. Something like:
import java.nio.file.Files;
...
IloCplex iloCplexInstance = new IloCplex();
if (!Files.exists(fileName))
throw new AssertionError("path does not exist: " + fileName);
iloCplexInstance.importModel(fileName);
Following #rkersh answer i did this:
String modelsPath = "absolute\\folder\\path";
Collection<Object[]> models = new ArrayList<>();
File folder = new File(modelsPath);
for (final File fileEntry : Objects.requireNonNull(folder.listFiles())) {
if (fileEntry.isFile())
models.add(new String[]{fileEntry.getAbsolutePath()});
}
return models;
This makes sure that the absolute path is correct and now iloCplexInstance.importModel(fileName); accepts it fine

UnsopportedOperationException TODO when running R script with renjin in java

I'm new to R and Renjin, but I'm trying to run a legacy R script from a spring boot service and I'm getting this Unsupported exception. I've narrowed down the issue on the legacy script to a call made to svymean R function.
I've added details in here https://github.com/bedatadriven/renjin/issues/494
The design is generated with:
svrepdesign(data = svy_data, type = "JK1", weights = ~ weights, repweights = "weights[1-9]+")
What I've tried so far is:
Adding dependencies that could have been missing (matrix, base, stats), this didn't work and in some cases it only generated more problems
Commenting the parameters I'm passing in one by one, this didn't make any difference, the error still appears
I also tried with another similar function svytotal and I'm getting the same error
Could it be that it's just not supported to calculate the mean with replicated weighted? And if so, there is another way to calculate this and still use renjin? And if not, any other recommendation to use instead of renjin to run R legacy code?
The same script with the same parameters is running just fine from within RStudio.

Using external java library in KNIME. Why initialization of one class fails while it succeeds for another class?

I have a problem integrating Java code into KNIME. Similar posts on Knime forum (http://tech.knime.org/forum/knime-general/using-external-jar-in-java-snippet-node-workflow-not-able-to-initialize-class-of) were of little help and I also posted a question there but have not got answer so far, so I'm trying my luck here.
I am trying to integrate my code into KNIME workflow using JavaSnippet. I have exported the code into a jar and put it into the KNIME jre/lib/endorsed folder. The code references CDK 1.4.19 and I have also placed the corresponding jar file into the same directory. I do not have CDK node extensions installed in KNIME and using them is also not an option in my case.
The code starts with:
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
SmilesParser sp= new SmilesParser(builder);
When I try to execute JavaSnippet I get the following exception message:
Evaluation of java snippet failed for row "Row0". Exception message: Could not initialize class org.openscience.cdk.smiles.SmilesParser
When I just try
IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
It works and I get no exception message. I have checked, the builder is not a null. However, when I try to initialize SmilesParser, it fails. This class is public. It has no default constructor and has one public constructor that takes IChemObjectBuilder as a parameter.
I have tried to use class loader:
URL[] classLoaderUrls = new URL[]{new URL("file:///path on my computer/knime_2.9.4/jre/lib/endorsed/cdk-1.4.19.jar")};
URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);
Class<?> parserClass = urlClassLoader.loadClass("org.openscience.cdk.smiles.SmilesParser");
Class[] classParameters = new Class[] {IChemObjectBuilder.class};
Constructor<?> constructor = parserClass.getConstructor(classParameters); //until this line there are no problems
Object parser = constructor.newInstance(builder); //fails here with the same exception message: Could not initialize class org.openscience.cdk.smiles.SmilesParser
I am sure that this is not a CDK error because I can execute the code in Eclipse.
Why can a constructor of one class be called from KNIME without any problems and a constructor of another class can not??
I would be very grateful if you could suggest a solution or a probable reason why this happens.
Thank you!
Ok, finally solved the problem myself. Cleaned project meta data, cleaned endorsed library directory, switched KNIME workspace to another directory, put all jars into one folder and added them as external libraries. Now it works:)

Error debugging CGLIB FastClass on eclipse

I'm trying to debug an java eclipse project with some problem!
I'm starting using CGLIB to make faster reflection calls using the index metod.
example
FastClass fastClass = FastClass.create(getClass());
int index = fastClass.getIndex("methodName", new Class[] { Object.class });
fastClass.invoke(index, this, new Object[] { obj } );
now when i try to put a breakpoint into a class that is called by fastreflection method this is the eclipse output.
I try to change compiler option on generate line number with no results.
I also upload an eclipse project (built with Juno version) that replicates the problem!!
http://www.filefactory.com/file/4zryz3gjgbyh/n/FastDebug.rar
Thanks!
I "resolved"(understand) the problem, but it is not a problem with Eclipse. When you launch
the program this line: FastClass.create(ReflectionTarget.class); ends up
creating an entirely new version of the compiled class removing all
non-essential stuff from the classfile to make it "fast" - that includes all
the line number / debug infos, which means the breakpoint cannot be set in it.
http://cglib.sourceforge.net/xref/net/sf/cglib/core/package-summary.html
There's no javadoc and you need to read the source but now i understand this is not a
problem but a feature of this method to make fast reflection!

Categories