I am trying the Java client to rserve. I have a function defined in R as follows:
bar <- function(x) { x+1 }
executing this inside R gives the following (expected) output:
> bar(1)
[1] 2
however, executing the following Java code:
public static void main(String[] args) throws REXPMismatchException, REngineException {
RConnection c = new RConnection();
REXP x = c.eval("try({bar(1)}, silent=TRUE)");
System.out.println(x.asString());
}
gives the following output:
Error in try({ : could not find function "bar"
inside the R console the following message is output:
> Error: could not find function "bar"
Is there something I need to do to make my function visible to Rserve?
Steve
You are defining your function inside of a different workspace / process. Rserve and R do not share the same process space so declaring your method in one will not make it appear in Rserve workspace.
public static void main(String[] args) throws REXPMismatchException, REngineException {
RConnection c = new RConnection();
REXP x = c.eval("try({bar <- function(x) { x+1 }; bar(1)}, silent=TRUE)");
System.out.println(x.asString());
}
i had similar issue, you need to store your R code in a file which is called when Rserve is started (within same process), steps to be taken:
Create file with your R code (functions etc. ...) and name it for example filename.R
Create Rserv.conf file and paste there line
source /path to the file with R code/filename.R
Run R serve using command
Rserve(debug = TRUE, args='--no-save --RS-conf /path to the Rserv file/Rserv.conf')
it should works ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.