I am running R studio with R version 3.4.1 and java version 1.8.0_131
I am trying to run a function I created and I am getting these errors:
I get this error when I run the specific function
java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
I get this error when I run the entire script
Error in ._jobjRef_dollar(x[["jobj"]], name) :
no field, method or inner class called '.when'
I am wondering if anyone may have an idea as to what is causing these errors, before I post the function. It is pretty long. It is essentially opening multiple files and reading them as tables. Each path is correct and it works individually.
It looks like you are trying to use .when somewhere in your script, however there is no such method for .when.
The issue could be that one of your libraries (R?) is an older version than the documentation you are using, or the .when call never existed in the first place.
The easiest way to fix this is to search and find out where .when is being called in your code, and then check if there is another method you can use instead, or if you just need to update to a newer library.
Add a bit of debugging code to print to console between opening each file and between reading them to the tables, that way you can at least see how far along your code gets before it fails.
For reference on where the error is called, you can take a look at the source for rJava here:
https://github.com/cran/rJava/blob/master/R/reflection.R
Specifically note your error in the last else block when rJave attempts to look for a function, but cannot find it:
### syntactic sugar to allow object$field and object$methods(...)
### first attempts to find a field of that name and then a method
._jobjRef_dollar <- function(x, name) {
if (hasField(x, name) ){
.jfield(x, , name)
} else if( hasJavaMethod( x, name ) ) {
function(...) .jrcall(x, name, ...)
} else if( hasClass(x, name) ) {
cl <- .jcall( x, "Ljava/lang/Class;", "getClass" )
inner.cl <- .jcall( "RJavaTools", "Ljava/lang/Class;", "getClass", cl, name, FALSE )
new("jclassName", name=.jcall(inner.cl, "S", "getName"), jobj=inner.cl)
} else if( is.character(name) && length(name) == 1L && name == "length" && isJavaArray(x) ){
length( x )
} else {
stop( sprintf( "no field, method or inner class called '%s' ", name ) )
}
}
Related
I have a problem with saving .csv files by multi parts. In this part of the code:
#Override
public String completeMultipartUpload(MultipartUpload mpu, List<MultipartPart> parts) {
ImmutableList.Builder<Blob> blobs = ImmutableList.builder();
long contentLength = 0;
Hasher md5Hasher = Hashing.md5().newHasher();
for (MultipartPart part : parts) {
Blob blobPart = getBlob(mpu.containerName(), MULTIPART_PREFIX + mpu.id() + "-" + mpu.blobName() + "-" + part.partNumber());
contentLength += blobPart.getMetadata().getContentMetadata().getContentLength();
blobs.add(blobPart);
md5Hasher.putBytes(BaseEncoding.base16().lowerCase().decode(blobPart.getMetadata().getETag()));
}
The problem occurs on my computer, the program works for other people. Namely, for a given part of the code, blobPart.getMetadata().GetETag() always for the first 9 parts work fine. And for part 10 and each subsequent part does return a null value for ETag, which then causes an error NullPointerException.
All List<MultipartPart> parts are adding in the same way. All have correct property.
I think it is problem with my Environment. I tried:
reinstall IntelliJ
reinstall Java/use a different version
remove all libraries and install again
Or maybe something with encoding?
Other people for which it works are from another country, and I can see there is BaseEncoding.base16()?
StackTrace:
java.lang.NullPointerException: null
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:878)
at com.google.common.io.BaseEncoding$StandardBaseEncoding.trimTrailingPadding(BaseEncoding.java:676)
at com.google.common.io.BaseEncoding.decodeChecked(BaseEncoding.java:231)
at com.google.common.io.BaseEncoding.decode(BaseEncoding.java:217)
at org.jclouds.blobstore.config.LocalBlobStore.completeMultipartUpload(LocalBlobStore.java:843)
I hit perfectly the limit of the path length for windows, it threw an error because the path passed to
UserDefinedFileAttributeView view = getUserDefinedFileAttributeView(file.toPath()); it was too long.
What am I doing?
I am writing a data analysis program in Java which relies on R´s arulesViz library to mine association rules.
What do I want?
My purpose is to store the rules in a String variable in Java so that I can process them later.
How does it work?
The code works using a combination of String.format and eval Java and RJava instructions respectively, being its behavior summarized as:
Given properly formatted Java data structures, creates a data frame in R.
Formats the recently created data frame into a transaction list using the arules library.
Runs the apriori algorithm with the transaction list and some necessary values passed as parameter.
Reorders the generated association rules.
Given that the association rules cannot be printed, they are written to the standard output with R´s write method, capture the output and store it in a variable. We have converted the association rules into a string variable.
We return the string.
The code is the following:
// Step 1
Rutils.rengine.eval("dataFrame <- data.frame(as.factor(c(\"Red\", \"Blue\", \"Yellow\", \"Blue\", \"Yellow\")), as.factor(c(\"Big\", \"Small\", \"Small\", \"Big\", \"Tiny\")), as.factor(c(\"Heavy\", \"Light\", \"Light\", \"Heavy\", \"Heavy\")))");
//Step 2
Rutils.rengine.eval("transList <- as(dataFrame, 'transactions')");
//Step 3
Rutils.rengine.eval(String.format("info <- apriori(transList, parameter = list(supp = %f, conf = %f, maxlen = 2))", supportThreshold, confidenceThreshold));
// Step 4
Rutils.rengine.eval("orderedRules <- sort(info, by = c('count', 'lift'), order = FALSE)");
// Step 5
REXP res = Rutils.rengine.eval("rulesAsString <- paste(capture.output(write(orderedRules, file = stdout(), sep = ',', quote = TRUE, row.names = FALSE, col.names = FALSE)), collapse='\n')");
// Step 6
return res.asString().replaceAll("'", "");
What´s wrong?
Running the code in Linux Will work perfectly, but when I try to run it in Windows, I get the following error referring to the return line:
Exception in thread "main" java.lang.NullPointerException
This is a common error I have whenever the R code generates a null result and passes it to Java. There´s no way to syntax check the R code inside Java, so whenever it´s wrong, this error message appears.
However, when I run the R code in brackets in the R command line in Windows, it works flawlessly, so both the syntax and the data flow are OK.
Technical information
In Linux, I am using R with OpenJDK 10.
In Windows, I am currently using Oracle´s latest JDK release, but trying to run the program with OpenJDK 12 for Windows does not solve anything.
Everything is 64 bits.
The IDE used in both operating systems is IntelliJ IDEA 2019.
Screenshots
Linux run configuration:
Windows run configuration:
When I call eval (in strict mode) on a nashorn engine with the following script I get an exception:
var yfunc = function () {
(null).apply(null, arguments);
};
yfunc();
I've truncated my personal situation heavily. The "(null)" on line 2 can be replaced with anything between parenthesis or a local variable, either way just something that shouldn't throw a compile error, and it will yield the same result.
The issue seems to be explicitly that "arguments" is passed directly as the second argument of calling a method called "apply". Any of the following changes will undo the thrown exception:
Putting "arguments" in a variable first (but simply wrapping it in parenthesis doesn't work!)
Calling something other than apply
Passing "arguments" in a different argument slot when calling apply
Calling print() (with or without passing any arguments) as a preceding line of code inside yfunc() (weird huh?)
Defining more than 0 parameters for yfunc()
Binding yfunc first and then calling the bound method
Calling yfunc via Function.apply (not so much with Function.call!)
The Exception thrown is this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.runtime.Undefined to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:4)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
When I call this method with an owner, the exception thrown changes. Example code:
var yfunc = {
method: function () {
(null).apply(null, arguments);
}
};
var x = yfunc.method();
Then the thrown exception looks like this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO4 to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:5)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
I've reproduced the issue so far on specifically these environments:
windows 7 64bit -> jdk 1.8.0_60 64bit
windows 8 64bit -> jdk 1.8.0_131 64bit
I can't seem to find anything on the internet about similar issues. Do I need to report this to Oracle/OpenJDK?
Minor update
Added items 6 and 7 to list of "following changes will undo the thrown exception".
Final update
Bug filed: JDK-8184720
Yes, it appears to be a bug. Please file a bug.
I'm running into a very curious problem. I've an R code which doesn't contain any print statement (except my explicit calls to log the time taken) and yet the entire json gets dumped on to the "R console". This is causing a serious performance issue with our module and I need your help to track down the problem.
Here is just a part of the R file (due to company policies, I cannot post the entire source code and I apologize for not giving much info)
#run time/online time series models
LAST_TIME_ID <- DATA[nrow(DATA),id];
LAST_TIME_ID <- strptime(LAST_TIME_ID,format="%d-%m-%Y %H:%M");
#timestamp tag computation using frequency
FREQUENCY_VEC <- rep(TIMESTAMP_FREQUENCY*60,PREDICTION_NUMBER);
FREQUENCY_VEC <- cumsum(FREQUENCY_VEC);
TIMESTAMP_TAGS <- LAST_TIME_ID + FREQUENCY_VEC;
TIMESTAMP_TAGS <- format(strptime(TIMESTAMP_TAGS,format="%Y-%m-%d %H:%M"),format="%d-%m-%Y %H:%M");
#prepare the prediction points data per tag into table format
PREDICTION_DATA <- NULL;
startTime <- Sys.time();
for (tag_index in 1:length(MODEL[,tag_id])) {
TEMP <- data.table(id=as.character(TIMESTAMP_TAGS),tag_id = MODEL[tag_index,tag_id], prediction = as.vector(MODEL[,Forecast][[tag_index]]));
PREDICTION_DATA <- data.table(rbind(PREDICTION_DATA,TEMP));
rm(TEMP);
};
endTime <- Sys.time();
print(paste("seconds consumed (prediction points data per tag into TEMP): ",(endTime-startTime)/1000));
#OUTPUT <- dcast(PREDICTION_DATA,id~tag_id); #into output
OUTPUT <- PREDICTION_DATA;
#compute final output in json format
js_object <- toJSON(OUTPUT,asIs = TRUE);
js_object;
I can assure you the rest of code looks the same (i.e. no prints). I'm running my R code through Java (1.8) using RServe (REngine.jar) on Windows 8.
Any ideas/clues would be greatly appreciated.
The last line of your choice snippet where you just execute:
js_object;
prints the variable to the console.
Remove such type of statements where nothing assigned to a variable.
When you type the js_object file name at the end, you are calling that object and R is printing the contents to the console. All you need to do is remove that an it should stop printing it out.
I am preparing an R wrapper for a java code that I didn't write myself (and in fact I don't know java). I am trying to use rJava for the first time and I am struggling to get the .jcall right.
Here is an extract of the java code for which I write a wrapper:
public class Model4R{
[...cut...]
public String[][] runModel(String dir, String initFileName, String[] variableNames, int numSims) throws Exception {
[...cut...]
dir and initFileName are character strings for the directory and file name with initial conditions, variable names is a list of character strings that I would write like this in R: c("var1", "var2", "var3", ...) and can be of length from one to five. Finally, numSim is an integer.
Here is my tentative R code for a wrapper function:
runmodel <- function(dir, inFile, varNames, numSim){
hjw <- .jnew("Model4R")
out <- .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir), as.character(inFile), as.vector(varNames), as.integer(numSim))
return(out)
}
The error in R is:
Error in .jcall(hjw, "[[Ljava/lang/String", "runModel", as.character(dir),
: method runModel with signature (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)[[Ljava/lang/String not found
I suspect that the JNI type isn't correct for String[][]. Anyhow, any help that could direct me towards a solution would be welcome!
You're missing a semicolon at the end of the JNI for String[][] - it should be "[[Ljava/lang/String;". Also, I think you need to call .jarray instead of as.vector on varNames. The R error is telling you that rJava thinks the class of the third argument is Ljava/lang/String; instead of [Ljava/lang/String;.