R Syntax Rules

Within an R program block, only statements and functions recognized by R are allowed. R syntax rules differ from IBM® SPSS® Statistics syntax rules in a number of ways:

R is case-sensitive.

This includes variable names, function names, and pretty much anything else you can think of. A variable name of myRvariable is not the same as MyRVariable, and the function GetCaseCount() cannot be written as getcasecount().

R uses a less than sign followed by a dash (<-) for assignment.

For example:

var1 <- var2+1

R commands are terminated with a semi-colon or new line; continuation lines do not require special characters or indentation.

For example:

var1 <- var2+
3

is read as var1<-var2+3, since R continues to read input until a command is syntactically complete. However:

var1 <- var2
+3

will be read as two separate commands, and var1 will be set to the value of var2.

Groupings of statements are indicated by braces. Groups of statements in structures such as loops, conditional expressions, and functions are indicated by enclosing the statements in braces, as in:

while (!spssdata.IsLastSplit()){
   data <- spssdata.GetSplitDataFromSPSS()
   cat("\nCases in Split: ",length(data[,1]))
}

R Quoting Conventions

  • Strings in the R programming language can be enclosed in matching single quotes (') or double quotes ("), as in IBM SPSS Statistics.
  • To specify an apostrophe (single quote) within a string, enclose the string in double quotes. For example,

    "Joe's Bar and Grille"

    is treated as

    Joe's Bar and Grille

  • To specify quotation marks (double quote) within a string, use single quotes to enclose the string, as in

    'Categories Labeled "UNSTANDARD" in the Report'

  • In the R programming language, doubled quotes of the same type as the outer quotes are not allowed. For example,

    'Joe''s Bar and Grille'

    results in an error.

File Specifications. Since escape sequences in the R programming language begin with a backslash (\)--such as \n for newline and \t for tab--it is recommended to use forward slashes (/) in file specifications on Windows. In this regard, IBM SPSS Statistics always accepts a forward slash in file specifications.

spssRGraphics.Submit("/temp/R_graphic.jpg")

Alternatively, you can escape each backslash with another backslash, as in:

spssRGraphics.Submit("\\temp\\R_graphic.jpg")