Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created June 5, 2015 01:34
Show Gist options
  • Save andrewheiss/fe64718c41991b1620f5 to your computer and use it in GitHub Desktop.
Save andrewheiss/fe64718c41991b1620f5 to your computer and use it in GitHub Desktop.
Access LimeSurvey RemoteControl API with R
library(httr)
library(jsonlite)
library(base64enc)
#----------------------------------------
# Functions to access RemoteControl API
#----------------------------------------
get_session_key <- function(username, password) {
body.json = list(method = "get_session_key",
id = 1,
params = list(admin = username,
password = password))
# Need to use jsonlite::toJSON because single elements are boxed in httr, which
# is goofy. toJSON can turn off the boxing automatically, though it's not
# recommended. They say to use unbox on each element, like this:
# params = list(admin = unbox("username"), password = unbox("password"))
# But that's a lot of extra work. So auto_unbox suffices here.
# More details and debate: https://github.com/hadley/httr/issues/159
r <- POST(LIME_API, content_type_json(),
body = toJSON(body.json, auto_unbox = TRUE))
session_key <- fromJSON(content(r))$result
return(session_key)
}
export_resposes <- function(session_key, survey_id) {
body.json <- list(method = "export_responses",
id = 1,
params = list(sSessionKey = session_key,
iSurveyID = survey_id,
DocumentType = "csv",
sLanguageCode = "en",
sHeadingType = "full"))
r <- POST(LIME_API, content_type_json(),
body = toJSON(body.json, auto_unbox = TRUE))
# The API returns a base 64 encoded string, so it has to be decoded. R decodes
# it as raw bytes, which then have to be converted into characters. That raw
# text then has to be treated as a file with textConnection
raw_csv <- rawToChar(base64decode(fromJSON(content(r))$result))
return(read.csv(textConnection(raw_csv)))
}
release_session_key <- function(session_key) {
body.json <- list(method = "release_session_key",
id = 1,
params = list(sSessionKey = session_key))
r <- POST(LIME_API, content_type_json(),
body = toJSON(body.json, auto_unbox = TRUE))
return(fromJSON(content(r))$result)
}
#-------------------------
# Get actual survey data
#-------------------------
LIME_API <- "http://example.com/limesurvey/index.php/admin/remotecontrol"
USERNAME <- "put_username_here"
PASSWORD <- "put_password_here"
SURVEY_ID <- "put_survey_id_here"
session_key <- get_session_key(USERNAME, PASSWORD)
final.data <- export_resposes(session_key, SURVEY_ID)
release_session_key(session_key)
@munozedg
Copy link

andrewheiss, I'm getting this error:

final.data <- export_resposes(session_key, SURVEY_ID)
No encoding supplied: defaulting to UTF-8.
Error: Argument 'txt' must be a JSON string, URL or path to existing file.
7 base::stop(..., call. = FALSE)
6 stop("Argument 'txt' must be a JSON string, URL or path to existing file.")
5 fromJSON(content(r))
4 inherits(what, "connection")
3 base64decode(fromJSON(content(r))$result)
2 rawToChar(base64decode(fromJSON(content(r))$result))
1 export_resposes(session_key, SURVEY_ID)
release_session_key(session_key)
No encoding supplied: defaulting to UTF-8.
[1] "OK"

@msberends
Copy link

@munozedg I got this as well, for future people:

L11 params = list(admin = username, should be replaced with params = list(username = username,.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment