此例子是我再SPLUS的自带的例子上修改而来,对初学者具有较高的阅读价值
// spllm.cxx : a simple console application that illustrates how to
// 1. create a connection to S-PLUS.
// 2. create permanent objects in S database.
// 3. evaluate an S expression.
//
// Steps to build and test this program from a DOS prompt.
// 1. Change current directory to directory containing this file
// cd <directory containing this file>
// 2. Build the program
// msdev spllm.dsp /make
// 3. Run the program
// spllm.exe S_HOME=%SHOME%
// 4. Run the S-PLUS console
// %SHOME%/cmd/sqpe.exe
// 5. Look for S objects: x, y and z. For example,
// > objects()
#include "sconnect.h"
#include "iostream"
#include "string"
using namespace std;
//A global connection object
CSPengineConnect g_engineConnect;
int main(int argc, char* argv[])
{
//Create the connection to S-PLUS
g_engineConnect.Create( argc, argv);
//Create S object with name "x" in the current database.
//Same as x<-1:10 at the command line.
string x;
cout<<"输入数目:";
cin>>x;
x="1:"+x;
CSPnumeric sx;
sx.Create(x.c_str(),"x");
//Squaring sx, which is the same as S expression sy <- x*x in a local frame,
//but here we set it to local C++ variable sy.
CSPnumeric sy = sx * sx;
int i ;
for( i=0;i<sy.length();i++)
cout<<sy[i]<<endl;
// Assign the result as S object with name "y" in the current database.
sy.Assign("y");
//Evaluate z<-lm(y~x)
g_engineConnect.SyncParseEval("z<-lm(y~x)");
CSPlist result = g_engineConnect.get("z");
CSPnumeric rank=result.GetAt("rank");
cout<<"rank:";
for( i=0;i<rank.length();i++)
cout<<rank[i];
cout<<endl;
CSPcharacter call=result.GetAt("call");
cout<<"回归表达式:" <<(char *)call(1) <<"=";
for( i=1;i<call.length();i++)
cout<<(char *)call[i];
cout<<endl;
CSPmatrix coefficients =result.GetAt("coefficients");
cout<<"回归截距:"<<(double)coefficients(1,1)<<endl;
cout<<"回归系数:"<<(double)coefficients(2,1)<<endl;
return 1;
}