Package org.math.R
Class Rsession
- java.lang.Object
-
- org.math.R.Rsession
-
- All Implemented Interfaces:
RLog
- Direct Known Subclasses:
R2jsSession,RenjinSession,RserveSession
public abstract class Rsession extends Object implements RLog
- Author:
- richet
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classRsession.Functionstatic classRsession.RException-
Nested classes/interfaces inherited from interface org.math.R.RLog
RLog.Level
-
-
Field Summary
Fields Modifier and Type Field Description static String_PACKAGE_static StringCAST_ERRORbooleandebugstatic StringDEFAULT_REPOSprotected StringenvNamestatic StringGRAPHIC_BMPstatic StringGRAPHIC_JPEGstatic StringGRAPHIC_PNGstatic StringGRAPHIC_TIFFstatic StringHEAD_SETstatic StringHEAD_TRYstatic StringPACKAGEINSTALLEDstatic StringPACKAGELOADEDStringreposbooleanSAVE_ASCIIbooleanTRY_MODEbooleanTRY_MODE_DEFAULT
-
Constructor Summary
Constructors Constructor Description Rsession(PrintStream p)create rsession using System as a loggerRsession(RLog console)create a new Rsession.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddBusyListener(BusyListener b)voidaddEvalListener(EvalListener b)voidaddLogger(RLog l)voidaddUpdateObjectsListener(UpdateObjectsListener b)abstract double[]asArray(Object o)abstract doubleasDouble(Object o)StringasHTML(String command)Get R command text output in HTML formatabstract intasInteger(Object o)abstract int[]asIntegers(Object o)abstract MapasList(Object o)abstract booleanasLogical(Object o)abstract boolean[]asLogicals(Object o)abstract double[][]asMatrix(Object o)StringasR2HTML(String command)Get R command text output in HTML formatabstract StringasString(Object o)abstract String[]asStrings(Object o)static StringbuildList(String... vars)create a R list with given R objectsstatic StringbuildListPattern(String... vars)create a R list with given R string patternsstatic StringbuildListString(String... vars)create a R list with given R stringsabstract Objectcast(Object o)static Stringcat(double[] array)static Stringcat(double[][] array)static Stringcat(int[] array)static Stringcat(int[][] array)static Stringcat(Object[] array)static Stringcat(Object[][] array)static Stringcat(String sep, String[] array)voidcloseLog()abstract voidcopyGlobalEnv(String newEnvName)voidend()Objecteval(String expression)Objecteval(String expression, boolean tryEval)protected voidfinalize()abstract voidgetFileFromWorkspace(File f)StringgetGlobalEnv()abstract Stringgethomedir()StringgetLastError()StringgetLastLogEntry()StringgetLastOutput()StringgetRepository()Stringgetwd()protected Stringhash(Object k)StringinstallPackage(File pack, boolean load)Start installation procedure of local R packageStringinstallPackage(String pack, boolean load)Start installation procedure of CRAN R packageStringinstallPackage(String pack, File dir, boolean load)Start installation procedure of local R packageStringinstallPackages(String[] pack, boolean load)Start installation procedure of R packagesabstract booleanisAvailable()abstract booleanisNull(Object o)booleanisPackageInstalled(String pack, String version)Check for package installed in R environment.booleanisPackageLoaded(String pack)Check for package loaded in R environment.String[]listCommands()voidload(File f)loads R data file (eg ".Rdata" file)StringloadPackage(String pack)load R backage using library() commandFilelocal2remotePath(File localpath)voidlog(String message, RLog.Level level)Support R messages printinString[]ls()String[]ls(boolean all)list R variables in R env.String[]ls(String... vars)list R variables in R env. matching patternsvoidnote_code(String code)voidnote_code(String... code)voidnote_header(String title, String author)voidnote_text(String txt)Stringnotebook()Stringprint(String command)Get R command text outputObjectproxyEval(String expression, Map<String,Object> vars)Method to rawEval expression.abstract FileputFileInWorkspace(File f)protected ObjectrawEval(String expression)Launch R command and return value.protected ObjectrawEval(String expression, boolean tryEval)Launch R command and return value.Fileremote2localPath(File remotepath)voidremoveBusyListener(BusyListener b)voidremoveEvalListener(EvalListener b)voidremoveLogger(RLog l)voidremoveUpdateObjectsListener(UpdateObjectsListener b)protected static double[]reshapeAsRow(double[][] a)protected static double[]reshapeAsRow(Double[][] a)booleanrm(String... vars)delete R variables in R env.booleanrmAll()booleanrmAll(boolean all)delete all variables in R environmentbooleanrmls(String... vars)delete R variables in R env. matching patternsvoidsave(File f, String... vars)Save R variables in data filevoidsavels(File f, String... vars)Save R variables in data fileabstract booleanset(String varname, double[][] data, String... names)Set R data.frame in R env.abstract booleanset(String varname, Object var)Set R object in R env.booleanset(Map<String,Object> _vars)Set R object in R env.voidsetBusy(boolean bb)voidsetCRANRepository(String url)abstract voidsetGlobalEnv(String envName)voidsetRepository(String url)voidsetwd(File wdir)protected ObjectsilentlyRawEval(String expression)Silently (ie no log) launch R command and return value.protected abstract ObjectsilentlyRawEval(String expression, boolean tryEval)Silently (ie no log) launch R command and return value.protected booleansilentlyVoidEval(String expression)Silently (ie no log) launch R command without return value.protected abstract booleansilentlyVoidEval(String expression, boolean tryEval)Silently (ie no log) launch R command without return value.voidsinkMessage(boolean s)voidsinkOutput(boolean s)voidsource(File f)loads R source file (eg ".R" file)protected double[][]t(double[][] m)voidtoBMP(File f, int width, int height, String... commands)voidtoGraphic(File f, int width, int height, String... commands)voidtoGraphic(File f, int width, int height, String fileformat, String... commands)Create a file for R graphical command outputstatic StringtoHTML(String src)voidtoJPEG(File f, int width, int height, String... commands)voidtoPNG(File f, int width, int height, String... commands)protected static StringtoRcode(double[] d)protected static StringtoRcode(double[][] d)protected static StringtoRcode(Double[] d)protected static StringtoRcode(Double[][] d)protected static StringtoRcode(Object o)protected static StringtoRcode(String[] d)protected static StringtoRcode(Map l)StringtoRpath(File path)Map java File object to R path (as string)StringtoRpath(String path)Map java path to R path (as string)abstract StringtoString(Object o)voidtoTIFF(File f, int width, int height, String... commands)StringtypeOf(String robject)booleanunset(String... varname)delete R object in R env.booleanunset(Collection varname)delete R object in R env.booleanvoidEval(String expression)Launch R command without return value.booleanvoidEval(String expression, boolean tryEval)Launch R command without return value.
-
-
-
Field Detail
-
HEAD_TRY
public static final String HEAD_TRY
- See Also:
- Constant Field Values
-
TRY_MODE_DEFAULT
public boolean TRY_MODE_DEFAULT
-
TRY_MODE
public boolean TRY_MODE
-
CAST_ERROR
public static final String CAST_ERROR
- See Also:
- Constant Field Values
-
_PACKAGE_
public static final String _PACKAGE_
- See Also:
- Constant Field Values
-
PACKAGEINSTALLED
public static final String PACKAGEINSTALLED
- See Also:
- Constant Field Values
-
PACKAGELOADED
public static final String PACKAGELOADED
- See Also:
- Constant Field Values
-
debug
public boolean debug
-
DEFAULT_REPOS
public static String DEFAULT_REPOS
-
repos
public String repos
-
SAVE_ASCII
public boolean SAVE_ASCII
-
HEAD_SET
public static final String HEAD_SET
- See Also:
- Constant Field Values
-
GRAPHIC_PNG
public static final String GRAPHIC_PNG
- See Also:
- Constant Field Values
-
GRAPHIC_JPEG
public static final String GRAPHIC_JPEG
- See Also:
- Constant Field Values
-
GRAPHIC_BMP
public static final String GRAPHIC_BMP
- See Also:
- Constant Field Values
-
GRAPHIC_TIFF
public static final String GRAPHIC_TIFF
- See Also:
- Constant Field Values
-
envName
protected String envName
-
-
Constructor Detail
-
Rsession
public Rsession(RLog console)
create a new Rsession.- Parameters:
console- RLog for R output
-
Rsession
public Rsession(PrintStream p)
create rsession using System as a logger- Parameters:
p- PrintStream for R output
-
-
Method Detail
-
sinkOutput
public void sinkOutput(boolean s)
-
sinkMessage
public void sinkMessage(boolean s)
-
isAvailable
public abstract boolean isAvailable()
-
finalize
protected void finalize() throws Throwable
-
addLogger
public void addLogger(RLog l)
-
removeLogger
public void removeLogger(RLog l)
-
end
public void end()
-
addBusyListener
public void addBusyListener(BusyListener b)
-
removeBusyListener
public void removeBusyListener(BusyListener b)
-
setBusy
public void setBusy(boolean bb)
-
addUpdateObjectsListener
public void addUpdateObjectsListener(UpdateObjectsListener b)
-
removeUpdateObjectsListener
public void removeUpdateObjectsListener(UpdateObjectsListener b)
-
addEvalListener
public void addEvalListener(EvalListener b)
-
removeEvalListener
public void removeEvalListener(EvalListener b)
-
cat
public static String cat(double[] array)
-
cat
public static String cat(int[] array)
-
cat
public static String cat(double[][] array)
-
cat
public static String cat(int[][] array)
-
toRpath
public String toRpath(File path)
Map java File object to R path (as string)- Parameters:
path- java File object- Returns:
- R path with suitable level delimiter "/"
-
toRpath
public String toRpath(String path)
Map java path to R path (as string)- Parameters:
path- java string path- Returns:
- R path with suitable level delimiter "/"
-
getLastLogEntry
public String getLastLogEntry()
-
note_text
public void note_text(String txt)
-
note_code
public void note_code(String... code)
-
note_code
public void note_code(String code)
-
toRcode
protected static String toRcode(double[] d)
-
toRcode
protected static String toRcode(double[][] d)
-
notebook
public String notebook()
-
log
public void log(String message, RLog.Level level)
Description copied from interface:RLogSupport R messages printin
-
listCommands
public String[] listCommands()
- Returns:
- available R commands
-
setRepository
public void setRepository(String url)
- Parameters:
url- CRAN repository to use for packages installation (eg http://cran.r-project.org)
-
setCRANRepository
public void setCRANRepository(String url)
-
getRepository
public String getRepository()
- Returns:
- CRAN repository used for packages installation
-
isPackageLoaded
public boolean isPackageLoaded(String pack)
Check for package loaded in R environment.- Parameters:
pack- R package name- Returns:
- package loading status
-
isPackageInstalled
public boolean isPackageInstalled(String pack, String version)
Check for package installed in R environment.- Parameters:
pack- R package nameversion- R package version- Returns:
- package loading status
-
installPackages
public String installPackages(String[] pack, boolean load)
Start installation procedure of R packages- Parameters:
pack- packages to installload- automatically load packages after successfull installation- Returns:
- installation status
-
installPackage
public String installPackage(File pack, boolean load)
Start installation procedure of local R package- Parameters:
pack- package file to installload- automatically load package after successfull installation- Returns:
- installation status
-
installPackage
public String installPackage(String pack, File dir, boolean load)
Start installation procedure of local R package- Parameters:
pack- package to installdir- directory where package file (.zip, .tar.gz or .tgz) is located.load- automatically load package after successfull installation- Returns:
- installation status
-
installPackage
public String installPackage(String pack, boolean load)
Start installation procedure of CRAN R package- Parameters:
pack- package to installload- automatically load package after successfull installation- Returns:
- installation status
-
loadPackage
public String loadPackage(String pack)
load R backage using library() command- Parameters:
pack- R package name- Returns:
- loading status
-
getLastOutput
public String getLastOutput()
-
getLastError
public String getLastError()
-
silentlyVoidEval
protected boolean silentlyVoidEval(String expression)
Silently (ie no log) launch R command without return value. Encapsulate command in try() to cacth errors- Parameters:
expression- R expresison to evaluate- Returns:
- well evaluated ?
-
silentlyVoidEval
protected abstract boolean silentlyVoidEval(String expression, boolean tryEval)
Silently (ie no log) launch R command without return value.- Parameters:
expression- R expresison to evaluatetryEval- encapsulate command in try() to cacth errors- Returns:
- well evaluated ?
-
silentlyRawEval
protected Object silentlyRawEval(String expression)
Silently (ie no log) launch R command and return value. Encapsulate command in try() to cacth errors.- Parameters:
expression- R expresison to evaluate- Returns:
- REXP R expression
-
silentlyRawEval
protected abstract Object silentlyRawEval(String expression, boolean tryEval)
Silently (ie no log) launch R command and return value.- Parameters:
expression- R expression to evaluatetryEval- encapsulate command in try() to cacth errors- Returns:
- REXP R expression
-
rawEval
protected Object rawEval(String expression, boolean tryEval)
Launch R command and return value.- Parameters:
expression- R expresison to evaluatetryEval- encapsulate command in try() to cacth errors- Returns:
- REXP R expression
-
rawEval
protected Object rawEval(String expression)
Launch R command and return value. Encapsulate command in try() to cacth errors.- Parameters:
expression- R expresison to evaluate- Returns:
- REXP R expression
-
voidEval
public boolean voidEval(String expression, boolean tryEval) throws Rsession.RException
Launch R command without return value.- Parameters:
expression- R expresison to evaluatetryEval- encapsulate command in try() to cacth errors- Returns:
- well evaluated ?
- Throws:
Rsession.RException- Could not eval
-
voidEval
public boolean voidEval(String expression) throws Rsession.RException
Launch R command without return value. Encapsulate command in try() to catch R errors.- Parameters:
expression- R expresison to evaluate- Returns:
- well evaluated ?
- Throws:
Rsession.RException- Could not eval
-
eval
public Object eval(String expression, boolean tryEval) throws Rsession.RException
- Throws:
Rsession.RException
-
eval
public Object eval(String expression) throws Rsession.RException
- Throws:
Rsession.RException
-
rmAll
public boolean rmAll(boolean all)
delete all variables in R environment- Parameters:
all- also shadow variables?- Returns:
- well removed ?
-
rmAll
public boolean rmAll()
-
buildList
public static String buildList(String... vars)
create a R list with given R objects- Parameters:
vars- R object names- Returns:
- list expression
-
buildListString
public static String buildListString(String... vars)
create a R list with given R strings- Parameters:
vars- R strings- Returns:
- String list expression
-
buildListPattern
public static String buildListPattern(String... vars)
create a R list with given R string patterns- Parameters:
vars- R string patterns- Returns:
- ls pattern expression
-
gethomedir
public abstract String gethomedir()
-
getwd
public String getwd()
-
setwd
public void setwd(File wdir)
-
source
public void source(File f)
loads R source file (eg ".R" file)- Parameters:
f- ".R" file to source
-
load
public void load(File f)
loads R data file (eg ".Rdata" file)- Parameters:
f- ".Rdata" file to load
-
ls
public String[] ls()
-
ls
public String[] ls(boolean all)
list R variables in R env.- Parameters:
all- - If TRUE, all object names are returned. If FALSE, names which begin with a . are omitted.- Returns:
- list of R objects names
-
ls
public String[] ls(String... vars)
list R variables in R env. matching patterns- Parameters:
vars- R object name patterns- Returns:
- list of R objects names
-
rm
public boolean rm(String... vars) throws Rsession.RException
delete R variables in R env.- Parameters:
vars- R objects names- Returns:
- well removed ?
- Throws:
Rsession.RException- Could not do rm
-
rmls
public boolean rmls(String... vars) throws Rsession.RException
delete R variables in R env. matching patterns- Parameters:
vars- R object name patterns- Returns:
- well removed ?
- Throws:
Rsession.RException- Could not do rm
-
getFileFromWorkspace
public abstract void getFileFromWorkspace(File f)
-
save
public void save(File f, String... vars) throws Rsession.RException
Save R variables in data file- Parameters:
f- file to store data (eg ".Rdata")vars- R variables to save- Throws:
Rsession.RException- Could not do save
-
savels
public void savels(File f, String... vars) throws Rsession.RException
Save R variables in data file- Parameters:
f- file to store data (eg ".Rdata")vars- R variables names patterns to save- Throws:
Rsession.RException- Could not do save
-
typeOf
public String typeOf(String robject)
- Parameters:
robject- R object name- Returns:
- R type of object
-
unset
public boolean unset(String... varname) throws Rsession.RException
delete R object in R env.- Parameters:
varname- R objects to delete- Returns:
- succeeded ?
- Throws:
Rsession.RException- Could not unset
-
unset
public boolean unset(Collection varname) throws Rsession.RException
delete R object in R env.- Parameters:
varname- R objects to delete- Returns:
- succeeded ?
- Throws:
Rsession.RException- Could not unset
-
set
public boolean set(Map<String,Object> _vars) throws Rsession.RException
Set R object in R env.- Parameters:
_vars- R objects to set as key/values- Returns:
- succeeded ?
- Throws:
Rsession.RException- Could not set one var
-
set
public abstract boolean set(String varname, double[][] data, String... names) throws Rsession.RException
Set R data.frame in R env.- Parameters:
varname- R list namedata- numeric data in listnames- names of columns- Returns:
- succeeded ?
- Throws:
Rsession.RException- Could not set
-
set
public abstract boolean set(String varname, Object var) throws Rsession.RException
Set R object in R env.- Parameters:
varname- R object namevar- R object value- Returns:
- succeeded ?
- Throws:
Rsession.RException- Could not set
-
reshapeAsRow
protected static double[] reshapeAsRow(double[][] a)
-
reshapeAsRow
protected static double[] reshapeAsRow(Double[][] a)
-
t
protected double[][] t(double[][] m)
-
asDouble
public abstract double asDouble(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asArray
public abstract double[] asArray(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asMatrix
public abstract double[][] asMatrix(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asString
public abstract String asString(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asStrings
public abstract String[] asStrings(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asInteger
public abstract int asInteger(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asIntegers
public abstract int[] asIntegers(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asLogical
public abstract boolean asLogical(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asLogicals
public abstract boolean[] asLogicals(Object o) throws ClassCastException
- Throws:
ClassCastException
-
asList
public abstract Map asList(Object o) throws ClassCastException
- Throws:
ClassCastException
-
isNull
public abstract boolean isNull(Object o)
-
cast
public abstract Object cast(Object o) throws ClassCastException
- Throws:
ClassCastException
-
toGraphic
public void toGraphic(File f, int width, int height, String fileformat, String... commands)
Create a file for R graphical command output- Parameters:
f- File to store data (eg .jpg file)width- width of imageheight- height of imagefileformat- format of image: png,tiff,jpeg,bmpcommands- R command to create image (eg plot())
-
asR2HTML
public String asR2HTML(String command)
Get R command text output in HTML format- Parameters:
command- R command returning text- Returns:
- HTML string
-
asHTML
public String asHTML(String command)
Get R command text output in HTML format- Parameters:
command- R command returning text- Returns:
- HTML string
-
print
public String print(String command)
Get R command text output- Parameters:
command- R command returning text- Returns:
- String
-
proxyEval
public Object proxyEval(String expression, Map<String,Object> vars) throws Rsession.RException
Method to rawEval expression. Holds many optimizations (@see noVarsEvals) and turn around for reliable usage (like engine auto restart). 1D Numeric "vars" are replaced using Java replace engine instead of R one. Intended to not interfer with current R env vars. Yes, it's hard-code :)- Parameters:
expression- String to evaluatevars- HashMap<String, Object> vars inside expression. Passively overload current R env variables.- Returns:
- java castStrict Object Warning, UNSTABLE and high CPU cost.
- Throws:
Rsession.RException- Could not proxyEval
-
setGlobalEnv
public abstract void setGlobalEnv(String envName)
-
getGlobalEnv
public String getGlobalEnv()
-
copyGlobalEnv
public abstract void copyGlobalEnv(String newEnvName)
-
-