Little function to download ESS data on the go

Motivation

Yes, there is a recently published brand new R package ess for downloading European social survey data, I tried it, although at this point it is quite limited.
What are the good sides of ess package?

  • it downloads data, sometimes several data at a time

What’s not so good?

  • when it downloads several rounds, you get a list of data instead of integrated dataset;
  • it can only download one country data at a time;
  • it tuned up for use in Stata, but not in R, for example, I couldn’t see most of the value labels.

So, I thought it would be useful to have a customizable function (instead of package) to do the same thing, but better. For example, you can keep labels to use, for example, with my label_book.

Details

Don’t put more than one country or more than one round – it won’t work. For countries, use iso2c codes, or “all”. This function will expire when ESS updates its data versions, but it happens about twice a year, and can be fixed manually.

Examples

#1. Source the function
eval(parse(text =getURL("https://raw.githubusercontent.com/MaksimRudnev/LittleHelpers/master/download_ess/download_ess.R")))
#2. Enjoy it
ESS2 <- download_ess(round=2, country="all", "mymail@gmail.com") #Add your registered on ESS website mail here
ESS6.Russia <- download_ess(round=6, country="RU", "mymail@gmail.com")

Function itself

download_ess <- function(round, country="all", user) {
 #1.Create url
 if(country!="all") {
 download.url <- paste("http://www.europeansocialsurvey.org/file/download?f=ESS", round, country, ".spss.zip&c=", country, "&y=", round*2+2000, sep="")
 } else {
 version <- c("06_5", "03_5", "03_6", "04_4", "03_3", "02_3", "02_1", "01")[round]
 download.url <- paste("http://www.europeansocialsurvey.org/file/download?f=ESS", round, "e", version, ".spss.zip&c=&y=", round*2+2000, sep="")
 }
 #2. Download data
 library(httr)
 #Authenticate
 a<-POST("http://www.europeansocialsurvey.org/user/login", body = list(u=user))
 # Download
 data.file <- GET(download.url)
 # Write temporary file
 writeBin(content(data.file, "raw"), paste(tempdir(), "file.zip"))
 # Unzip
 path<-utils::unzip(paste(tempdir(), "file.zip"))
 #3. Read in with haven package
 haven::read_spss(path[length(path)])
}

UPD. Now these functions are a part of my R package LittleHelpers.