R, Java, RCaller - java

So i'm trying to use RCaller to do the following (in psuedo-code):
x=simarray(..) // in java
'send x to R'
run y = rlogcon(40000, sort(x)) //in R, rlogcon is part of rlogcondens and the function
produces a numeric vector of length 40000
//send y to java and hold it in a double matrix.
this is what i've got in my main function (i've adapted one of the examples I found, but I must have made a mistake/misunderstood something. I tested the example on my machine, and it worked as expected):
RCaller caller = new RCaller();
RCode code = new RCode();
double[] x = a.simarraysimbeta(5, 1, 100, 30, 40); //some method
savesample("simarraysimbeta.txt",x); //testing, it works.
caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.1.0\\bin\\Rscript.exe");
code.addDoubleArray("X", x);
code.addRCode("library(logcondens)"); //rlogcon is part of logcondens library
code.addRCode("ols <- rlogcon(40,000, sort(X))");
caller.setRCode(code);
caller.runAndReturnResult("ols");
double[] residuals = caller.getParser().getAsDoubleArray("X_star");
and the following relevent imports:
import rcaller.RCaller;
import rcaller.RCode;
import rcaller;
I get the following error:
Exception in thread "main" rcaller.exception.ParseException: Can not
handle R results due to : rcaller.exception.ParseException: Can not
parse output: The generated file
C:\Users\jo\AppData\Local\Temp\Routput8804446513483695061 is empty
here is the output:
concat<-function(to,...){
to<-paste(to,toString(...),sep="");
return(to);
}
cleanNames<-function(names){
cleanNames<-paste(unlist(strsplit(names,"\\.")),collapse="_");
Exception in thread "main" rcaller.exception.ParseException: Can not
handle R results due to : rcaller.exception.ParseException: Can not
parse output: The generated file
C:\Users\jo\AppData\Local\Temp\Routput8804446513483695061 is empty
cleanNames<-paste(unlist(strsplit(cleanNames,"<")),collapse="");
at rcaller.RCaller.runAndReturnResult(RCaller.java:409)
cleanNames<-paste(unlist(strsplit(cleanNames,">")),collapse="");
at dissertation.Dissertation.main(Dissertation.java:709)
cleanNames<-paste(unlist(strsplit(cleanNames," ")),collapse="");
cleanNames<-paste(unlist(strsplit(cleanNames,"\\(")),collapse="");
cleanNames<-paste(unlist(strsplit(cleanNames,"\\)")),collapse="");
cleanNames<-paste(unlist(strsplit(cleanNames,"\\[")),collapse="");
cleanNames<-paste(unlist(strsplit(cleanNames,"\\]")),collapse="");
cleanNames<-paste(unlist(strsplit(cleanNames,"\\*")),collapse="");
return(cleanNames);
}
makevectorxml<-function(code,objt,name=""){
xmlcode<-code;
if(name==""){
varname<-cleanNames(deparse(substitute(obj)));
}else{
varname<-name;
}
obj<-objt;
n <- 0; m <- 0
mydim <- dim(obj)
if(!is.null(mydim)){
n <- mydim[1]; m <- mydim[2];
}else{
n <- length(obj); m <- 1;
}
if(is.matrix(obj)) obj<-as.vector(obj);
if(typeof(obj)=="language") obj<-toString(obj);
if(typeof(obj)=="logical") obj<-as.character(obj);
if(is.vector(obj) && is.numeric(obj)){
xmlcode<-paste(xmlcode,"<variable name=\"",varname,"\" type=\"numeric\" n=\"", n, "\" m=\"", m, "\">",sep="");
for (i in obj){
xmlcode<-paste(xmlcode,"<v>",sep="");
xmlcode<-paste(xmlcode,toString(i),sep="");
xmlcode<-paste(xmlcode,"</v>",sep="");
}
xmlcode<-paste(xmlcode,"</variable>\n",sep="");
}
if(is.vector(obj) && is.character(obj)){
xmlcode<-paste(xmlcode,"<variable name=\"",varname,"\" type=\"character\">\n",sep="");
for (i in obj){
xmlcode<-paste(xmlcode,"<v>",sep="");
xmlcode<-paste(xmlcode,toString(i),sep="");
xmlcode<-paste(xmlcode,"</v>",sep="");
}
xmlcode<-paste(xmlcode,"</variable>\n");
}
return(xmlcode);
}
makexml<-function(obj,name=""){
xmlcode<-"<?xml version=\"1.0\"?>\n";
xmlcode<-concat(xmlcode,"<root>\n");
if(!is.list(obj)){
xmlcode<-makevectorxml(xmlcode,obj,name);
}
else{
objnames<-names(obj);
for (i in 1:length(obj)){
xmlcode<-makevectorxml(xmlcode,obj[[i]],cleanNames(objnames[[i]]));
}
}
xmlcode<-concat(xmlcode,"</root>\n");
return(xmlcode);
}
X<-c(##100 doubles);
library(logcondens)
ols <- rlogcon(40,000, sort(X))
cat(makexml(obj=ols, name="ols"), file="C:/Users/##/AppData/Local/Temp/Routput8804446513483695061")

40,000 shold be 40000 in your code.

There are two ways in which a file remains empty:
there is an error in R code
the result is NULL or NA
I recommend you to use tryCach in R
ols <- tryCatch({
rlogcon(40,000, sort(X))
}, error = function(e) {
-1
})
And at the end if ols equlas -1, you'll know that there was an error in R

Related

java.nio.ByteBuffer wrap method partly working with sbt run

I have an issue where I read a bytestream from a big file ~ (100MB) and after some integers I get the value 0 (but only with sbt run ). When I hit the play button on IntelliJ I get the value I expected > 0.
My guess was that the environment is somehow different. But I could not spot the difference.
// DemoApp.scala
import java.nio.{ByteBuffer, ByteOrder}
object DemoApp extends App {
val inputStream = getClass.getResourceAsStream("/HandRanks.dat")
val handRanks = new Array[Byte](inputStream.available)
inputStream.read(handRanks)
inputStream.close()
def evalCard(value: Int) = {
val offset = value * 4
println("value: " + value)
println("offset: " + offset)
ByteBuffer.wrap(handRanks, offset, handRanks.length - offset).order(ByteOrder.LITTLE_ENDIAN).getInt
}
val cards: List[Int] = List(51, 45, 14, 2, 12, 28, 46)
def eval(cards: List[Int]): Unit = {
var p = 53
cards.foreach(card => {
println("p = " + evalCard(p))
p = evalCard(p + card)
})
println("result p: " + p);
}
eval(cards)
}
The HandRanks.dat can be found here: (I put it inside a directory called resources)
https://github.com/Robert-Nickel/scala-texas-holdem/blob/master/src/main/resources/HandRanks.dat
build.sbt is:
name := "LoadInts"
version := "0.1"
scalaVersion := "2.13.4"
On my windows machine I use sbt 1.4.6 with Oracle Java 11
You will see that the evalCard call will work 4 times but after the fifth time the return value is 0. It should be higher than 0, which it is when using IntelliJ's play button.
You are not reading a whole content. This
val handRanks = new Array[Byte](inputStream.available)
allocates only as much as InputStream buffer and then you read the amount in buffer with
inputStream.read(handRanks)
Depending of defaults you will process different amount but they will never be 100MB of data. For that you would have to read data into some structure in the loop (bad idea) or process it in chunks (with iterators, stream, etc).
import scala.util.Using
// Using will close the resource whether error happens or not
Using(getClass.getResourceAsStream("/HandRanks.dat")) { inputStream =>
def readChunk(): Option[Array[Byte]] = {
// can be done better, but that's not the point here
val buffer = new Array[Byte](inputStream.available)
val bytesRead = inputStream.read(buffer)
if (bytesRead >= 0) Some(buffer.take(bytesRead))
else None
}
#tailrec def process(): Unit = {
readChunk() match {
case Some(chunk) =>
// do something
process()
case None =>
// nothing to do - EOF reached
}
}
process()
}

R Integrate with Java using Rserve

I have build an application connecting R and java using the RServe package. In this project I use neuralnet to predict output. Where is the source code that I use are as follows:
myneuralnetscript=function(){
trainingData = read.csv("D:\\Kuliah\\Semester V\\TA\\Implementasi\\training.csv")
testingData = read.csv("D:\\Kuliah\\Semester V\\TA\\Implementasi\\testing.csv")
X1training <- trainingData$open
X2training <- trainingData$high
X3training <- trainingData$low
X4training <- trainingData$close
X5training <- trainingData$volume
targetTraining <- trainingData$target
X1testing <- testingData$open
X2testing <- testingData$high
X3testing <- testingData$low
X4testing <- testingData$close
X5testing <- testingData$volume
targetTesting <- testingData$target
xTraining <- cbind(X1training,X2training,X3training,X4training,X5training)
sum.trainingData <- data.frame(xTraining,targetTraining)
net.sum <- neuralnet(targetTraining~X1training+X2training+X3training+X4training+X5training, sum.trainingData, hidden=5,act.fct="logistic")
xTesting <- cbind(X1testing,X2testing,X3testing,X4testing,X5testing)
sum.testingData <- data.frame(xTesting,targetTesting)
result <- compute(net.sum,sum.testingData[,1:5])
return(result)
}
The output generated as follows:
Here the program from Java to access the results of the R.
public static void main(String[] args) {
RConnection connection = null;
try {
/* Create a connection to Rserve instance running on default port
* 6311
*/
connection = new RConnection();
//Directory of R script
connection.eval("source('D:\\\\Kuliah\\\\Semester V\\\\TA\\\\Implementasi\\\\R\\\\neuralNet.R')");
//Call method
double output = connection.eval("myneuralnetscript()").asDouble();
System.out.println(output);
} catch (RserveException | REXPMismatchException e) {
System.out.println("There is some problem indeed...");
}
}
However, the output that appears is "There is some problem indeed ...".
Please do not catch exceptions just to print a useless message. Remove your try catch and declare main to throw Exception. That way you'll see the actual error.
Either Rserve is not running locally on 6311 or it's failing to evaluate or the result of second evaluation cannot be coerced into a single double.
When you run eval do
tryCatch({CODE},e=function ()e)
instead and check if return inherits from try-error and get the message

R text mining: error in extracting bigrams from corpus during tokenization step

Fetching data from url:
suppressMessages(library(readr))
suppressMessages(library(RCurl))
amazon_url <- getURL('http://s3.amazonaws.com/assets.datacamp.com/production/course_935/datasets/500_amzn.csv',
ssl.verifyhost=FALSE, ssl.verifypeer=FALSE)
amazon <- read.csv(textConnection(amazon_url), header = TRUE)
Create amzn_cons:
amazon_cons <- amazon$cons
Build cleaning function based qdap package for text organization:
suppressWarnings(library(qdap))
qdap_clean <- function(x) {
x <- replace_abbreviation(x)
x <- replace_contraction(x)
x <- replace_number(x)
x <- replace_ordinal(x)
x <- replace_symbol(x)
x <- tolower(x)
return(x)
}
Build cleaning function based on tm package for text organization:
suppressWarnings(library(tm))
tm_clean <- function(corpus) {
corpus<- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, stripWhitespace)
corpus <- tm_map(corpus, removeWords,
c(stopwords("en"), "Amazon","company"))
return(corpus)
}
Word cleaning:
amzn_cons <- qdap_clean(amazon_cons)
amzn_cons <- VCorpus(VectorSource(amzn_cons))
amzn_cons_corp <- tm_clean(amzn_cons)
Build custom function to extract bigram features:
suppressWarnings(library(RWeka))
tokenizer <- function(x)
NGramTokenizer(x, Weka_control(min = 2, max = 2))
Apply tokenization function to get bigrams words:
amzn_c_tdm <- TermDocumentMatrix(
amzn_cons_corp,control = list(tokenize = tokenizer) )
This results in the following error:
Error in .jcall("RWekaInterfaces", "[S", "tokenize", .jcast(tokenizer, :
java.lang.NullPointerException
How to solve this error?

Number format exception in scala

I am trying to write a scala program to generate an output file omega0_Real.txt which contains pre-calculated values of the cosine function for inputs ranging from 0 to pi/2 radians. Each of these calculated values are 72 bit long and are stored in hex format. The code I have written so far is as follows:
import java.io._
import scala.math._
object omega0_Real {
def main (args: Array[String]) {
val arg = (0.0).to(2-pow(2, -10), pow(2, -10))
val cosArg = arg.map (i => cos(i))
val cosBit = cosArg.map (i => List.tabulate(72)(j = (BigDecimal((i*pow(2,j))).toBigInt % 2)))
val cosStr = cosBit.map (i => i mkString)
val cosBig = cosStr.map (i => BigInt(i, 2))
val cosBigStr = cosBig.map (i => i.toString(16))
val cosList = cosBigStr.toList
val file = "omega0_Real.txt"
val writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)))
for (x <- cosList) {
writer.write(x + "\n")
}
writer.close()
}
which gives the error: java.lang.NumberFormatException: Illegal embedded sign character followed by many others. Please help me debug this code.
PS: I ran this code line-by-line on sbt console but it did not give any error, although the values generated were erroneous.

Issue in R arules package using java

For a university project I have to implement arules(package of R) in java. I have successfully integrated R and java using JRI. I did not understand how to get output of "inspect(Groceries[1:1])". I have tried with asString(),asString[]() but this gives me following error:
Exception in thread "main" java.lang.NullPointerException
at TestR.main(TestR.java:11)
Also, how can implement summary(Groceries) in java? How to get output of summary in String array or string?
R code:
>data(Groceries)
>inspect(Groceries[1:1])
>summary(Groceries)
Java code:
import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
public class TestR {
public static void main(String[] args){
Rengine re = new Rengine(new String[]{"--no-save"}, false, null);
re.eval("library(arules)");
re.eval("data(Groceries)");
REXP result = re.eval("inspect(Groceries[1:1])");
System.out.println(result.asString());
}
}
Appears that the inspect function in pkg:arules returns NULL. The output you see is a "side-effect". You can attempt to "capture output" but this is untested since I don't have experience with this integration across languages. Try instead.:
REXP result = re.eval("capture.output( inspect(Groceries[1:1]) )");
In an R console session you will get:
library(arules)
data("Adult")
rules <- apriori(Adult)
val <- inspect(rules[1000])
> str(val)
NULL
> val.co <- capture.output(inspect(rules[1000]))
> val.co
[1] " lhs rhs support confidence lift"
[2] "1 {education=Some-college, "
[3] " sex=Male, "
[4] " capital-loss=None} => {native-country=United-States} 0.1208181 0.9256471 1.031449"
But I haven't tested this in a non-interactive session. May need to muck with the file argument to capture.output, ... or it may not work at all.

Categories

Resources