-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathqualtricsRevision.R
77 lines (48 loc) · 2.01 KB
/
qualtricsRevision.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
library(httr)
# username = "[email protected]#nd"
surveyID = "SV_9Fjwrg4TKCSg125"
surveyID = "SV_eJmYJFlpzjQ4bv7"
apiToken = "DSfaY34hc6jFdTOHiEns1sFf4IOtfuJbB15O0Xz2"
dataCenter = 'ca1'
getQualtricsData <- function(apiToken, dataCenter, surveyID) {
library(httr)
baseUrl <- paste("https://",
dataCenter,
".qualtrics.com/API/v3/surveys/",
surveyID,
"/export-responses",
sep = "")
requestHeaders <- c("Content-Type" = "application/json",
"X-API-TOKEN" = apiToken)
startSurveyDownload = POST(url = baseUrl,
body = list("format" = "csv",
"compress" = "false"),
add_headers(.headers = requestHeaders),
encode = "json")
requestID <- content(startSurveyDownload)$`result`$`progressId`
progressLink <- paste(baseUrl, "/", requestID,
sep = "")
# The following gets reran:
fileIDFunction <- function() {
progressComplete <- GET(url = progressLink,
add_headers(.headers = requestHeaders))
fileID <- content(progressComplete)$`result`$`fileId`
return(fileID)
}
fileID <- fileIDFunction()
while(is.null(fileID)) {
fileID <- fileIDFunction()
}
downloadLink <- paste(baseUrl, "/",
fileID, "/file",
sep = "")
downloadData <- GET(url = downloadLink,
add_headers(.headers = requestHeaders), progress())
out <- data.table::fread(content(downloadData, as = "text"))
keepVariables <- grep("responseID|^values", names(out), value = TRUE)
out <- out[, keepVariables]
names(out) <- gsub("values\\.", "", names(out))
return(out)
}
debugonce(getQualtricsData)
dataTest = getQualtricsData(apiToken, dataCenter, surveyID)