Package org.math.R
Class R2jsSession
- java.lang.Object
-
- org.math.R.Rsession
-
- org.math.R.R2jsSession
-
- All Implemented Interfaces:
RLog
public class R2jsSession extends Rsession implements RLog
This class evaluate an R expression by parsing it in javascript expression and then evaluate the javascript expression with the Java ScriptEngine. This class uses external javascript library like mathjs to evaluate expressions. ------------------------------ Supported and unsupported syntaxes ------------------------------------------------------------------ ## Basic syntaxes - affectation (=, <-) - mathematical expression (pi, ^, **, <, <=, >, >=, !=, ==, ++, +=, -=, priority of operators, for, for in, if, else, range) ## Functions - All syntaxes of functions in R - recursive functions - Mathematical functions ("abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp", "floor", "log", "max", "min", "round", "sin", "sqrt", "tan" ) - NOT SUPPORTED: - function with named argument (ex: "f(y=1.23,x=4.56)") (impossible to support in the ES5) - multiple functions in one command (impossible just write multiple lines) ## Arrays - definition of arrays (c(1,2,3), c(0:4), array(0.0,c(4,3))) - accessor: a[1], - operations (+, -, *, /) - function: length - NOT SUPPORTED: %*%, %/% : create matrices instead of arrays to use theses operations ## Matrices - definition of matrices with: nrow, ncol, byrow - operations (transpose, +, -, *, ^, %*%, %/%, %%) - column and row selection ([1,], [,1], [,], [c(1,2),]) - function supported: determinant, solve, dim - TO SUPPORT: eigen ## DataFrames - constructor: data.frame(first=a,second=b), data.frame('first'=a,'second'=b), data.frame(a,b) - accessor: '$', "[['element']]" NOT SUPPORTED: column extraction "[1:2,]" ## Lists - constructor: list(first=a,second=b), list('first'=a,'second'=b), list(a,b) - accessor: '$', "[['element']]" NOT SUPPORTED: column extraction "[1:2,]", accessor "[[1]]" ## R functions - write.csv - runif - save and load variables - ls - rm variables - cbind (on matrices only) - rbind (on matrices only) - file.exists - savels TO SUPPORT: rnorm, capture.output NOT SUPPORTED: toPNG, asHTML, cbind (array and dataframe), rbind (array and dataframe), multiple imbricated functions ------------------------------------------------------------------------------------------------------------------------------------ Author:
- Nicolas Chabalier
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.math.R.Rsession
Rsession.Function, Rsession.RException
-
Nested classes/interfaces inherited from interface org.math.R.RLog
RLog.Level
-
-
Field Summary
Fields Modifier and Type Field Description booleandebug_jsstatic String[]DISABLED_FUNCTIONSSet<String>functionsSetScriptEnginejsstatic PropertiesR_TO_JSSet<String>variablesSet-
Fields inherited from class org.math.R.Rsession
_PACKAGE_, CAST_ERROR, debug, DEFAULT_REPOS, envName, GRAPHIC_BMP, GRAPHIC_JPEG, GRAPHIC_PNG, GRAPHIC_TIFF, HEAD_SET, HEAD_TRY, PACKAGEINSTALLED, PACKAGELOADED, repos, SAVE_ASCII, TRY_MODE, TRY_MODE_DEFAULT
-
-
Constructor Summary
Constructors Constructor Description R2jsSession(PrintStream p, Properties properties)R2jsSession(PrintStream p, Properties properties, String environmentName)R2jsSession(RLog console, Properties properties)R2jsSession(RLog console, Properties properties, String environmentName)Default constructor Initialize the Javascript js and load external js libraries
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddJSFunction(String name, String js)voidaddReturnNullFunction(String name)double[]asArray(Object o)doubleasDouble(Object o)intasInteger(Object o)int[]asIntegers(Object o)MapasList(Object o)booleanasLogical(Object o)boolean[]asLogicals(Object o)double[][]asMatrix(Object o)StringasString(Object o)String[]asStrings(Object o)Objectcast(Object o)voidcopyGlobalEnv(String envName)voidend()static StringgetEnv(String k)voidgetFileFromWorkspace(File file)Stringgethomedir()static StringHTMLfun(String Rcode, String fun, String... args)booleanisAvailable()booleanisNull(Object o)StringloadPackage(String pack)load R backage using library() commandString[]ls(boolean all)list R variables in R env.static voidmain(String[] args)static StringnameRtoJs(String e)Convert an R expression in a Js expression WARNING: many R syntaxes are not supported yetstatic R2jsSessionnewInstance(RLog console, Properties properties)FileputFileInWorkspace(File file)static StringreplaceIndexes(String expr)Replace indexes by mathjs indexesstatic StringreplaceIndexesSet(String expr)booleanrm(String... vars)delete R variables in R env.booleanrmAll()booleanset(String varname, double[][] data, String... names)Set R data.frame in R env.booleanset(String varname, Object var)Set R object in R env.static voidsetEnv(String k, String v)voidsetGlobalEnv(String envName)protected ObjectsilentlyRawEval(String expression, boolean tryEval)Silently (ie no log) launch R command and return value.protected booleansilentlyVoidEval(String expression, boolean tryEval)Silently (ie no log) launch R command without return value.voidsource(File file)loads R source file (eg ".R" file)StringtoString(Object o)-
Methods inherited from class org.math.R.Rsession
addBusyListener, addEvalListener, addLogger, addUpdateObjectsListener, asHTML, asR2HTML, buildList, buildListPattern, buildListString, cat, cat, cat, cat, cat, cat, cat, closeLog, eval, eval, finalize, getGlobalEnv, getLastError, getLastLogEntry, getLastOutput, getRepository, getwd, hash, installPackage, installPackage, installPackage, installPackages, isPackageInstalled, isPackageLoaded, listCommands, load, local2remotePath, log, ls, ls, note_code, note_code, note_header, note_text, notebook, print, proxyEval, rawEval, rawEval, remote2localPath, removeBusyListener, removeEvalListener, removeLogger, removeUpdateObjectsListener, reshapeAsRow, reshapeAsRow, rmAll, rmls, save, savels, set, setBusy, setCRANRepository, setRepository, setwd, silentlyRawEval, silentlyVoidEval, sinkMessage, sinkOutput, t, toBMP, toGraphic, toGraphic, toHTML, toJPEG, toPNG, toRcode, toRcode, toRcode, toRcode, toRcode, toRcode, toRcode, toRpath, toRpath, toTIFF, typeOf, unset, unset, voidEval, voidEval
-
-
-
-
Field Detail
-
js
public ScriptEngine js
-
DISABLED_FUNCTIONS
public static final String[] DISABLED_FUNCTIONS
-
R_TO_JS
public static Properties R_TO_JS
-
debug_js
public boolean debug_js
-
-
Constructor Detail
-
R2jsSession
public R2jsSession(RLog console, Properties properties)
-
R2jsSession
public R2jsSession(RLog console, Properties properties, String environmentName)
Default constructor Initialize the Javascript js and load external js libraries- Parameters:
console- - consoleproperties- - propertiesenvironmentName- - name of the environment
-
R2jsSession
public R2jsSession(PrintStream p, Properties properties)
-
R2jsSession
public R2jsSession(PrintStream p, Properties properties, String environmentName)
-
-
Method Detail
-
newInstance
public static R2jsSession newInstance(RLog console, Properties properties)
-
addReturnNullFunction
public void addReturnNullFunction(String name) throws ScriptException
- Throws:
ScriptException
-
addJSFunction
public void addJSFunction(String name, String js) throws ScriptException
- Throws:
ScriptException
-
nameRtoJs
public static String nameRtoJs(String e)
Convert an R expression in a Js expression WARNING: many R syntaxes are not supported yet- Parameters:
e- - the R expression- Returns:
- the js script expression
-
ls
public String[] ls(boolean all)
Description copied from class:Rsessionlist R variables in R env.
-
replaceIndexes
public static String replaceIndexes(String expr)
Replace indexes by mathjs indexes- Parameters:
expr- - the expression containing indexes to replace- Returns:
- the expression with replaced indexes
-
isAvailable
public boolean isAvailable()
- Specified by:
isAvailablein classRsession
-
gethomedir
public String gethomedir()
- Specified by:
gethomedirin classRsession
-
silentlyVoidEval
protected boolean silentlyVoidEval(String expression, boolean tryEval)
Description copied from class:RsessionSilently (ie no log) launch R command without return value.- Specified by:
silentlyVoidEvalin classRsession- Parameters:
expression- R expresison to evaluatetryEval- encapsulate command in try() to cacth errors- Returns:
- well evaluated ?
-
silentlyRawEval
protected Object silentlyRawEval(String expression, boolean tryEval)
Description copied from class:RsessionSilently (ie no log) launch R command and return value.- Specified by:
silentlyRawEvalin classRsession- Parameters:
expression- R expression to evaluatetryEval- encapsulate command in try() to cacth errors- Returns:
- REXP R expression
-
set
public boolean set(String varname, double[][] data, String... names) throws Rsession.RException
Description copied from class:RsessionSet R data.frame in R env.- Specified by:
setin classRsession- Parameters:
varname- R list namedata- numeric data in listnames- names of columns- Returns:
- succeeded ?
- Throws:
Rsession.RException- Could not set
-
putFileInWorkspace
public File putFileInWorkspace(File file)
- Specified by:
putFileInWorkspacein classRsession
-
getFileFromWorkspace
public void getFileFromWorkspace(File file)
- Specified by:
getFileFromWorkspacein classRsession
-
source
public void source(File file)
Description copied from class:Rsessionloads R source file (eg ".R" file)
-
rm
public boolean rm(String... vars) throws Rsession.RException
delete R variables in R env.- Overrides:
rmin classRsession- Parameters:
vars- R objects names- Returns:
- well removed ?
- Throws:
Rsession.RException- Could not do rm
-
loadPackage
public String loadPackage(String pack)
Description copied from class:Rsessionload R backage using library() command- Overrides:
loadPackagein classRsession- Parameters:
pack- R package name- Returns:
- loading status
-
asDouble
public double asDouble(Object o) throws ClassCastException
- Specified by:
asDoublein classRsession- Throws:
ClassCastException
-
asArray
public double[] asArray(Object o) throws ClassCastException
- Specified by:
asArrayin classRsession- Throws:
ClassCastException
-
asMatrix
public double[][] asMatrix(Object o) throws ClassCastException
- Specified by:
asMatrixin classRsession- Throws:
ClassCastException
-
asString
public String asString(Object o) throws ClassCastException
- Specified by:
asStringin classRsession- Throws:
ClassCastException
-
asStrings
public String[] asStrings(Object o) throws ClassCastException
- Specified by:
asStringsin classRsession- Throws:
ClassCastException
-
asInteger
public int asInteger(Object o) throws ClassCastException
- Specified by:
asIntegerin classRsession- Throws:
ClassCastException
-
asIntegers
public int[] asIntegers(Object o) throws ClassCastException
- Specified by:
asIntegersin classRsession- Throws:
ClassCastException
-
asLogical
public boolean asLogical(Object o) throws ClassCastException
- Specified by:
asLogicalin classRsession- Throws:
ClassCastException
-
asLogicals
public boolean[] asLogicals(Object o) throws ClassCastException
- Specified by:
asLogicalsin classRsession- Throws:
ClassCastException
-
asList
public Map asList(Object o) throws ClassCastException
- Specified by:
asListin classRsession- Throws:
ClassCastException
-
cast
public Object cast(Object o) throws ClassCastException
- Specified by:
castin classRsession- Throws:
ClassCastException
-
setGlobalEnv
public void setGlobalEnv(String envName)
- Specified by:
setGlobalEnvin classRsession
-
copyGlobalEnv
public void copyGlobalEnv(String envName)
- Specified by:
copyGlobalEnvin classRsession
-
HTMLfun
public static String HTMLfun(String Rcode, String fun, String... args) throws Rsession.RException
- Throws:
Rsession.RException
-
-