org.af.jhlir.tools
Class RCmdBatch

java.lang.Object
  extended by org.af.jhlir.tools.RCmdBatch

public class RCmdBatch
extends Object

Execute R in batch mode. Use this to query R for information / set up stuff / install packages, before you can properly connect to it through jhlir. Don't rely on this class too much and try to connect to R properly as aerly as possibly as error recognition and handling is shaky, as one can only use the textual output of the R batch script. Only prerequisite to invoke this class is knowledge of the path to R_HOME.


Field Summary
static String[] R_ARGS
           
 
Constructor Summary
RCmdBatch(File rHome)
          Constructor
RCmdBatch(String rHome)
          Constructor
 
Method Summary
 List<String> exec(List<String> input)
          Excecute a list of R commands in batch mode.
 List<String> getInstalledPackages()
          Retrieves all installed R packages on the library paths.
 RPackage getInstalledPackInfo(String pack)
          Retrieve information about an installed R package on the current library paths.
 String getLibPaths()
          Get the R lib paths, separated by the path separator of your platform.
 String getRVersion()
          Get R version as a String.
 RPackage installCranPackage(String pack)
          Installs an R package from CRAN into the defaullt library directory.
 RPackage installCranPackage(String pack, File where)
          Installs an R package from CRAN.
 RPackage installRForgePackage(String pack)
          Installs an R package from R-Forge into the defaullt library directory.
 RPackage installRForgePackage(String pack, File where)
          Installs an R package from R-Forge.
 boolean isInstalled(String pack)
          Check whether an R package was installed on the current library paths.
 Map<String,String> retrieveInfo(List<String> input, List<String> vars)
          Excecute a list of R commands in batch mode and return the contents of R variables as strings.
 String retrieveInfo(List<String> input, String var)
          Excecute a list of R commands in batch mode and return the content of a R variable as a string.
 void retrieveRInfo()
          Collects information of the used R engine and stores it in this object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

R_ARGS

public static final String[] R_ARGS
Constructor Detail

RCmdBatch

public RCmdBatch(File rHome)
Constructor

Parameters:
rHome - R Home directory.

RCmdBatch

public RCmdBatch(String rHome)
Constructor

Parameters:
rHome - Absolute Path to R Home directory.
Method Detail

exec

public List<String> exec(List<String> input)
                  throws RCmdBatchException
Excecute a list of R commands in batch mode. R CMD BATCH will write an output file, whose lines are returned as strings.

Parameters:
input - List of R commonds
Returns:
Contents of output file.
Throws:
RCmdBatchException - In case of IO errors or general errors with the R CMD BATCH process. Underlying exceptions are encapsulated as causing exceptions.

retrieveInfo

public String retrieveInfo(List<String> input,
                           String var)
                    throws RCmdBatchException
Excecute a list of R commands in batch mode and return the content of a R variable as a string. Uses exec method. !! Don't use this for too complicated stuff. At the moment the R variable must only generate a one-line string when printed!!

Parameters:
input - List of R commands
var - R variable you want to retrieve.
Returns:
Contents R variable as string when printed in R.
Throws:
RCmdBatchException - In case of IO errors or general errors with the R CMD BATCH process. Underlying exceptions are encapsulated as causing exceptions.

retrieveInfo

public Map<String,String> retrieveInfo(List<String> input,
                                       List<String> vars)
                                throws RCmdBatchException
Excecute a list of R commands in batch mode and return the contents of R variables as strings. Uses exec method. !! Don't use this for too complicated stuff. At the moment the R variables must only generate a one-line string when printed!!

Parameters:
input - List of R commands
vars - R variables you want to retrieve.
Returns:
Contents R variables as strings when printed in R.
Throws:
RCmdBatchException - In case of IO errors or general errors with the R CMD BATCH process. Underlying exceptions are encapsulated as causing exceptions.

getInstalledPackInfo

public RPackage getInstalledPackInfo(String pack)
                              throws RCmdBatchException
Retrieve information about an installed R package on the current library paths. Uses exec method.

Parameters:
pack - R package name.
Returns:
Information about R package or null if package was not found.
Throws:
RCmdBatchException - See exec(java.util.List).

isInstalled

public boolean isInstalled(String pack)
                    throws RCmdBatchException
Check whether an R package was installed on the current library paths. Simply performs getInstalledPackInfo(pack) == null

Parameters:
pack - R package name.
Returns:
True if package was found.
Throws:
RCmdBatchException - See exec(java.util.List).

getInstalledPackages

public List<String> getInstalledPackages()
                                  throws RCmdBatchException
Retrieves all installed R packages on the library paths. Uses exec method.

Returns:
List of installed R package names.
Throws:
RCmdBatchException - See exec(java.util.List).

installCranPackage

public RPackage installCranPackage(String pack)
                            throws CantFindPackageException,
                                   RCmdBatchException
Installs an R package from CRAN into the defaullt library directory. Uses exec method.

Parameters:
pack - R package to install.
Returns:
Package information after install.
Throws:
CantFindPackageException - If the package is not available on CRAN.
RCmdBatchException - See exec(java.util.List).

installCranPackage

public RPackage installCranPackage(String pack,
                                   File where)
                            throws CantFindPackageException,
                                   RCmdBatchException
Installs an R package from CRAN. Uses exec method.

Parameters:
pack - R package to install.
where - Directory to install the package into.
Returns:
Package information after install.
Throws:
CantFindPackageException - If the package is not available on CRAN.
RCmdBatchException - See exec(java.util.List).

installRForgePackage

public RPackage installRForgePackage(String pack)
                              throws CantFindPackageException,
                                     RCmdBatchException
Installs an R package from R-Forge into the defaullt library directory. Uses exec method.

Parameters:
pack - R package to install.
Returns:
Package information after install.
Throws:
CantFindPackageException - If the package is not available on R-Forge.
RCmdBatchException - See exec(java.util.List).

installRForgePackage

public RPackage installRForgePackage(String pack,
                                     File where)
                              throws CantFindPackageException,
                                     RCmdBatchException
Installs an R package from R-Forge. Uses exec method.

Parameters:
pack - R package to install.
where - Directory to install the package into.
Returns:
Package information after install.
Throws:
CantFindPackageException - If the package is not available on R-Forge.
RCmdBatchException - See exec(java.util.List).

retrieveRInfo

public void retrieveRInfo()
                   throws RCmdBatchException
Collects information of the used R engine and stores it in this object.

Throws:
RCmdBatchException
See Also:
this.getRVersion, this.getLibPaths

getRVersion

public String getRVersion()
Get R version as a String. Call retrieveRInfo() before this.

Returns:
R version.

getLibPaths

public String getLibPaths()
Get the R lib paths, separated by the path separator of your platform. Call retrieveRInfo() before this.

Returns:
R lib paths.