Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 3 functions for existing API methods. landings, species_codes a…
…nd country_codes.
- Loading branch information
Showing
8 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export(country_codes) | ||
export(landings) | ||
export(species_codes) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
|
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.