de.jstacs.utils
Class REnvironment

java.lang.Object
  extended by de.jstacs.utils.REnvironment

public class REnvironment
extends Object

This is an environment class for R and Rserve that helps you to handle all things in a more gentle way.

Author:
Jens Keilwagen
See Also:
RUtils

Constructor Summary
REnvironment()
          This constructor creates a connection to an R instance on the "localhost" with empty login and password.
REnvironment(String server, int port, String user, String passwd)
          Creates a new REnvironment with initial size 10 using the given port.
REnvironment(String server, int port, String user, String passwd, int initSize)
          Creates a new REnvironment using the given port.
REnvironment(String server, String user, String passwd)
          Creates a new REnvironment with initial size 10 using the default port.
REnvironment(String server, String user, String passwd, int initSize)
          Creates a new REnvironment using the default port.
 
Method Summary
 void close()
          Closes the REnvironment and removes all files from the server.
 boolean copyFileFromServer(String serverFileName, String clientFileName, boolean overwriteExistingFile)
          Copies a file from the server to the local machine.
 boolean copyFileToServer(String clientFileName, String serverFileName, boolean overwriteExistingFile)
          Copies a file from the local machine to the server.
 REXP createMatrix(String matrixName, double[][] matrix)
          Creates a matrix of doubles.
 REXP createMatrix(String matrixName, int[][] matrix)
          Creates a matrix of ints.
 REXP createVector(String vectorName, double[] vector)
          Creates a vector of doubles.
 REXP createVector(String vectorName, int[] vector)
          Creates a vector of ints.
 REXP createVector(String vectorName, long[] vector)
          Creates a vector of longs.
 REXP createVector(String vectorName, String[] vector)
          Creates a vector of Strings.
 void deleteAllFilesAtTheServer()
          Deletes all files that have been copied to the server or created on the server.
 REXP eval(String cmd)
          Evaluates the String as R commands.
protected  void finalize()
           
 String getVersionInformation()
          Returns information about the version of R that is used.
 void installScript(String clientFileName, String serverFileName, boolean overwriteExistingFile)
          Installs a script on the server.
 BufferedImage plot(String pltcmd)
          Creates a buffered image from a given plot command.
 BufferedImage plot(String pltcmd, double width, double height)
          Creates a buffered image with given dimension from a given plot command.
 void plot(String pltcmd, double width, double height, String formatName, OutputStream out)
          This method creates an image with given dimension from a given plot command and pipes it to the output stream.
 boolean plotToPDF(String pltcmd, double width, double height, String fileName, boolean overwriteExistingFile)
          Creates a pdf file with given dimension from a given plot command.
 boolean plotToPDF(String pltcmd, String fileName, boolean overwriteExistingFile)
          Creates a pdf file from a given plot command.
 boolean plotToTexFile(String pltcmd, double width, double height, String fileName, boolean overwriteExistingFile)
          Creates a tex file with given dimension from a given plot command.
 boolean plotToTexFile(String pltcmd, String fileName, boolean overwriteExistingFile)
          Creates a tex file from a given plot command.
static JFrame showImage(String title, BufferedImage img)
          Enables you to show an image.
static JFrame showImage(String title, BufferedImage img, int defaultCloseOperation)
          Enables you to show an image.
 void voidEval(String cmd)
          Evaluates the String as R commands.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

REnvironment

public REnvironment()
             throws Exception
This constructor creates a connection to an R instance on the "localhost" with empty login and password.

Throws:
Exception - if something went wrong
See Also:
REnvironment(String, String, String)

REnvironment

public REnvironment(String server,
                    String user,
                    String passwd,
                    int initSize)
             throws Exception
Creates a new REnvironment using the default port.

Parameters:
server - the name of the server
user - the login
passwd - the password for the login
initSize - the initial number of slots for files on the server (has to be at least 1)
Throws:
Exception - if something went wrong

REnvironment

public REnvironment(String server,
                    String user,
                    String passwd)
             throws Exception
Creates a new REnvironment with initial size 10 using the default port.

Parameters:
server - the name of the server
user - the login
passwd - the password for the login
Throws:
Exception - if something went wrong
See Also:
REnvironment(String, String, String, int)

REnvironment

public REnvironment(String server,
                    int port,
                    String user,
                    String passwd,
                    int initSize)
             throws Exception
Creates a new REnvironment using the given port.

Parameters:
server - the name of the server
port - the port
user - the login
passwd - the password for the login
initSize - the initial number of slots for files on the server (has to be at least 1)
Throws:
Exception - if something went wrong

REnvironment

public REnvironment(String server,
                    int port,
                    String user,
                    String passwd)
             throws Exception
Creates a new REnvironment with initial size 10 using the given port.

Parameters:
server - the name of the server
port - the port
user - the login
passwd - the password for the login
Throws:
Exception - if something went wrong
See Also:
REnvironment(String, String, String, int)
Method Detail

copyFileFromServer

public boolean copyFileFromServer(String serverFileName,
                                  String clientFileName,
                                  boolean overwriteExistingFile)
                           throws Exception
Copies a file from the server to the local machine.

Parameters:
serverFileName - name of the file on the server
clientFileName - name of the file on the client (local machine)
overwriteExistingFile - whether to overwrite the file, if already existing on the client
Returns:
true if the file is copied successfully
Throws:
Exception - if something went wrong

copyFileToServer

public boolean copyFileToServer(String clientFileName,
                                String serverFileName,
                                boolean overwriteExistingFile)
                         throws Exception
Copies a file from the local machine to the server.

Parameters:
serverFileName - name of the file on the server
clientFileName - name of the file on the client (local machine)
overwriteExistingFile - whether to overwrite the file, if already existing on the server
Returns:
true if the file is copied successfully
Throws:
Exception - if something went wrong

close

public void close()
           throws Exception
Closes the REnvironment and removes all files from the server.

Throws:
Exception - if something went wrong

createMatrix

public REXP createMatrix(String matrixName,
                         int[][] matrix)
                  throws RserveException,
                         IllegalArgumentException
Creates a matrix of ints.

Parameters:
matrixName - the name of the matrix
matrix - the matrix of ints
Returns:
an R expression
Throws:
RserveException - if something with Rserve went wrong
IllegalArgumentException - if matrix[i].length != matrix[j].length

createMatrix

public REXP createMatrix(String matrixName,
                         double[][] matrix)
                  throws RserveException,
                         IllegalArgumentException
Creates a matrix of doubles.

Parameters:
matrixName - the name of the matrix
matrix - the matrix of doubles
Returns:
an R expression
Throws:
RserveException - if something with Rserve went wrong
IllegalArgumentException - if matrix[i].length != matrix[j].length

createVector

public REXP createVector(String vectorName,
                         String[] vector)
                  throws RserveException
Creates a vector of Strings.

Parameters:
vectorName - the name of the vector
vector - the vector of Strings
Returns:
an R expression
Throws:
RserveException - if something with Rserve went wrong

createVector

public REXP createVector(String vectorName,
                         int[] vector)
                  throws RserveException
Creates a vector of ints.

Parameters:
vectorName - the name of the vector
vector - the vector of ints
Returns:
an R expression
Throws:
RserveException - if something with Rserve went wrong

createVector

public REXP createVector(String vectorName,
                         long[] vector)
                  throws RserveException
Creates a vector of longs.

Parameters:
vectorName - the name of the vector
vector - the vector of longs
Returns:
an R expression
Throws:
RserveException - if something with Rserve went wrong

createVector

public REXP createVector(String vectorName,
                         double[] vector)
                  throws RserveException
Creates a vector of doubles.

Parameters:
vectorName - the name of the vector
vector - the vector of doubles
Returns:
an R expression
Throws:
RserveException - if something with Rserve went wrong

deleteAllFilesAtTheServer

public void deleteAllFilesAtTheServer()
                               throws Exception
Deletes all files that have been copied to the server or created on the server.

Throws:
Exception - if something went wrong

eval

public REXP eval(String cmd)
          throws Exception
Evaluates the String as R commands.

Parameters:
cmd - the String to be evaluated
Returns:
the result from R
Throws:
Exception - if something went wrong

getVersionInformation

public String getVersionInformation()
                             throws Exception
Returns information about the version of R that is used.

Returns:
information about the version of R that is used
Throws:
Exception - if something went wrong

installScript

public void installScript(String clientFileName,
                          String serverFileName,
                          boolean overwriteExistingFile)
                   throws Exception
Installs a script on the server.

Parameters:
clientFileName - the name of the scriptfile on the client
serverFileName - the name of the scriptfile on the server
overwriteExistingFile - if true the method is enabled to overwrite an existing file
Throws:
Exception - if something went wrong

plot

public BufferedImage plot(String pltcmd)
                   throws Exception
Creates a buffered image from a given plot command.

If you use a java version below 1.6 your image is internally encoded as jpeg.
If you use a java version at least 1.6 your image is internally encoded as png.

Parameters:
pltcmd - the plot command
Returns:
the buffered image
Throws:
Exception - if something went wrong
See Also:
plot(String, double, double), showImage(String, BufferedImage), ImageIO.write(java.awt.image.RenderedImage, String, File)

plot

public BufferedImage plot(String pltcmd,
                          double width,
                          double height)
                   throws Exception
Creates a buffered image with given dimension from a given plot command.

If you use a java version below 1.6 your image is internally encoded as jpeg.
If you use a java version at least 1.6 your image is internally encoded as png.

Parameters:
pltcmd - the plot command
width - the width of the image (in pixel)
height - the height of the image (in pixel)
Returns:
the buffered image
Throws:
Exception - if something went wrong
See Also:
showImage(String, BufferedImage), ImageIO.write(java.awt.image.RenderedImage, java.lang.String, java.io.File)

plot

public void plot(String pltcmd,
                 double width,
                 double height,
                 String formatName,
                 OutputStream out)
          throws IOException,
                 Exception
This method creates an image with given dimension from a given plot command and pipes it to the output stream.

Parameters:
pltcmd - the plotting command
width - the width of the image
height - the height of the image
formatName - the name of the image format used for writing the image (see ImageIO)
out - the OutputStream used for writing the image
Throws:
IOException - see ImageIO.write(java.awt.image.RenderedImage, String, OutputStream)
Exception - see plot(String, double, double)
See Also:
ImageIO.write(java.awt.image.RenderedImage, String, OutputStream)

plotToPDF

public boolean plotToPDF(String pltcmd,
                         String fileName,
                         boolean overwriteExistingFile)
                  throws Exception
Creates a pdf file from a given plot command.

Parameters:
pltcmd - the plot command
fileName - the name of the pdf file
overwriteExistingFile - if true the method is enabled to overwrite an existing file
Returns:
true if the plot is done
Throws:
Exception - if something went wrong

plotToPDF

public boolean plotToPDF(String pltcmd,
                         double width,
                         double height,
                         String fileName,
                         boolean overwriteExistingFile)
                  throws Exception
Creates a pdf file with given dimension from a given plot command.

Parameters:
pltcmd - the plot command
width - the width of the image
height - the height of the image
fileName - the name of the pdf file
overwriteExistingFile - if true the method is enabled to overwrite an existing file
Returns:
true if the plot is done
Throws:
Exception - if something went wrong

plotToTexFile

public boolean plotToTexFile(String pltcmd,
                             String fileName,
                             boolean overwriteExistingFile)
                      throws Exception
Creates a tex file from a given plot command.

Parameters:
pltcmd - the plot command
fileName - the name of the tex file
overwriteExistingFile - if true the method is enabled to overwrite an existing file
Returns:
true if the plot is done
Throws:
Exception - if something went wrong

plotToTexFile

public boolean plotToTexFile(String pltcmd,
                             double width,
                             double height,
                             String fileName,
                             boolean overwriteExistingFile)
                      throws Exception
Creates a tex file with given dimension from a given plot command.

Parameters:
pltcmd - the plot command
width - the width of the image
height - the height of the image
fileName - the name of the tex file
overwriteExistingFile - if true the method is enabled to overwrite an existing file
Returns:
true if the plot is done
Throws:
Exception - if something went wrong

voidEval

public void voidEval(String cmd)
              throws Exception
Evaluates the String as R commands.

Parameters:
cmd - the String to be evaluated
Throws:
Exception - if something went wrong

finalize

protected void finalize()
                 throws Throwable
Overrides:
finalize in class Object
Throws:
Throwable

showImage

public static JFrame showImage(String title,
                               BufferedImage img)
                        throws InterruptedException
Enables you to show an image.

Parameters:
title - the title of the frame
img - the image
Returns:
a frame showing the image
Throws:
InterruptedException - if the process is interrupted
See Also:
showImage(String, BufferedImage, int)

showImage

public static JFrame showImage(String title,
                               BufferedImage img,
                               int defaultCloseOperation)
                        throws InterruptedException
Enables you to show an image.

Parameters:
title - the title of the frame
img - the image
defaultCloseOperation - the variable used to control the window-closing operation
Returns:
a frame showing the image
Throws:
InterruptedException - if the process is interrupted
See Also:
WindowConstants