Skip to content

Instantly share code, notes, and snippets.

@jtrecenti
Created August 21, 2014 20:34
Show Gist options
  • Save jtrecenti/98f244c669c9316d4b9f to your computer and use it in GitHub Desktop.
Save jtrecenti/98f244c669c9316d4b9f to your computer and use it in GitHub Desktop.
#' faz o download de uma tabela do Anuário Estatístico da USP
#' secao: numero da secao (1 a 11)
#' id: id da tabela (depende da secao)
#' retorna um data.frame com os dados.
rusp_tab <- function(secao, id) {
link <- sprintf("https://uspdigital.usp.br/anuario/br/tabelas/XLS/2013/T%d.%02d.xls", secao, id)
cat('Acessando ', link, '...\n', sep='')
tmp <- tempfile()
r <- RCurl::getBinaryURL(link, ssl.verifypeer=FALSE, encoding='latin1')
writeBin(as.vector(r), tmp)
a <- gdata::read.xls(tmp, stringsAsFactors=FALSE, encoding='latin1')
a <- a[which(a[,2]!=''),]
names(a) <- tolower(gsub('[./, ]+', '_', gsub("`|\\'", "", iconv(a[1,], to = "ASCII//TRANSLIT"))))
a <- a[-1,]
for(n in names(a)) {
a[[n]] <- type.convert(a[[n]], as.is=T)
}
return(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment