Skip to content

Commit

Permalink
Added 3 functions for existing API methods. landings, species_codes a…
Browse files Browse the repository at this point in the history
…nd country_codes.
  • Loading branch information
karthik committed Dec 15, 2012
1 parent 85b4836 commit 0805ed1
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
@@ -0,0 +1,3 @@
export(country_codes)
export(landings)
export(species_codes)
15 changes: 15 additions & 0 deletions R/country_codes.R
@@ -0,0 +1,15 @@
#' Download full list of ISO-3166 alpha 3 country code. Function returns a data frame with country name and \code{iso3c} code which is required by the \code{landings} function to return country specific data
#'
#' @param curl Pass curl handle when calling function recursively.
#' @param ... additional optional parameters
#' @export
#' @return data.frame
#' @examples \dontrun{
#' country_codes()
#'}
country_codes <- function(curl=getCurlHandle(), ...) {
url <- "http://openfisheries.org/api/landings/countries"
countries <- suppressWarnings(getForm(url, .opts = list(...), curl=curl))
countries <- ldply(fromJSON(I(countries)))
return(countries)
}
37 changes: 37 additions & 0 deletions R/landings.R
@@ -0,0 +1,37 @@
#' Returns landings data from the openfisheries API
#'
#' The function returns aggregate landings data if no parameters are supplied. One could get country or species-specific data by specifying either one of those options. Country must be provided as the iso3c code and species must be supplied as a3_code. Supporting functions \code{country_codes} and \code{species_codes} provide that data and can be combined to return data for multiple countries or species.
#' @param country Default is \code{NA}. Download country specific data by specifying the ISO-3166 alpha 3 country code.
#' @param species Default is \code{NA}. Download species specific data by specifying the three-letter ASFIS species code
#' @param curl Pass curl handle when calling function recursively.
#' @param ... additional optional parameters
#' @export
#' @return data.frame
#' @examples \dontrun{
#' landings()
#' # Landings by country
#' landings(country = "CAN")
#' #landings by species
#' landings(species = "SKJ")
#'}
landings <- function(country = NA, species = NA, curl=getCurlHandle(), ...){

if(!is.na(country) && !is.na(species))
stop("Specify country or species but not both", call.=FALSE)

if(is.na(country) && is.na(species)) {
url <- "http://openfisheries.org/api/landings"
} else if(!is.na(country) && is.na(species)) {
url <- paste0("http://openfisheries.org/api/landings/countries/", country)
} else {
url <- paste0("http://openfisheries.org/api/landings/species/", species)
}

landings_data <- suppressWarnings(getForm(url, .opts = list(...), curl=curl))
landings_data <- ldply(fromJSON(I(landings_data)))
if(nrow(landings_data) == 0) {
stop("No data found", call.=FALSE)
} else {
return(landings_data)
}
}
22 changes: 22 additions & 0 deletions R/species_codes.R
@@ -0,0 +1,22 @@

#'Download species data including three-letter ASFIS species code. Returns a data frame with scientific_name, taxocode, a3_code, isscaap, and English name. The a3_code is required by \code{landings} to return species specific landing data.
#'
#' @param curl Pass curl handle when calling function recursively.
#' @param ... additional optional parameters
#' @export
#' @return data.frame
#' @examples \dontrun{
#' species_codes()
#'}
species_codes <- function(curl=getCurlHandle(), ...) {
url <- "http://openfisheries.org/api/landings/species"
species <- suppressWarnings(getForm(url, .opts = list(...), curl=curl))
species <- fromJSON(I(species))
species_list <- ldply(species, function(x) {
if(x[4]=="NULL")
x[4] <- "NA"
data.frame(x)
}, .progress = 'text')
return(species_list)
}

27 changes: 27 additions & 0 deletions man/country_codes.Rd
@@ -0,0 +1,27 @@
\name{country_codes}
\alias{country_codes}
\title{Download full list of ISO-3166 alpha 3 country code. Function returns a data frame with country name and \code{iso3c} code which is required by the \code{landings} function to return country specific data}
\usage{
country_codes(curl = getCurlHandle(), ...)
}
\arguments{
\item{curl}{Pass curl handle when calling function
recursively.}

\item{...}{additional optional parameters}
}
\value{
data.frame
}
\description{
Download full list of ISO-3166 alpha 3 country code.
Function returns a data frame with country name and
\code{iso3c} code which is required by the
\code{landings} function to return country specific data
}
\examples{
\dontrun{
country_codes()
}
}

44 changes: 44 additions & 0 deletions man/landings.Rd
@@ -0,0 +1,44 @@
\name{landings}
\alias{landings}
\title{Returns landings data from the openfisheries API}
\usage{
landings(country = NA, species = NA,
curl = getCurlHandle(), ...)
}
\arguments{
\item{country}{Default is \code{NA}. Download country
specific data by specifying the ISO-3166 alpha 3 country
code.}

\item{species}{Default is \code{NA}. Download species
specific data by specifying the three-letter ASFIS
species code}

\item{curl}{Pass curl handle when calling function
recursively.}

\item{...}{additional optional parameters}
}
\value{
data.frame
}
\description{
The function returns aggregate landings data if no
parameters are supplied. One could get country or
species-specific data by specifying either one of those
options. Country must be provided as the iso3c code and
species must be supplied as a3_code. Supporting functions
\code{country_codes} and \code{species_codes} provide
that data and can be combined to return data for multiple
countries or species.
}
\examples{
\dontrun{
landings()
# Landings by country
landings(country = "CAN")
#landings by species
landings(species = "SKJ")
}
}

28 changes: 28 additions & 0 deletions man/species_codes.Rd
@@ -0,0 +1,28 @@
\name{species_codes}
\alias{species_codes}
\title{Download species data including three-letter ASFIS species code. Returns a data frame with scientific_name, taxocode, a3_code, isscaap, and English name. The a3_code is required by \code{landings} to return species specific landing data.}
\usage{
species_codes(curl = getCurlHandle(), ...)
}
\arguments{
\item{curl}{Pass curl handle when calling function
recursively.}

\item{...}{additional optional parameters}
}
\value{
data.frame
}
\description{
Download species data including three-letter ASFIS
species code. Returns a data frame with scientific_name,
taxocode, a3_code, isscaap, and English name. The a3_code
is required by \code{landings} to return species specific
landing data.
}
\examples{
\dontrun{
species_codes()
}
}

Binary file added multiple_countries.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0805ed1

Please sign in to comment.