Skip to content

Instantly share code, notes, and snippets.

Last active October 11, 2017 18:37
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Gender API Example
#' Gets gender by name or email address, optionally by country or IP address.
#' @import httr
#' @import rjson
#' @param name A character string containing a first name, or a character vector containing first names. One must specify name or email.
#' @param email A character string containing an email address with a first name. One must specify name or email.
#' @param country An optional character string containing a two-letter country name, as listed here:
#' @param ip An optional character string containing an IP address, used in place of country.
#' @return A dataframe containing the estimated gender, number of samples that estimate is based upon, an accuracy metric (ranging from 0 to 100), and the length of time for the server to process the request.
#' @export
#' @examples \dontrun{
#' gender('Andrea', country='US')
#' gender('Andrea', country='IT')
#' gender(c('george','thomas','ben'))
#' }
gender <- function(name=NULL, email=NULL, country=NULL, ip=NULL){
args <- list()
args$name <- paste(head(unique(name),100), collapse=';')
args$name <- name
warning("Only first 100 names will be used!")
args$email <- email
args$country <- country
args$ip <- ip
result <- GET('', query=args)
out <- content(result)
if(grepl('errno', content(result,as='text')))
if((!is.null(name) & length(name)==1) || !is.null(email))
out <-
out <-,out$result)
rownames(out) <- 1:nrow(out)
#' @import httr
#' @import XML
#' @return A dataframe containing the country name and two-letter shortcut for every country available via the API.
#' @export
#' @examples \dontrun{
#' genderCountryCodes()
#' }
genderCountryCodes <- function(){
html <- htmlParse(GET(""))
out <- = xpathSApply(html, "//div[@class='country clearfix']//div[@class='name']", xmlValue),
shortcut = xpathSApply(html, "//div[@class='country clearfix']//div[@class='shortcut']", xmlValue))
Copy link

leeper commented Feb 18, 2014

I emailed the developer. Seems like a bug in the API, but your code is a logical workaround (but note the 1000 request/month rate limit, which you might quickly expend doing one name at a time).

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