ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
packages <- c("tidyverse","data.table", "XML","httr","xts")
ipak(packages)
## Loading required package: tidyverse
## -- Attaching packages ------------------------------------------ tidyverse 1.3.0 --
## v ggplot2 3.3.0 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.0
## v tidyr 1.1.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts --------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Loading required package: data.table
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
##
## between, first, last
## The following object is masked from 'package:purrr':
##
## transpose
## Loading required package: XML
## Loading required package: httr
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'xts'
## The following objects are masked from 'package:data.table':
##
## first, last
## The following objects are masked from 'package:dplyr':
##
## first, last
## tidyverse data.table XML httr xts
## TRUE TRUE TRUE TRUE TRUE
extraccion<- function(codigo=NULL,start=NULL,end=NULL){
vector<- vector()
datos<- NULL
for(i in 1:length(codigo)){
ex<- paste0("https://estadisticas.bcrp.gob.pe/estadisticas/series/api/",
codigo[i], "/xml/",start,"/",end)
ObjXML <- httr::GET(ex)
L_parseXML <- xmlParse(ObjXML)
L_XML <- xmlToList(L_parseXML)
dt_BCRP <- data.table::data.table()
for (a in 1:length(L_XML$periods)){
dt_temp <- data.table(
serie= L_XML$periods[[a]]$v)
dt_BCRP <- rbind(dt_BCRP,dt_temp)
}
vector[i]<- dt_BCRP[,serie:=as.numeric(serie)]
}
for(i in 1: length(codigo)){
datos<- cbind(datos,vector[[i]])
}
datos<- as.data.frame(datos)
names(datos)<- codigo
return(datos)
}
ata<-extraccion(c("PN01488BM","PN01210PM","PN37692AQ" ),"2000-1","2007-2")
## No encoding supplied: defaulting to UTF-8.
## No encoding supplied: defaulting to UTF-8.
## No encoding supplied: defaulting to UTF-8.
## Warning in cbind(datos, vector[[i]]): number of rows of result is not a multiple
## of vector length (arg 2)