Skip to content

Commit

Permalink
First version of the iadbstats library in R to pull data from n4d of …
Browse files Browse the repository at this point in the history
…the inter-american development bank
  • Loading branch information
arcuellar88 committed Nov 18, 2016
1 parent d954849 commit 9c5f391
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
20 changes: 20 additions & 0 deletions DESCRIPTION
@@ -0,0 +1,20 @@
Package: idbstats
Type: Package
Title: Access to the data and statistics from Numbers for Development of the Inter-American Development Bank API
Version: 1.0
Date: 2016-11-17
Author: Alejandro Rodriguez Cuellar
Maintainer: Alejandro Rodríguez Cuéllar <alejandroro@iadb.org>
Description: Tool to download the metadata and data of the indicators from Numbers for Development of the Inter-American Development Bank
<https://data.iadb.org/NumbersForDevelopment/NumbersForDevelopment>
c(
person("Alejandro", "Rodríguez Cuéllar", email = "alejandroro@iadb.org", role = c("aut", "cre"))
)
License: MIT + file LICENSE

Depends:
R (>= 2.10)
Imports:
httr,
jsonlite
stringr
1 change: 1 addition & 0 deletions NAMESPACE
@@ -0,0 +1 @@
exportPattern("^[[:alpha:]]+")
24 changes: 24 additions & 0 deletions R/iadbsearch.R
@@ -0,0 +1,24 @@

#' # Search for indicators
#' # 'searchvalue = ALL or indicator code'
#' idbmsearch(pattern = "poverty|unemployment|employment")
#' @export
iadbmsearch <- function(value = "ALL"){
urlmeta <- "metadata/indicator"
searchType <-"?searchtype=name"
searchvalue<-paste0("&searchvalue=",value)
searchLanguage<-"&languagecode=EN"

urls<-iadburls()

url <- paste0(urls$base_url,urlmeta,searchType,searchvalue,searchLanguage,urls$utils_url)

#url
iadbget.raw(url)


#http://api-data.iadb.org/?searchtype=namemetadata/indicator&searchvalue=ALL&languagecode=EN&responsetype=json
#http://api-data.iadb.org/metadata/indicator?searchtype=name&searchvalue=ALL&languagecode=EN&

}

54 changes: 54 additions & 0 deletions R/iadbstats.R
@@ -0,0 +1,54 @@
#' # Search for indicators
#' # 'searchvalue = ALL or indicator code'
#' idbmsearch(pattern = "poverty|unemployment|employment")
#' @export
iadbstats <- function(country="ALL",frequency="year",indicatorcode="ALL"){


if(country=="ALL" && indicatorcode=="ALL") stop("All countries and All indicators cannot be requested")

if(indicatorcode=="ALL"&& stringr::str_count(country, ',')>3) stop("When All indicators’ data is requested, data can be requested for a maximum of 4 countries")

if(country=="ALL"&& stringr::str_count(indicatorcode, ',')>9) stop("When All country data is requested, data can be requested for a maximum of 10 indicators")

urlmeta <- "datasitedata?"
searchcountry<-paste0("countrycode=",country)
searchfrequency<-paste0("&frequency=",frequency)
searchLanguage<-"&languagecode=EN"
searchIndicator<-paste0("&indicatorcode=",indicatorcode)

urls<-iadburls()

#print(lenght(indicatorCode))
url <- paste0(urls$base_url,urlmeta,searchcountry,searchfrequency,searchLanguage,searchIndicator,urls$utils_url)

#url
idbget.raw(url)

#idbget.raw("http://api-data.iadb.org/datasitedata?countrycode=arg&frequency=year&languagecode=en&responsetype=json&indicatorcode=SOC_050,SOC_057,SOC_060,SOC_071,SOC_073")

}

iadbstats.list <- function(frequency="year",indicatorcodes){

scountry="ALL"

# dfInd<- as.data.frame(indicatorcodes)
# subsetsInd<- split(dfInd, (seq(nrow(dfInd))-1) %/% 10)
# ind <-paste(as.character(subsetsInd[[1]]),collapse=",")


df<- split(indicatorCode, ceiling(seq_along(indicatorCode)/10))

indicator_list <- list()

for(i in 1:length(df))
{
ind <-paste(as.character(df[[1]]),collapse=",")
indicator_list[[i]] = iadbstats(country=scountry,frequency=frequency,indicatorcode=ind)
}

full_data <- bind_rows(indicator_list)

full_data
}
35 changes: 35 additions & 0 deletions R/utilities.R
@@ -0,0 +1,35 @@
#' Call the Data Catalog API
#'
#' Helper function for the data catalog call
#'
#' @param url A charcter string. A formatted url string
#' @note This call is seperate because the data catalog is actaully a different
#' API and therefore has a different return structure.
#' @return A data frame
iadbget.raw <- function(url) {

return_get <- httr::GET(url)
return_json <- httr::content(return_get, as = "text")
return_list <- jsonlite::fromJSON(return_json, flatten = TRUE)
return_list

}

iadburls <- function() {

base_url <- "http://api-data.iadb.org/"
utils_url <- "&responsetype=json"

url_list <- list(base_url = base_url, utils_url = utils_url)

url_list
}




#' http://api-data.iadb.org/metadata/country?searchtype=code&searchvalue=ARG&languagecode=en&responsetype=xml

#'http://api-data.iadb.org/metadata/indicator?searchtype=name&searchvalue=ALL&languagecode=EN&responsetype=xml

#'http://api-data.iadb.org/datasitedata?countrycode=arg&frequency=year&languagecode=en&responsetype=json&indicatorcode=all
8 changes: 8 additions & 0 deletions Read-and-delete-me
@@ -0,0 +1,8 @@
* Edit the help file skeletons in 'man', possibly combining help files for multiple functions.
* Edit the exports in 'NAMESPACE', and add necessary imports.
* Put any C/C++/Fortran code in 'src'.
* If you have compiled code, add a useDynLib() directive to 'NAMESPACE'.
* Run R CMD build to build the package tarball.
* Run R CMD check to check the package tarball.

Read "Writing R Extensions" for more information.
17 changes: 17 additions & 0 deletions iadbstats.Rproj
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
40 changes: 40 additions & 0 deletions man/idbstats-package.Rd
@@ -0,0 +1,40 @@
\name{idbstats-package}
\alias{idbstats-package}
\alias{idbstats}
\docType{package}
\title{
What the package does (short line)
~~ package title ~~
}
\description{
More about what it does (maybe more than one line)
~~ A concise (1-5 lines) description of the package ~~
}
\details{
\tabular{ll}{
Package: \tab idbstats\cr
Type: \tab Package\cr
Version: \tab 1.0\cr
Date: \tab 2016-11-17\cr
License: \tab What license is it under?\cr
}
~~ An overview of how to use the package, including the most important functions ~~
}
\author{
Who wrote it

Maintainer: Who to complain to <yourfault@somewhere.net>
~~ The author and/or maintainer of the package ~~
}
\references{
~~ Literature or other references for background information ~~
}
~~ Optionally other standard keywords, one per line, from file KEYWORDS in the R documentation directory ~~
\keyword{ package }
\seealso{
~~ Optional links to other man pages, e.g. ~~
~~ \code{\link[<pkg>:<pkg>-package]{<pkg>}} ~~
}
\examples{
~~ simple examples of the most important functions ~~
}

0 comments on commit 9c5f391

Please sign in to comment.