|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
org.rosuda.JRI.Rengine
public class Rengine
Rengine class is the interface between an instance of R and the Java VM. Due to the fact that R has no threading support, you can run only one instance of R withing a multi-threaded application. There are two ways to use R from Java: individual call and full event loop. See the Rengine constructor
for details.
Important note: All methods starting with rni
(R Native Interface) are low-level native methods that should be avoided if a high-level methods exists. They do NOT attempt any synchronization, so it is the duty of the calling program to ensure that the invocation is safe (see getRsync()
for details). At some point in the future when the high-level API is complete they should become private. However, currently this high-level layer is not complete, so they are available for now.
All rni
methods use long
type to reference SEXP
s on R side. Those reference should never be modified or used in arithmetics - the only reason for not using an extra interface class to wrap those references is that rni
methods are all native methods and therefore it would be too expensive to handle the unwrapping on the C side.
jri
methods are called internally by R and invoke the corresponding method from the even loop handler. Those methods should usualy not be called directly.
Since 0.5 a failure to load the JRI naitve library will not be fatal if jri.ignore.ule=yes
system preference is set. Rengine will still not work, but that gives a chance to GUI programs to report the error in a more meaningful way (use jriLoaded
to check the availability of JRI).
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.lang.Thread |
---|
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler |
Field Summary | |
---|---|
static int |
DEBUG
debug flag. |
int |
idleDelay
this value specifies the time (in ms) to spend sleeping between checks for R shutdown requests if R event loop is not used. |
static boolean |
jriLoaded
this flags is set to true if the native code was successfully loaded. |
static int |
SO_BaseEnv
constant to be used in rniSpecialObject(int) to return R_baseEnv reference |
static int |
SO_BlankString
constant to be used in rniSpecialObject(int) to return R_BlankString reference |
static int |
SO_EmptyEnv
constant to be used in rniSpecialObject(int) to return R_EmptyEnv reference |
static int |
SO_GlobalEnv
constant to be used in rniSpecialObject(int) to return R_GlobalEnv reference |
static int |
SO_MissingArg
constant to be used in rniSpecialObject(int) to return R_MissingArg reference |
static int |
SO_NaString
constant to be used in rniSpecialObject(int) to return R_NaString reference |
static int |
SO_NilValue
constant to be used in rniSpecialObject(int) to return R_NilValue reference |
static int |
SO_UnboundValue
constant to be used in rniSpecialObject(int) to return R_UnboundValue reference |
Fields inherited from class java.lang.Thread |
---|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
---|---|
Rengine()
create a new engine by hooking into an existing, initialized R instance which is calling this constructor. |
|
Rengine(java.lang.String[] args,
boolean runMainLoop,
RMainLoopCallbacks initialCallbacks)
create and start a new instance of R. |
Method Summary | |
---|---|
void |
addMainLoopCallbacks(RMainLoopCallbacks c)
Add a handler for R callbacks. |
boolean |
assign(java.lang.String sym,
boolean[] val)
assign values of an array of booleans to a symbol in R (creating a logical vector). |
boolean |
assign(java.lang.String sym,
double[] val)
assign values of an array of doubles to a symbol in R (creating an integer vector). |
boolean |
assign(java.lang.String sym,
int[] val)
assign values of an array of integers to a symbol in R (creating a numeric vector). |
boolean |
assign(java.lang.String sym,
REXP r)
assign a content of a REXP to a symbol in R. |
boolean |
assign(java.lang.String sym,
java.lang.String ct)
assign a string value to a symbol in R. |
boolean |
assign(java.lang.String sym,
java.lang.String[] val)
assign values of an array of strings to a symbol in R (creating a character vector). |
REXP |
createRJavaRef(java.lang.Object o)
creates a jobjRef reference in R via rJava. |
void |
end()
attempt to shut down R. |
REXP |
eval(java.lang.String s)
Parses and evaluates an R expression and returns the result. |
REXP |
eval(java.lang.String s,
boolean convert)
Parses and evaluates an R expression and returns the result. |
static Rengine |
getMainEngine()
return the current main R engine instance. |
Mutex |
getRsync()
returns the synchronization mutex for this engine. |
static long |
getVersion()
API version of the Rengine itself; see also rniGetVersion() for binary version. |
REXP |
idleEval(java.lang.String s)
This method is very much like eval(String) , except that it is non-blocking and returns null if the engine is busy. |
REXP |
idleEval(java.lang.String s,
boolean convert)
This method is very much like eval(String,boolean) , except that it is non-blocking and returns null if the engine is busy. |
static boolean |
inMainRThread()
returns true if the current thread is the main R thread, false otherwise |
boolean |
isStandAlone()
returns true if this engine was started as a stand-alone Java application or false if this engine was hooked into an existing R instance |
void |
jriBusy(int which)
JRI: R_Busy call-back from R |
java.lang.String |
jriChooseFile(int newFile)
JRI: R_ChooseFile call-back from R |
void |
jriFlushConsole()
JRI: R_FlushConsole call-back from R |
void |
jriLoadHistory(java.lang.String filename)
JRI: R_loadhistory call-back from R |
java.lang.String |
jriReadConsole(java.lang.String prompt,
int addToHistory)
JRI: R_ReadConsole call-back from R. |
void |
jriSaveHistory(java.lang.String filename)
JRI: R_savehistory call-back from R |
void |
jriShowMessage(java.lang.String message)
JRI: R_ShowMessage call-back from R |
void |
jriWriteConsole(java.lang.String text,
int oType)
JRI: R_WriteConsole call-back from R |
boolean |
rniAssign(java.lang.String name,
long exp,
long rho)
RNI: assign a value to an environment |
long |
rniCAR(long exp)
RNI: get CAR of a dotted-pair list (LISTSXP) |
long |
rniCDR(long exp)
RNI: get CDR of a dotted-pair list (LISTSXP) |
long |
rniCons(long head,
long tail)
RNI: create a dotted-pair list (LISTSXP) |
long |
rniCons(long head,
long tail,
long tag,
boolean lang)
RNI: create a dotted-pair list (LISTSXP or LANGSXP) |
long |
rniEval(long exp,
long rho)
RNI: evaluate R expression (do NOT use directly unless you know exactly what you're doing, where possible use eval(java.lang.String) instead). |
int |
rniExpType(long exp)
RNI: get the SEXP type |
long |
rniFindVar(java.lang.String sym,
long rho)
RNI: find variable in an environment |
long |
rniGetAttr(long exp,
java.lang.String name)
RNI: get an attribute |
java.lang.String[] |
rniGetAttrNames(long exp)
RNI: get attribute names |
int[] |
rniGetBoolArrayI(long exp)
RNI: get the contents of a logical vector in its integer array form |
double[] |
rniGetDoubleArray(long exp)
RNI: get the contents of a numeric vector |
int[] |
rniGetIntArray(long exp)
RNI: get the contents of an integer vector |
long[] |
rniGetList(long exp)
RNI: retrieve CAR part of a dotted-part list recursively as an array of references |
byte[] |
rniGetRawArray(long exp)
RNI: get the contents of a raw vector |
java.lang.String |
rniGetString(long exp)
RNI: get the contents of the first entry of a character vector |
java.lang.String[] |
rniGetStringArray(long exp)
RNI: get the contents of a character vector |
java.lang.String |
rniGetSymbolName(long sym)
RNI: retrieve name of a symbol (c.f. |
long[] |
rniGetVector(long exp)
RNI: get the contents of a generic vector (aka list) |
static long |
rniGetVersion()
RNI: return the API version of the native library |
void |
rniIdle()
RNI: run other event handlers in R |
boolean |
rniInherits(long exp,
java.lang.String cName)
RNI: determines whether an R object instance inherits from a specific class (S3 for now) |
long |
rniInstallSymbol(java.lang.String sym)
RNI: install a symbol name |
long |
rniJavaToXref(java.lang.Object o)
RNI: convert Java object to EXTPTRSEXP |
long |
rniLCons(long head,
long tail)
RNI: create a dotted-pair language list (LANGSXP) |
long |
rniListEnv(long exp,
boolean all)
RNI: return the list of variable names of an environment |
long |
rniParentEnv(long exp)
RNI: return the parent environment |
long |
rniParse(java.lang.String s,
int parts)
RNI: parses a string into R expressions (do NOT use directly unless you know exactly what you're doing, where possible use eval(java.lang.String) instead). |
void |
rniPreserve(long exp)
RNI: preserve object (prevent grabage collection in R) until rniRelease is called. |
void |
rniPrint(java.lang.String s,
int oType)
RNI: print. |
void |
rniPrintValue(long exp)
RNI: print the value of a given R object (via print or show method) to the console |
void |
rniProtect(long exp)
RNI: protect an R object (c.f. |
long |
rniPutBoolArray(boolean[] a)
RNI: create a boolean vector |
long |
rniPutBoolArrayI(int[] a)
RNI: create a boolean vector from an integer vector |
long |
rniPutDoubleArray(double[] a)
RNI: create a numeric vector |
long |
rniPutIntArray(int[] a)
RNI: create an integer vector |
long |
rniPutList(long[] cont)
RNI: create a dotted-pair list (LISTSXP) |
long |
rniPutRawArray(byte[] a)
RNI: create a raw vector |
long |
rniPutString(java.lang.String s)
RNI: create a character vector of the length 1 |
long |
rniPutStringArray(java.lang.String[] a)
RNI: create a character vector |
long |
rniPutVector(long[] exps)
RNI: create a generic vector (aka a list) |
void |
rniRelease(long exp)
RNI: release object previously preserved via rniPreserve. |
int |
rniRJavaLock()
RNI: lock rJava to allow callbacks - this interrupts R event loop until @link{rniRJavaUnlock} is called. |
int |
rniRJavaUnlock()
RNI: unlock rJava - resumes R event loop. |
void |
rniRunMainLoop()
RNI: run the main loop. |
void |
rniSetAttr(long exp,
java.lang.String name,
long attr)
RNI: set an attribute |
int |
rniSetupRJava(int _in,
int _out)
RNI: setup IPC with RJava. |
long |
rniSpecialObject(int which)
RNI: return a special object reference. |
int |
rniStop(int flag)
RNI: interrupt the R process (if possible). |
long |
rniTAG(long exp)
RNI: get TAG of a dotted-pair list (LISTSXP) |
void |
rniUnprotect(int count)
RNI: unprotect last count references (c.f. |
java.lang.Object |
rniXrefToJava(long exp)
RNI: convert EXTPTRSEXP to Java object - make sure the pointer is really what you expect, otherwise you'll crash the JVM! |
void |
run()
The implementation of the R thread. |
void |
startMainLoop()
if Rengine was initialized with runMainLoop=false then this method can be used to start the main loop at a later point. |
static boolean |
versionCheck()
check API version of this class and the native binary. |
boolean |
waitForR()
check the state of R |
Methods inherited from class java.lang.Thread |
---|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static boolean jriLoaded
true
if the native code was successfully loaded. If this flag is false
then none of the rni methods are available. Previous
public static final int SO_NilValue
rniSpecialObject(int)
to return R_NilValue
reference
public static final int SO_GlobalEnv
rniSpecialObject(int)
to return R_GlobalEnv
reference
public static final int SO_EmptyEnv
rniSpecialObject(int)
to return R_EmptyEnv
reference
public static final int SO_BaseEnv
rniSpecialObject(int)
to return R_baseEnv
reference
public static final int SO_UnboundValue
rniSpecialObject(int)
to return R_UnboundValue
reference
public static final int SO_MissingArg
rniSpecialObject(int)
to return R_MissingArg
reference
public static final int SO_NaString
rniSpecialObject(int)
to return R_NaString
reference
public static final int SO_BlankString
rniSpecialObject(int)
to return R_BlankString
reference
public static int DEBUG
public int idleDelay
Thread.stop()
uses interrupt to awake from the idle sleep immediately, but some implementation may not honor that).
Constructor Detail |
---|
public Rengine(java.lang.String[] args, boolean runMainLoop, RMainLoopCallbacks initialCallbacks)
args
- arguments to be passed to R. Please note that R requires the presence of certain arguments (e.g. --save
or --no-save
or equivalents), so passing an empty list usually doesn't work.runMainLoop
- if set to true
the the event loop will be started as soon as possible, otherwise no event loop is started. Running loop requires initialCallbacks
to be set correspondingly as well.initialCallbacks
- an instance implementing the RMainLoopCallbacks
interface that provides methods to be called by Rpublic Rengine()
Method Detail |
---|
public static long getVersion()
versionCheck()
long
in the form 0xMMmm
public static boolean versionCheck()
true
if the API version of the Java code and the native library matches, false
otherwisepublic static Rengine getMainEngine()
null
if no R engine was started yet.public static boolean inMainRThread()
true
if the current thread is the main R thread, false
otherwise
public boolean isStandAlone()
true
if this engine was started as a stand-alone Java application or false
if this engine was hooked into an existing R instance
public int rniSetupRJava(int _in, int _out)
public int rniRJavaLock()
public int rniRJavaUnlock()
public long rniParse(java.lang.String s, int parts)
eval(java.lang.String)
instead). Note that no synchronization is performed!
s
- string to parseparts
- number of expressions contained in the string
public long rniEval(long exp, long rho)
eval(java.lang.String)
instead). Note that no synchronization is performed!
exp
- reference to the expression to evaluaterho
- environment to use for evaluation (or 0 for global environemnt)
public void rniProtect(long exp)
exp
- reference to protectpublic void rniUnprotect(int count)
count
references (c.f. UNPROTECT in C)
count
- number of references to unprotectpublic java.lang.String rniGetString(long exp)
exp
- reference to STRSXP
null
if the reference is not STRSXPpublic java.lang.String[] rniGetStringArray(long exp)
exp
- reference to STRSXP
null
if the reference is not STRSXPpublic int[] rniGetIntArray(long exp)
exp
- reference to INTSXP
null
if the reference is not INTSXPpublic int[] rniGetBoolArrayI(long exp)
exp
- reference to LGLSXP
null
if the reference is not LGLSXPpublic double[] rniGetDoubleArray(long exp)
exp
- reference to REALSXP
null
if the reference is not REALSXPpublic byte[] rniGetRawArray(long exp)
exp
- reference to RAWSXP
null
if the reference is not RAWSXPpublic long[] rniGetVector(long exp)
exp
- reference to VECSXP
null
if the reference is not VECSXPpublic long rniPutString(java.lang.String s)
s
- initial contents of the first entry
public long rniPutStringArray(java.lang.String[] a)
a
- initial contents of the vector
public long rniPutIntArray(int[] a)
a
- initial contents of the vector
public long rniPutBoolArrayI(int[] a)
a
- initial contents of the vector
public long rniPutBoolArray(boolean[] a)
a
- initial contents of the vector
public long rniPutDoubleArray(double[] a)
a
- initial contents of the vector
public long rniPutRawArray(byte[] a)
a
- initial contents of the vector
public long rniPutVector(long[] exps)
exps
- initial contents of the vector consisiting of an array of references
public long rniGetAttr(long exp, java.lang.String name)
exp
- reference to the object whose attribute is requestedname
- name of the attribute
public java.lang.String[] rniGetAttrNames(long exp)
exp
- reference to the object whose attributes are requested
null
if there are nonepublic void rniSetAttr(long exp, java.lang.String name, long attr)
exp
- reference to the object whose attribute is to be modifiedname
- attribute nameattr
- reference to the object to be used as the contents of the attributepublic boolean rniInherits(long exp, java.lang.String cName)
exp
- reference to an objectcName
- name of the class to check
true
if cName
inherits from class cName
(see inherits
in R)public long rniCons(long head, long tail, long tag, boolean lang)
head
- CARtail
- CDR (must be a reference to LISTSXP or 0)tag
- TAGlang
- if true
then LANGSXP is created, otherwise LISTSXP.
public long rniCons(long head, long tail)
head
- CARtail
- CDR (must be a reference to LISTSXP or 0)
public long rniLCons(long head, long tail)
head
- CARtail
- CDR (must be a reference to LANGSXP or 0)
public long rniCAR(long exp)
exp
- reference to the list
public long rniCDR(long exp)
exp
- reference to the list
public long rniTAG(long exp)
exp
- reference to the list
public long rniPutList(long[] cont)
cont
- contents as an array of references
public long[] rniGetList(long exp)
exp
- reference to a dotted-pair list (LISTSXP)
public java.lang.String rniGetSymbolName(long sym)
sym
- reference to a symbol
null
on error or if exp is no symbolpublic long rniInstallSymbol(java.lang.String sym)
sym
- symbol name
public void rniPrint(java.lang.String s, int oType)
Note: May NOT be called inside any WriteConsole callback as it would cause an infinite loop.
s
- string to print (as-is)oType
- output type (see R for exact references, but 0 should be regular output and 1 error/warning)public void rniPrintValue(long exp)
exp
- reference to an R objectpublic void rniPreserve(long exp)
exp
- reference to an R objectpublic void rniRelease(long exp)
Note: releasing an obejct that was not preserved is an error and results in an undefined behavior.
exp
- reference to an R objectpublic long rniParentEnv(long exp)
exp
- reference to environment
public long rniFindVar(java.lang.String sym, long rho)
sym
- symbol namerho
- reference to environment
public long rniListEnv(long exp, boolean all)
exp
- reference to the environmentall
- if set to true
then all objects will be shown, otherwise hidden objects will be omitted
public long rniSpecialObject(int which)
which
- constant referring to a particular special object (see SO_xxx constants)
public long rniJavaToXref(java.lang.Object o)
o
- arbitrary Java object
public java.lang.Object rniXrefToJava(long exp)
exp
- reference to EXTPTRSEXP pointing to a Java object
public static long rniGetVersion()
public int rniStop(int flag)
flag
- determines how to attempt to inform R about the interrput. For normal (safe) operation using flag signalling must be 0. Other options are 1 (SIGINT for compatibility with older JRI API) and 2 (Rf_onintr call - use only on the R thread and only if you know what it means). Values other than 0 are only supported since JRI 0.5-4.
public boolean rniAssign(java.lang.String name, long exp, long rho)
name
- nameexp
- valuerho
- environment (use 0 for the global environment)
true
if successful, false
on failure (usually this means that the binding is locked)void
)public int rniExpType(long exp)
exp
- reference to a SEXP
public void rniRunMainLoop()
public void rniIdle()
public void addMainLoopCallbacks(RMainLoopCallbacks c)
public void startMainLoop()
runMainLoop=false
then this method can be used to start the main loop at a later point. It has no effect if the loop is running already. This method returns immediately but the loop will be started once the engine is ready. Please note that there is currently no way of stopping the R thread if the R event loop is running other than using quit
command in R which closes the entire application.
public void jriWriteConsole(java.lang.String text, int oType)
text
- text to displypublic void jriBusy(int which)
which
- statepublic java.lang.String jriReadConsole(java.lang.String prompt, int addToHistory)
prompt
- prompt to display before waiting for the input.addToHistory
- flag specifying whether the entered contents should be added to history
null
corresponds to an EOF and usually causes R to exit (as in q()).
public void jriShowMessage(java.lang.String message)
message
- messagepublic void jriLoadHistory(java.lang.String filename)
filename
- name of the history filepublic void jriSaveHistory(java.lang.String filename)
filename
- name of the history filepublic java.lang.String jriChooseFile(int newFile)
newFile
- flag specifying whether an existing or new file is requested
null
if cancelledpublic void jriFlushConsole()
public REXP eval(java.lang.String s)
eval(s, true)
.
s
- expression (as string) to parse and evaluate
null
if something wnet wrongpublic REXP eval(java.lang.String s, boolean convert)
s
- expression (as string) to parse and evaluateconvert
- if set to true
the resulting REXP will contain native representation of the contents, otherwise an empty REXP will be returned. Depending on the back-end an empty REXP may or may not be used to convert the result at a later point.
null
if something wnet wrongpublic REXP idleEval(java.lang.String s)
eval(String)
, except that it is non-blocking and returns null
if the engine is busy.
s
- string to evaluate
null
if the engine is busypublic REXP idleEval(java.lang.String s, boolean convert)
eval(String,boolean)
, except that it is non-blocking and returns null
if the engine is busy.
s
- string to evaluateconvert
- flag denoting whether an empty or fully-converted REXP should be returned (see eval(String,boolean)
for details)
null
if the engine is busypublic Mutex getRsync()
boolean obtainedLock = e.getRsync().safeLock(); try { // use RNI here ... } finally { if (obtainedLock) e.getRsync().unlock(); }
public boolean waitForR()
true
if R is alive and false
if R died or exittedpublic void end()
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public boolean assign(java.lang.String sym, java.lang.String ct)
sym
- symbol name. The symbol name is used as-is, i.e. as if it was quoted in R code (for example assigning to "foo$bar" has the same effect as `foo$bar`<- and NOT foo$bar<-).ct
- contents
true
if successful, false
otherwisepublic boolean assign(java.lang.String sym, REXP r)
sym
- symbol name. The symbol name is used as-is, i.e. as if it was quoted in R code (for example assigning to "foo$bar" has the same effect as `foo$bar`<- and NOT foo$bar<-).r
- contents as REXP
. currently only raw references and basic types (int, double, int[], double[], boolean[]) are supported.
true
if successful, false
otherwise (usually locked binding or unsupported REXP)public boolean assign(java.lang.String sym, double[] val)
assign(String, REXP)
.
sym
- symbol nameval
- double array to assign
true
if successful, false
otherwisepublic boolean assign(java.lang.String sym, int[] val)
assign(String, REXP)
.
sym
- symbol nameval
- integer array to assign
true
if successful, false
otherwisepublic boolean assign(java.lang.String sym, boolean[] val)
assign(String, REXP)
.
sym
- symbol nameval
- boolean array to assign
true
if successful, false
otherwisepublic boolean assign(java.lang.String sym, java.lang.String[] val)
assign(String, REXP)
.
sym
- symbol nameval
- string array to assign
true
if successful, false
otherwisepublic REXP createRJavaRef(java.lang.Object o)
jobjRef
reference in R via rJava.eval("{library(rJava);.jinit()}",false)
, otherwise this will fail. Requires rJava 0.4-13 or higher!
o
- object to push
jobjRef
object or null
upon failure. It will have the type XT_NONE
such that it can be used in @link{assign(String, REXP)}.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |