Skip to content

Instantly share code, notes, and snippets.

@JohnCoene
Created March 13, 2019 15:21
Show Gist options
  • Select an option

  • Save JohnCoene/e2d634412714bb8f3dc9801b602fb9a4 to your computer and use it in GitHub Desktop.

Select an option

Save JohnCoene/e2d634412714bb8f3dc9801b602fb9a4 to your computer and use it in GitHub Desktop.
Qualtrics Ping Method Proposal
############# FUNCTIONS
#' A ping function that returns an object of say `qualtricsDemands`
#' @param ids Vector of survey ids
demand_downloads <- function(ids){
# simulate ping
pinged <- purrr::map(ids, function(x) sample(c("failure", "success"), 1))
# construct object
# note that it inherits from class `list`
# otherwise not other method/function will work =/
structure(
pinged,
class = c("qualtricsDemands", "list")
)
}
#' method
download <- function(demands) UseMethod("download")
#' An object of class `qualtricsDemands`
download.qualtricsDemands <- function(demands){
if(!inherits(demands, "qualtricsDemands"))
stop("demands must be of class qualtricsDemand", call. = FALSE)
purrr::map(
demands,
function(x){
if(x == "success")
runif(10) # data
else
NULL # no data
}
)
}
#' Cool thing is with new class we can define other methods
#' for instance print
print.qualtricsDemands <- function(x, ...){
print(
table(
unlist(x)
)
)
}
############# EXAMPLES
# demand download for surveys id 1:10
demands <- demand_downloads(1:10)
print(demands) # print demand results
# download based on demands
download(demands)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment