Error using getSeries function to pull data from BACEN

Asked

Viewed 117 times

1

Could you help me?

I need to download data from the central bank using this routine here:

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)

I get the following error:

Error in ans["xsi:type"] <- XMLSchemaTypes[[m]][["xsi:type"]] : 
  replacement has length zero

What’s going on?

The github of that routine is here: https://gist.github.com/arademaker/1550651

I have already installed many packages, I have installed Java but I cannot solve the problem.

Some help?

  • 1

    Laura for someone to help you you need to provide a [mcve] for the person to test the code, read: How to create a minimum playable example in R.

  • @Noobsaibot thanks. I edited the question.

  • Take a peek at the ecoseries package. It has functions to download data from BACEN, IPEA, and CIDRA.

  • @Carloseduardolagosta I know him. I had trouble downloading data from BACEN. I tried hj and could not.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.