0

I have Rscript in script.R

preprocess1 <- function(directory){
  setwd(directory)
  library(stringi)
  library(stringr)
  main_dir <- directory
  sub_dir <- "preprocess1"
  dir.create(file.path(main_dir, sub_dir))

  ldf <- list() # creates a list
  listlog <- dir(pattern = "*.log") # creates the list of all the log files in the directory
  start.time <- Sys.time()

  for (i in 1:length(listlog)){
    ldf[[i]] <- read.table(listlog[i])
    ldf[[i]]$V2 = NULL
    ldf[[i]]$V3 = NULL
    ldf[[i]]$V5 = NULL
    ldf[[i]]$V10 = NULL
    ldf[[i]]$V11 = NULL
    tmp <- ldf[[i]]$V4
    tmp <- stringr::str_sub(tmp, start = 2)

    tmpdate = str_sub(tmp,1,11)
    tmphours = str_sub(tmp, -8)

    ldf[[i]]$date = factor(tmpdate)
    ldf[[i]]$hours = factor(tmphours)
    ldf[[i]]$V4 = NULL

    ldf[[i]] <- ldf[[i]][,c(1,6,7,2,3,4,5)]
    filename <- paste(i, ".log", sep="")
    write.table(ldf[[i]], file = file.path(main_dir, sub_dir, filename), sep=" ", row.names=FALSE, col.names=FALSE)
  }
  end.time <- Sys.time()
  time.taken <- end.time - start.time
  time.taken
  setwd(file.path(main_dir, sub_dir))
}

and I want to run that script in Java with Rserve, like this

static String dirPath;
    public static void main(String[] args) throws IOException, REXPMismatchException, REngineException {
        Scanner scanner = new Scanner(System.in );
        System.out.println("Enter the file path: ");
        dirPath = scanner.nextLine();;
        RConnection c = new RConnection();
        c.eval("source(\"E:/Data09/script007.R\")");
        REXP valueReturned = c.eval("preprocess1(\""+dirPath+"\")");
        System.out.println(valueReturned.asString());
}

In my expectation the script.R will execute it's process when I input path directory path of file I want to process. But, the error makes me stuck, and I don't know what should I do to fix it.

java.net.SocketException: Connection reset

I'm sure that I have running Rserve() in my R. Please, I need your help to finish my project. Thank you for your answer.

Ameerah
  • 65
  • 7
  • Is your Rserve instance local or remote? What is the Rserve configuration? Can you evaluate a simple R command? – copeg Aug 16 '16 at 00:11
  • @copeg I'm sorry, but how do i know whether my Rserve is local or remote? I copy the method of configuration from [here](http://www.codophile.com/how-to-integrate-r-with-java-using-rserve/) . Yes, I can evaluate when a simple command from my R. – Ameerah Aug 16 '16 at 00:49
  • 1
    ` how do i know whether my Rserve is local or remote` Is it running on the same machine as your java code? Is `fileio` enabled in the config? See https://rforge.net/Rserve/doc.html#conf – copeg Aug 16 '16 at 16:00
  • @copeg actually I can't find where is my Rserve.conf and then I see the answer from [this question](http://stackoverflow.com/questions/24150959/where-is-the-rserve-config-file-located-on-windows) to create it. Is that right? And then I set my `fileio` is enabled. I follow the instruction to run Rserve_d.exe, then I run my java code, it's still stop working. – Ameerah Aug 17 '16 at 02:58
  • I find another source about Rserve configuration in server side [here](http://www.originlab.com/doc/Origin-Help/Setup-R-Server). I follow the step just the setup for Rserve, run Rserve.exe, and then my java code, I get another error `org.rosuda.REngine.Rserve.RserveException: eval failed, request status: authorization failed`. How do you think? What should I do then? @copeg – Ameerah Aug 17 '16 at 03:04
  • Where is the `testMain()` function? – brijs Aug 17 '16 at 20:52
  • @copeg oh I'm sorry, I think that's my typo from give example in this question. I've edited it – Ameerah Aug 18 '16 at 17:43
  • 1
    You can launch Rserve with the specified arguments from within `R` using the `rserve` package as described [here](https://rforge.net/Rserve/doc.html#conf). Be sure `fileio` is `enabled` when launched, and be sure your firewall isn't blocking anything. I would also suggest you first attempt to evaluate a simple R statement from your java code to verify Rserver is functioning properly before pushing it to do more complex tasks. – copeg Aug 18 '16 at 18:02

0 Answers0