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.