Skip to main content
RSS feed Subscribe to feed

 

Skip Navigation LinksHome  Extending the Spotfire Platform  Common Tasks and Idioms  Spotfire Statistics Services Primer (C#)  Spotfire Statistics Services Expression Client

©Spotfire 2011

Spotfire Statistics Services Expression Client

Code example explaining how to use Spotfire Statistics Services Expression Client C# API.

Overview

You can use the Expression Client API to send S expressions directly to the S engine on the server. The Expression Client API contains four overloads of the Eval() function to provide flexibility in sending jobs to the server:

  • You can run a job by passing the expression and indicating whether to run it asynchronously.
  • You can run an asychronous job, passing the expression and the date and time to begin the run. If an S engine is available in the engine pool, the job runs at the specified time. (After the job has run, you can use the job ID, returned in the SplusDataResult object, to find the results.)

For either of these options, you can pass an SplusDataRequest object containing a list of S-PLUS objects and/or encapsulated data for input arguments for evaluation. This option does not require reading data from a server repository.

When you run a sychronous job, the server runs the job on the first available engine, and when the run is complete, it returns an SplusDataResult object containing data, a path to any image, text, any errors or warnings, and (optionally) detailed log files.

When you run an asynchronous job or a scheduled job, the server returns an SplusDataResult object containing only the job ID and status.

The following example demonstrates identifying a server, creating an object, validating the user, sending an expression, catching exceptions, and evaluting the jobs. This example demonstrates working from the command line. You can use it to test the API, and then you can adapt it to work with your .NET GUI application.

Example

  1. Set the path to the server and authentication credentials:
    string serviceUrl = "http://localhost:8080/SplusServer";
    string username   = "myusername";
    string password   = "mypassword";
  2. Create the ExpressionService client object:
    IExpressionClient client = ClientFactory.GetExpressionClient(serviceUrl, username, password);
  3. Construct a string containing the expression to evaluate. This expression sorts the values in a vector x and returns the sort order:
    string expr = "sort(x)";
  4. Construct an SplusDataRequest object containing the function input arguments. This request consists of a numeric vector x=c(5,3,4):
    Argument arg = new Argument("x",
          new Splus(
             new DisplayOptions(),
             new Header(),
             new SplusObject[] {
                new Vector(DataType.numeric, new Item("5"), new Item("3"), new Item("4"))
             }
             ));
       SplusDataRequest request = new SplusDataRequest(arg);
  5. Send the expression to evaluate synchronously and return the results:
    SplusDataResult result = Client.Eval(expr, new SynchronousJobStartup(3000), request);
  6. Return error or warning, if necessary:
    if (job.HasWarnings)
    	Console.WriteLine("Warnings: " + job.Warnings + "\n");
    if (job.HasError)
    	Console.WriteLine("Error: " + job.Error + "\n");
  7. Examine any console text output, for instance logs:
    if (job.HasTextOutput)
    	Console.WriteLine(job.TextOutput + "\n");

See also: