DataObject.AddVarsFromR

Prototypes

void AddVarsFromR( Matrix mVarNames <, Matrix mVarLabels>, String sRExpr )

void AddVarsFromR( String sRExpr )

Parameters

Matrix mVarNames
A vector that specifies the names to assign to the variables in the DataObject. Each string must be a valid SAS variable name.

Matrix mVarLabels
A vector that specifies the labels to assign to the variables in the DataObject. A variable's label describes the variable. The string mVarLabels[i] specifies the label of the variable whose name is mVarNames[i]. If the mVarLabels parameter is not specified, each variable is assigned a label that is the same as its name.

String sRExpr
An R expression that is evaluated and then coerced to a data frame. The number of variables (columns) in the data frame must match the number of entries in the vectors mVarNames and mVarLabels.

Remarks

This method adds one or more variables to the DataObject using the result of evaluating an R expression. The method evaluates the R expression, coerces the result to an R data frame, and adds the variables of the data frame to the DataObject.

If you specify the parameter mVarNames, the names of the variables in the R data frame are ignored. If you do not specify the parameter mVarNames, the names of the variables in the R data frame are used when adding the variables to the DataObject. Because SAS has different variable naming rules from R, IMLPlus applies the following transformations to convert an R variable name to a valid SAS variable name:

  1. If the name is longer than 32 characters, it is truncated to 32 characters.
  2. If the first character is not valid for a SAS variable name, the character is replaced with '_'. A SAS variable name must begin with one of the following characters: 'A'-'Z', 'a'-'z', or '_'.
  3. If any of the remaining characters are not valid for a SAS variable name, they are replaced with '_'. A SAS variable name can only contain the following characters: 'A'-'Z', 'a'-'z', '0'-'9', or '_'.
  4. If the resulting name duplicates an existing name, a number is appended to the name to make it unique. If appending the number causes the length of the name to exceed 32 characters, the name is truncated to make room for the number.

For information about how IML Studio transfers data from R to IMLPlus, please refer to the topic Data Exchange Details.

Example
DataName = "Baseball";
Xvar = "no_hits";
Yvar = "no_home";

declare DataObject dobj = DataObject.CreateFromFile( DataName );

dobj.ExportToR( "df" );

submit Xvar Yvar / R;
fm <- lm( &Yvar ~ &Xvar, data=df )
endsubmit;

dobj.AddVarsFromR( {"Fit", "Residual"}, "cbind( fitted(fm), residuals(fm) )" );

declare ScatterPlot plot;
plot = ScatterPlot.Create( dobj, Xvar, Yvar );
dobj.GetVarData( Xvar, x );
dobj.GetVarData( "Fit", fit );
plot.DrawUseDataCoordinates();
plot.DrawSetPenColor( RED );
plot.DrawLine( x, fit );

plot = ScatterPlot.Create( dobj, "Fit", "Residual" );
plot.DrawUseDataCoordinates();
plot.DrawSetPenAttributes( BLUE, DASHED, 1 );
plot.DrawLine( 0, 0, 30, 0 );
plot.SetWindowPosition( 50, 50 );
See Also

DataObject.AddAnalysisVarFromR
DataObject.AddAnalysisVarsFromR
DataObject.AddVarFromR