-
-
Save franciscojunior/9997c32a9a0d0e53e13ab99c73a144a9 to your computer and use it in GitHub Desktop.
getting data from BCB (Banco Central do Brasil)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script was originally written during for a summer course that I | |
# gave at FGV in January 2011. The script was used to show the | |
# students how to retrive data from Banco Central do Brasil | |
# (http://bcb.gov.br/?SERIETEMP) using SOAP protocol and R. | |
library(SSOAP) | |
library(XML) | |
library(RCurl) | |
wsdl <- getURL("https://www3.bcb.gov.br/sgspub/JSP/sgsgeral/FachadaWSSGS.wsdl", | |
ssl.verifypeer = FALSE) | |
doc <- xmlInternalTreeParse(wsdl) | |
def <- processWSDL(doc) | |
ff <- genSOAPClientInterface(def = def) | |
getSeries <- function(codigos, data.ini = "01/01/1998", data.fim = "01/01/2011", remove.old = TRUE) { | |
xmlstr <- ff@functions$getValoresSeriesXML(codigos, data.ini, data.fim, | |
.opts = list(ssl.verifypeer = FALSE)) | |
doc <- xmlInternalTreeParse(xmlstr) | |
cleanup <- xpathApply(doc,"//SERIE", function(s) { | |
id <- xmlGetAttr(s, "ID") | |
s1 <- xmlSApply(s, function(x) xmlSApply(x, xmlValue)) | |
s1 <- t(s1) | |
dimnames(s1) <- list(NULL, dimnames(s1)[[2]]) | |
df <- as.data.frame(s1, stringsAsFactors=FALSE) | |
df$SERIE <- id | |
df | |
}) | |
df <- Reduce(rbind, cleanup) | |
df$data <- as.Date(sapply(strsplit(df$DATA, "/"), | |
function(x) paste(c(x[2:1], 1), collapse="-")), "%Y-%m-%d") | |
df$valor <- as.numeric(df$VALOR) | |
df$serie <- factor(df$SERIE) | |
if(remove.old){ | |
df$BLOQUEADO <- NULL | |
df$SERIE <- NULL | |
df$DATA <- NULL | |
df$VALOR <- NULL | |
} | |
df | |
} | |
# Testing the function | |
codigos <- c(4606, 4607, 4608, 4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616) | |
df <- getSeries(codigos) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment