Package org.math.R

Class Rsession

    • 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()
      • addLogger

        public void addLogger​(RLog l)
      • removeLogger

        public void removeLogger​(RLog l)
      • closeLog

        public void closeLog()
        Specified by:
        closeLog in interface RLog
      • end

        public void end()
      • addBusyListener

        public void addBusyListener​(BusyListener b)
      • removeBusyListener

        public void removeBusyListener​(BusyListener b)
      • setBusy

        public void setBusy​(boolean bb)
      • 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_header

        public void note_header​(String title,
                                String author)
      • 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)
      • toRcode

        protected static String toRcode​(Double[][] d)
      • toRcode

        protected static String toRcode​(Map l)
      • notebook

        public String notebook()
      • log

        public void log​(String message,
                        RLog.Level level)
        Description copied from interface: RLog
        Support R messages printin
        Specified by:
        log in interface RLog
        Parameters:
        message - to log
        level - Level
      • 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 name
        version - 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 install
        load - 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 install
        load - 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 install
        dir - 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 install
        load - 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 evaluate
        tryEval - 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 evaluate
        tryEval - 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 evaluate
        tryEval - 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 evaluate
        tryEval - 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
      • 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)
      • putFileInWorkspace

        public abstract File putFileInWorkspace​(File f)
      • local2remotePath

        public File local2remotePath​(File localpath)
      • remote2localPath

        public File remote2localPath​(File remotepath)
      • 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​(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
      • 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)
      • 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
      • 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 name
        data - numeric data in list
        names - names of columns
        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)
      • isNull

        public abstract boolean isNull​(Object o)
      • 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 image
        height - height of image
        fileformat - format of image: png,tiff,jpeg,bmp
        commands - R command to create image (eg plot())
      • toGraphic

        public void toGraphic​(File f,
                              int width,
                              int height,
                              String... commands)
      • toJPEG

        public void toJPEG​(File f,
                           int width,
                           int height,
                           String... commands)
      • toPNG

        public void toPNG​(File f,
                          int width,
                          int height,
                          String... commands)
      • toBMP

        public void toBMP​(File f,
                          int width,
                          int height,
                          String... commands)
      • toTIFF

        public void toTIFF​(File f,
                           int width,
                           int height,
                           String... commands)
      • 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 evaluate
        vars - 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)