5.7 S-PLUS Integration
If you have a license for both S-PLUS and the S-PLUS integration module, Optimus RP provides python modules that enable you to interface with S-PLUS.
5.7.1 S-PLUS Desktop Integration
The Python module, pysplus enables you to interface with a windows desktop version of S-PLUS.
Pysplus enables the evaluation of S-PLUS expressions and two-way transfer of data between the Optimus RP Python environment and S-PLUS. You must have S-PLUS installed on your computer and have the S-PLUS cmd directory in your PATH in order for Python to be able to load the appropriate S-PLUS libraries. A typical location for this directory is: C:\Program Files\Insightful\splus62\cmd
You can set your PATH variable as follows:
- Right-click on your desktop My Computer icon, and select Properties.
- Select the Advanced tab.
- Click on the Environment Variables button.
- In the System Variables pane, edit the PATH variable.
- Append the path to the splus62\cmd directory to your PATH environment variable, prepending the path with a semi-colon. If S-PLUS is installed in its default location, you would append the following string to your PATH: “;C:\Program Files\Insightful\splus62\cmd”.
The pysplus python module needs to be imported with the following python command:
import pysplus
Then the following commands are available:
SYNTAX
varName = pysplus.get(String)
The String argument specifies the S-PLUS variable to be fetched. The String must be the name of a valid variable within the S-PLUS namespace, or an exception will be thrown. The data types that can be fetched back from S-PLUS are as follows:
- character
S-PLUS character data types are implemented as an array of character strings. If there is only a single character string, then a python String is returned. If there are 2 or more strings, then a list of python String values is returned.
item integer S-PLUS integer data types are implemented as an array of integers. If there is only a single integer then a python Int is returned. If there are 2 or more integers, then a list of python Int values is returned.
- numeric
S-PLUS numeric data types are implemented as an array of floating point numbers. If there is only a single numeric value, then a python Float (64-bit double precision real number) is returned. If there are 2 or more numerics, then a list of python Float values is returned.
- matrix
S-PLUS matrices can be of type Character, Integer, or Numeric. In each case, every value in the matrix is the same type. An S-PLUS matrix is returned as a list of column vectors, where each vector is a python List of the appropriate type.
- List
S-PLUS lists can contain any of the aforementioned types. When a get() is invoked on an S-PLUS list, it is returned as a python list composed of lists of the types mentioned previously.
SYNTAX
pysplus.put(String, Object)
The String argument specifies the name of the S-PLUS variable to be created inside the S-PLUS namespace. The python objects that can be put into S-PLUS are described next:
- String
If you put a python String into S-PLUS, it is represented as an S-PLUS character data type of length 1.
- Int
If you put a python Int into S-PLUS, it is represented as an S-PLUS integer data type of length 1.
- Float
If you put a python Float into S-PLUS, it is represented as an S-PLUS numeric data type of length 1.
- List of Strings, Ints or Floats
A one dimension list of Strings, Ints, or Floats of length n is represented respectively in S-PLUS as character, integer or numeric data types of length n. If Ints and Floats are mixed within a Python list, then the resulting S-PLUS data type is numeric of length n, where the integers are converted to double precision numeric values.
Any mixture of types results in a list of those types being constructed in S-PLUS.
- List of Lists
If the list represents a rectangular matrix of Strings, Ints or Floats, then an S-PLUS matrix of the appropriate type is constructed. As with 1D lists of Ints and Floats, if Ints and Floats are mixed within a 2-D list the resulting S-PLUS object will be a numeric Matrix. Matrices are stored column-wise. So one might create a 4x2 matrix, m1, as follows:
pysplus.put("ml", [[1,2,3,4],[5,6,7,8]])
Lists with depth greater than 2, or non-rectangular lists will be recursively converted into an S-PLUS list composed of types corresponding with appropriate constituent elements.
SYNTAX
pysplus.eval(String)
This function evaluates the given string as an S-PLUS expression. If an evaluation error occurs, an exception is thrown. The function does not return any values. Rather, the get() function should be used to retrieve the results of computations.
If you have the S-PLUS windows application open at the same time as Optimus RP, it may be desirable to have access to the variables that were constructed by the other application. These are not visible automatically. To refresh the database view from the S-PLUS window, you can type the command: synchronize(1). From the Python shell you can type pysplus.eval("synchronize(1)") to have access to variables created within the running desktop application.
Note that a useful syntax feature of python is to quote a multi line string with triple quotes. Note also, that backslashes are treated weirdly by S-PLUS – they need to be typed four times in path names when quoting within python. An alternative is to use forward slashes.
Below is an example of constructing a simple plot with S-plus and saving it as a JPEG image file:
import pysplus as splus
splus.put("val", 2.0) splus.eval(""" #load library for windows java functions library("winjava") x <- seq(0, 20, 0.1) y < exp(-x/10) * cos(val*x) java.graph(file="c:/test.jpg", format="JPG") plot(x, y, type = 'l') #Write graph to c:/test.jpg dev.off() y = splus.get("y") """) |
5.7.2 S-PLUS Client/Server Integration
This functionality is forthcoming.
The Python module, pysplusclient enables you to interface with a Unix-based S-PLUS server. For this to work, you must have installed the SPLUSSOAP interface on your server, and have SPLUSSOAP configured to enable you as a user.