File: jri.R

package info (click to toggle)
rjava 1.0-11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,184 kB
  • sloc: java: 13,223; ansic: 5,479; sh: 3,776; xml: 325; makefile: 250; perl: 33
file content (86 lines) | stat: -rw-r--r-- 3,532 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
## bindings into JRI

## warning: JRI REXP class has currently no finalizers! (RReleaseREXP must be used manually for now)
## warning: this produces JRI-API pbjects - that should go away! use toJava below
.r2j <- function(x, engine = NULL, convert = TRUE) {
  if (is.null(engine)) engine <- .jcall("org/rosuda/JRI/Rengine","Lorg/rosuda/JRI/Rengine;","getMainEngine")
  if (!is(engine, "jobjRef")) stop("invalid or non-existent engine")
  new("jobjRef",jobj=.Call(PushToREXP,"org/rosuda/JRI/REXP",engine@jobj,engine@jclass,x,convert),jclass="org/rosuda/JRI/REXP")
}

toJava <- function(x, engine = NULL) {
  ## this is really the wrong place for all this REngine checking stuff, but so far .jengine uses JRI API only and legacy code may rely on that
  ## so this is the only place that assumes REngine API and thus will load it ...
  ec <- .jfindClass("org.rosuda.JRI.Rengine", silent=TRUE)
  if (is.jnull(ec)) {
    .jcheck(TRUE)
    stop("JRI is not loaded. Please start JRI first - see ?.jengine")
  }
  ec <- .jfindClass("org.rosuda.REngine.REngine", silent=TRUE)
  if (is.jnull(ec)) {
    .jcheck(TRUE)
    fn <- system.file("jri","REngine.jar",package="rJava")
    if (nzchar(fn)) .jaddClassPath(fn)
    fn <- system.file("jri","JRIEngine.jar",package="rJava")
    if (nzchar(fn)) .jaddClassPath(fn)
    ec <- .jfindClass("org.rosuda.REngine.REngine", silent=TRUE)
    if (is.jnull(ec)) {
      .jcheck(TRUE)
      stop("Cannot find REngine API classes. Please make sure you have installed and loaded the REngine API")
    }
  }
  if (is.null(engine)) engine <- .jcall("org/rosuda/REngine/REngine","Lorg/rosuda/REngine/REngine;","getLastEngine")
  if (is.jnull(engine)) { # no last engine, but there may be JRI engine already running ...
    me <- .jcall("org/rosuda/JRI/Rengine","Lorg/rosuda/JRI/Rengine;","getMainEngine", check=FALSE)
    .jcheck(TRUE)
    if (is.jnull(me)) stop("JRI is not running. Please start JRI first - see ?.jengine")
    engine <- .jnew("org/rosuda/REngine/JRI/JRIEngine", me)
    .jcheck(TRUE)
  }
  .jcheck(TRUE)
  if (!is(engine, "jobjRef")) stop("invalid or non-existent engine")
  new("jobjRef",jobj=.Call(PushToREXP,"org/rosuda/REngine/REXPReference",engine@jobj,"org/rosuda/REngine/REngine",x,NULL),jclass="org/rosuda/REngine/REXPReference")
}

.setupJRI <- function(new=TRUE) {
  ec <- .jfindClass("org.rosuda.JRI.Rengine", silent=TRUE)
  if (is.jnull(ec)) {
    .jcheck(TRUE)
    .jaddClassPath(system.file("jri","JRI.jar",package="rJava"))
    ec <- .jfindClass("org.rosuda.JRI.Rengine", silent=TRUE)
    .jcheck(TRUE)
    if (is.jnull(ec))
      stop("Cannot find JRI classes")
  }
  me <- .jcall("org/rosuda/JRI/Rengine","Lorg/rosuda/JRI/Rengine;","getMainEngine", check=FALSE)
  .jcheck(TRUE)
  if (!is.jnull(me)) {
    if (!new) return(TRUE)
    warning("JRI engine is already running.")
    return(FALSE)
  }
  e <- .jnew("org/rosuda/JRI/Rengine")
  !is.jnull(e)
}

.jengine <- function(start=FALSE, silent=FALSE) {
  me <- NULL
  ec <- .jfindClass("org.rosuda.JRI.Rengine", silent=TRUE)
  .jcheck(TRUE)
  if (!is.jnull(ec)) {
    me <- .jcall("org/rosuda/JRI/Rengine","Lorg/rosuda/JRI/Rengine;","getMainEngine", check=FALSE)
    .jcheck(TRUE)
  }
  if (is.jnull(me)) {
    if (!start) {
      if (silent) return(NULL)
      stop("JRI engine is not running.")
    }
    .setupJRI(FALSE)
    me <- .jcall("org/rosuda/JRI/Rengine","Lorg/rosuda/JRI/Rengine;","getMainEngine", check=FALSE)
    .jcheck(TRUE)
  }
  if (is.jnull(me) && !silent)
    stop("JRI engine is not running.")
  me
}