Skip to content

How to use R with SPSS

Updated 

Environment

R

SPSS 27

SPSS 28

Solution

Mac Big Sur note:  SPSS Statistics may fail to load R libraries when you attempt to run R extensions or any R plug-in syntax on Big Sur. A workaround is to rename the libc++.1.dylib and libc++abi.1.dylib libraries (for example, change the filename extensions to '.dylibx'). These libraries are under the R 3.5 or 3.6 lib folder (for example, /Library/Frameworks/R.framework/Versions/3.6/Resources/lib). Renaming the libraries prevents older R library versions from loading and ensures that the correct system libraries are loaded.

You can run R programs within SPSS to analyze the data files you have opened in SPSS. R is a free software environment for statistical analysis, graphics, and more.

Initial Setup:

To be able to run R programs within SPSS, you must perform the following one-time setup procedures:

  • For SPSS 28:

    • Windows:  No setup is required.
    • Mac:  Users must manually download and install R 4.0 and XQuartz.

    Afterward, an easily configurable R runtime environment (R version 4.0.5) will be available that is configured for use with SPSS Statistics, and you can enter R code directly in the runtime environment console. It is accessed by clicking the R for SPSS Statistics option in the Start menu for Windows or Launchpad for macOS. See also Running R commands within SPSS below.

  • For SPSS 27:

    1. You must have R version 3.6.x installed. This is available at www.r-project.org
    2. Then perform these two steps:
      • First, install the SPSS Statistics R Configuration Extension according to these instructions. In those instructions, when you visit the Extension Hub, you can search for 'STATS_R' to quickly locate the R extensions in the displayed list. Select the extension that matches your installed version of R (i.e., STATS_R36).
      • Then configure the SPSS Statistics Integration Plug-in for R according to these instructions. You will be asked to specify your computer's R home directory during this process. Here are their standard locations (they may differ on your computer), using R version 3.6.3 as an example:
        • Windows:  C:\Program Files\R\R-3.6.3
        • Mac:  /Library/Frameworks/R.framework/Versions/3.6/Resources

Running R commands within SPSS:

(You must first perform any one-time setup procedures noted just above.)

To use R commands within SPSS, open an SPSS Syntax window (from the SPSS menus, select File / New / Syntax). Then type your R commands in this window, sandwiched between the "BEGIN PROGRAM R." and the "END PROGRAM." commands. Note that there is a period "." at the end of those two commands (but your R commands obey the usual R syntax rules). Here is a simple example:

BEGIN PROGRAM R.
x <- 2 + 5
cat("the value of x is", x)
END PROGRAM.

To execute those commands, display the "Run" menu in the Syntax window and select "All," which instructs SPSS to execute all commands in the Syntax window. Any resulting output will typically be displayed in your SPSS Viewer window. (Note:  The begin and end program commands can be typed in lowercase.)

To execute a subset of the commands in the Syntax window, select (highlight) them with the mouse and then click the green triangular "Run Selection" toolbar button (or display the "Run" menu and choose "Selection"). Note that if you select and run any portion of an R "sandwich" (that is, the "BEGIN PROGRAM R." command, its subsequent "END PROGRAM." command, and the R commands in between), the entire sandwich will be executed. 

To save your Syntax window commands to a file, select File / Save from the menus. To re-use those commands in the future, re-open the file in a new Syntax window (from the menus of any SPSS window, select File / Open / Syntax and browse for your file).

Your R commands can read and write data from and to a data file that you have opened within SPSS (i.e., in an SPSS Data Editor window). See, for example, Using R from SPSS (see also the general discussion at the top of that document).

Addional information:

For additional information, go to the Support section of IBM SPSS Statistics and select the online documentation link for your version of SPSS, ensure that Show full table of contents is selected, and then view the two topics entitled Integration Plug-in for R and Working with R.

Note that SPSS 28 has a new "Workbook" mode, which facilitates using the Syntax and Viewer windows. See Viewer Workbook for more info.