Introduction
This article describes how to import an SPSS data set into Displayr using R.
Requirements
- An SPSS (*.sav) data set made available via a URL or saved in the Displayr Cloud Drive. In this example, we have a technology data set saved on a website.
- A Displayr document.
Please note these steps require a Displayr license.
Method 1 - foreign R package
1. Select Data Sources > Plus (+) > R.
2. Enter a name for the data set under Name.
3. Paste the below R code where it states "Enter your R code here":
library(foreign)
location = "https://wiki.q-researchsoftware.com/images/3/35/Technology_2018.sav"
dataset = suppressWarnings(read.spss(location,
use.value.labels = TRUE,
to.data.frame = TRUE))
Here, we are using the use.value.labels argument to import with value labels displayed.
4. Note this will import using the variable names. Variable labels are not returned so you will need to update these directly in your Data Sets tree.
Method 2 - haven R package
This method additionally supports SPSS's compressed data files (*.zsav).
1. Select Data Sources> Plus (+) > R.
2. Enter a name for the data set under Name.
3. Paste the below R code where it states "Enter your R code here":
library(haven)
location = "https://wiki.q-researchsoftware.com/images/3/35/Technology_2018.sav"
dataset = read_sav(location)
# Replace values with labels
dataset = as_factor(dataset)
Here we use the as_factor function to replace the values with labels.
Method 3 - flipAPI R package
This method uses our own package to import a data set already loaded into the Displayr Cloud Drive. In general, the best way to do this is to use the non-code solution outlined in How to Import Data from the Displayr Cloud Drive. However, the below example is for when you wish to perform additional data transformations via R code on your data set.
1. Select Data Sources > Plus (+) > R.
2. Enter a name for the data set under Name.
3. Paste the below R code where it states "Enter your R code here":
library(flipAPI)
dataset = QLoadData("Technology_2018.sav")
See also How to Access the Displayr Cloud Drive Using R.
Technical Details
- Methods 2 and 3 both use the haven R package which is designed for SPSS file manipulation.
- Haven imports the variables with some extra metadata, and it uses a class called "haven_labelled" to manage this metadata.
- "haven_labelled" does not always play nicely with additional functions from other R packages, such as reshape and reshape2.
- In such cases, if you can't use the foreign R package instead, one workaround is to de-class the data set to remove this extra class by adding the following line of code:
dataset = as.data.frame(lapply(dataset, unclass))