Jason A. French

Northwestern University

Analyzing Qualtrics Data in R Using Github Packages

| Comments

Qualtrics is an online survey platform similar to SurveyMonkey that is used by researchers to collect data. Until recently, one had to manually download the data in either SPSS or .csv format, making ongoing data analysis difficult to check whether the trend of the incoming data supports the hypothesis.

Jason Bryer has recently developed an R package published to Github for downloading data from Qualtrics within R using the Qualtrics API (see his Github repo). Using this package, you can integrate your Qualtrics data with other experimental data collected in the lab and, by running an Rscript as a cronjob, get daily updates for your analyses in R. I’ll demonstrate the use of this package below.

Installing the Qualtrics Package from Github

Installing Qualtrics on Mac/Linux
1
2
3
4
5
6
7
#!/usr/bin/env Rscript
# Authors:  Jason A. French
# Email:    frenchja@u.northwestern.edu
install.packages('devtools',dependencies=TRUE)
# install.packages('RTools') # May be needed for some Windows versions.
devtools:::install_github(repo='qualtrics',username='jbryer')
require(qualtrics)

If you wanted to distribute the Rscript to colleagues with different setups, you would use something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env Rscript
# Authors:  Jason A. French
# Email:    frenchja@u.northwestern.edu
# Check for devtools
if (!require(devtools)) {
    warning('devtools not found. Attempting to install!')
    install.packages('devtools',dependencies=TRUE)
    require(devtools,character.only=TRUE)
}

# Check Qualtrics Package from Github
if (!require(qualtrics)) {
  warning('Qualtrics package not found')
  install_github(repo='qualtrics',username='jbryer')
  require(qualtrics)
}

Pulling Data from Qualtrics

First, you’ll need to find the Survey ID of the associated study on Qualtrics. Access your Account Settings and click on Qualtrics IDs. From here, copy the entire ID into your R code (e.g., SV_blahblah).

Next, load the qualtrics package in R and pull the data using getSurveyResults(). If you receive an error in R regarding API permissions, you may need to email support@qualtrics.com and request API access. The R function accesses your survey using XML.

1
2
3
4
# Get Qualtrics Survey Data
qualtrics.data <- getSurveyResults(username=qualtrics.user,
                      password=qualtrics.pass,
                      surveyid='SV_blahblah')

However, readers have pointed out that the resulting data doesn’t have your variable names. Alternatively, Trevor Kvaran points out that you can modify the getSurveyResults function to export the variable names by appending “&ExportTags=1” to the url:

Modifying getSurveyResults to pull variable names
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
getSurveyResults <- function (username, password, surveyid, truncNames = 20, startDate = NULL, endDate = NULL)
{
    url = paste("http://eu.qualtrics.com/Server...", username,
                "&Password=", password,
                "&SurveyID=", surveyid,
                "&ExportTags=1",
                "&Format=CSV",
                ifelse(is.null(startDate), "", paste("&StartDate=", startDate, sep = "")), ifelse(is.null(endDate), "", paste("&EndDate=", endDate, sep = "")), sep = "")

    t = read.csv(url)
    t<-t[-1,]
    t$X = NULL
    n = strsplit(names(t), "....", fixed = TRUE)
    for (i in 1:ncol(t)) {
        names(t)[i] = n[[i]][length(n[[i]])]
        if (nchar(names(t)[i]) > truncNames) {
            names(t)[i] = substr(names(t)[i], 1, truncNames)
        }
    }
    t
}

Again, if you are distributing the analysis to collaborators, you may want to use a setup like below, which reads the user and password securely. If you want to automate this, you’ll obviously need hardcode your username and password. At this time, I don’t believe R has a hashing mechanism for passwords like Python, meaning you’ll need to have a plaintext password.

Password Protection for the Paranoid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Get Qualtrics Survey Data
# Not sure this will work in R.app.  Only in Terminal.app
get_password <- function() {
  cat("Qualtrics Password: ")
  system("stty -echo")
  a <- readline()
  system("stty echo")
  cat("\n")
  return(a)
}

qualtrics.user <- readline(prompt='Enter your Qualtrics username')
qualtrics.pass <- get_password()
qualtrics.data <- getSurvey(username=qualtrics.user,
                      password=qualtrics.pass,
                      surveyid='SV_blahblah')

Outputting Data using xtable

Now that we have our data pulled from Qualtrics, we can analyze it as if it were a regular R data.frame. If you know your specific survey variables of interest, you’ll want to modify the code using qualtrics.data[,c('Var1','Var2)].

1
2
3
4
5
6
7
8
9
10
require(psych)
require(xtable)
qualtrics.describe <- describe(qualtrics.data)
qual.table <- xtable(qualtrics.describe)
print.xtable(qual.table,type='html',file='~/Desktop/Qualtrics.html')

# Or...
qual.lm <- lm(DV ~ IV1*IV2, data=qualtrics.data)
lm.table <- xtable(qual.lm)
print.xtable(lm.table,type="html",file='~/Desktop/Qualtrics.html',append=TRUE)

Running an Rscript

In order for your script to be run daily, it needs to be a) marked as executable (see here) and b) added to crontab. First, mark it as executable:

1
chmod +x ~/Qualtrics.R

Make sure that #!/usr/bin/env Rscript is at the top of your script!

Next, edit the system’s crontab and add the script.

1
2
3
sudo nano /etc/crontab

0 1 * * * ~/Qualtrics.R

Comments