Skip to content

Commit

Permalink
updated readme, and pkg level man file
Browse files Browse the repository at this point in the history
fix birdlife fxns, and tidy up a bit, fix #100
  • Loading branch information
sckott committed Sep 22, 2017
1 parent ad03cd7 commit df496d9
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 219 deletions.
70 changes: 28 additions & 42 deletions R/birdlife.R
Original file line number Diff line number Diff line change
@@ -1,77 +1,61 @@
#' Get bird habitat information from BirdLife/IUCN
#'
#' @export
#'
#' @param id A single IUCN species ID
#'
#' @return a \code{data.frame} with level 1 and level 2 habitat classes, as well as importance
#' ratings and occurrence type (e.g. breeding or non-breeding). The habitat classification
#' scheme is described at \url{http://bit.ly/1e6gKBr}
#' @return a \code{data.frame} with level 1 and level 2 habitat classes, as
#' well as importance ratings and occurrence type (e.g. breeding or
#' non-breeding). The habitat classification scheme is described
#' at \url{http://bit.ly/1e6gKBr}
#' @author David J. Harris \email{harry491@@gmail.com}
#' @family birdlife
#' @examples \dontrun{
#' # Setophaga chrysoparia
#' birdlife_habitat(22721692)
#' # Passer domesticus
#' birdlife_habitat(103818789)
#' }
#' @seealso \code{\link{birdlife_threats}}

birdlife_habitat = function(id){

birdlife_habitat <- function(id) {
stopifnot(length(id) == 1)

url = paste0(
"http://www.birdlife.org/datazone/species/factsheet/",
id,
"/additional"
)

url <- bl_url(id)
tables <- make_tables(url)

# Find the table that has "Habitat" as a column name
habitat_table_number <- which(
vapply(tables, function(table) any(grepl("Habitat", colnames(table))), logical(1))
vapply(tables, function(table) any(grepl("Habitat", colnames(table))),
logical(1))
)

out <- cbind(id, tables[[habitat_table_number]])
out[-NROW(out), ] # Drop last row (altitude)
}

#' Get bird threat information from BirdLife/IUCN
#'
#' @export
#'
#' @param id A single IUCN species ID
#'
#' @return a \code{data.frame} with the species ID and two levels of threat descriptions,
#' plus stresses, timing, scope, severity, and impact associated with each stressor.
#' @inheritParams birdlife_habitat
#' @return a \code{data.frame} with the species ID and two levels of threat
#' descriptions, plus stresses, timing, scope, severity, and impact associated
#' with each stressor.
#' @author David J. Harris \email{harry491@@gmail.com}
#' @family birdlife
#' @examples \dontrun{
#' # Setophaga chrysoparia
#' birdlife_threats(22721692)
#' # Aburria aburri
#' birdlife_threats(22678440)
#' }
#' @seealso \code{\link{birdlife_habitat}}

birdlife_threats = function(id){

birdlife_threats <- function(id) {
stopifnot(length(id) == 1)

url = paste0(
"http://www.birdlife.org/datazone/species/factsheet/",
id,
"/additional"
)

url <- bl_url(id)
tables <- make_tables(url)

is_threats <- sapply(
tables,
function(x){
all(c("Scope", "Severity", "Impact", "Timing") %in% unlist(x))
}
)

if(sum(is_threats) > 1){
if (sum(is_threats) > 1) {
stop("Malformed input. Multiple threat tables in ID ", id)
}

if(sum(is_threats) == 1){
if (sum(is_threats) == 1) {
threats = tables[is_threats][[1]]

rownums = seq_len(nrow(threats))
Expand All @@ -86,10 +70,9 @@ birdlife_threats = function(id){
severity = threats[rownums %% 5 == 2, 3],
impact = threats[rownums %% 5 == 2, 4]
)
}else{
} else {
out = NULL
}

out
}

Expand All @@ -98,3 +81,6 @@ make_tables <- function(x) {
tables <- xml2::xml_find_all(html, "//table")
rvest::html_table(tables, fill = TRUE)
}

bl_base <- "http://datazone.birdlife.org/species/factsheet"
bl_url <- function(x) sprintf("%s/%s/details", bl_base, x)
6 changes: 5 additions & 1 deletion R/traits-package.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' traits - Species trait data from around the web
#'
#' Currently included in \code{traits} with the associated function prefix:
#' Currently included in \code{traits} with the associated function name or
#' function prefix:
#' \itemize{
#' \item BETYdb \url{http://www.betydb.org} - \code{betydb_}
#' \item National Center for Biotechnology Information - NCBI
Expand All @@ -11,6 +12,9 @@
#' \code{birdlife_}
#' \item LEDA Traitbase http://www.leda-traitbase.org/LEDAportal/index.jsp -
#' \code{leda_}
#' \item USDA Plants Database - \code{\link{tr_usda}}
#' \item Zanne et al. plant dataset - \code{\link{tr_zanne}}
#' \item Amniote life history dataset - \code{\link{tr_ernest}}
#' \item More to come ...
#' }
#'
Expand Down
53 changes: 14 additions & 39 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
warning = FALSE,
message = FALSE,
cache.path = "inst/readmecache/"
message = FALSE
)
```

Expand All @@ -18,17 +17,17 @@ knitr::opts_chunk$set(

R client for various sources of species trait data.

Included in `traits` with the associated function prefix:
Included in `traits` with the associated function prefix or function name:

* [BETYdb](http://www.betydb.org) - `betydb_`
* [National Center for Biotechnology Information - NCBI](http://www.ncbi.nlm.nih.gov/) - `ncbi_`
* [Global Invasive Species Database - GISD](http://www.issg.org/database/welcome/) - `g_`
* [Encyclopedia of Life Invasive Species](http://eol.org/collections/38204) - `eol_`
* [Encyclopedia of Life Traitbank](http://eol.org/info/516) - `traitbank_`
* [Coral Traits Database](http://coraltraits.org/) - `coral_`
* [Flora Europaea](http://rbg-web2.rbge.org.uk/FE/fe.html) - `fe_`
* [Birdlife International](http://rbg-web2.rbge.org.uk/FE/fe.html) - `birdlife_`
* LEDA Traitbase - `leda_`
* USDA Plants Database - `tr_usda`
* Zanne et al. plant dataset - `tr_zanne`
* Amniote life history dataset - `tr_ernest`
* More to come ...

Talk to us on the [issues page](https://github.com/ropensci/traits/issues) if you know of a source of traits data with an API, and we'll see about including it.
Expand Down Expand Up @@ -78,54 +77,28 @@ salix %>%
```

## GISD invasive species data

```{r}
sp <- c("Carpobrotus edulis", "Rosmarinus officinalis")
g_invasive(sp)
```

Or as simplified output

```{r}
g_invasive(sp, simplify = TRUE)
```

## EOL invasive species data

```{r}
eol_invasive_('Brassica oleracea', dataset = 'gisd')
```

Another example, with more species, and from

```{r}
eol_invasive_(c('Lymantria dispar','Cygnus olor','Hydrilla verticillata','Pinus concolor'),
dataset = 'i3n')
```

## EOL's traitbank trait data

Searching for _Mesoplodon bidens_, page id `328566`
Searching for _Balaenoptera musculus_ (blue whale), page id `328574`

```{r cache=TRUE}
res <- traitbank(846827)
```{r cache=FALSE}
res <- traitbank(328574)
res$graph %>%
select(`dwc:measurementtype.@id`, `dwc:measurementtype.rdfs:label.en`) %>%
filter(!is.na(`dwc:measurementtype.rdfs:label.en`))
select(`dwc:measurementtype`) %>%
filter(!is.na(`dwc:measurementtype`))
```

## Coral

Get the species list and their ids

```{r cache=TRUE}
```{r cache=FALSE}
coral_species()
```

Get data by taxon

```{r cache=TRUE}
```{r cache=FALSE}
coral_taxa(80)
```

Expand All @@ -142,5 +115,7 @@ birdlife_habitat(22721692)
* Please [report any issues or bugs](https://github.com/ropensci/traits/issues).
* License: MIT
* Get citation information for `traits` in R doing `citation(package = 'traits')`
* Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).
By participating in this project you agree to abide by its terms.

[![ropensci_footer](http://ropensci.org/public_images/github_footer.png)](http://ropensci.org)
Loading

0 comments on commit df496d9

Please sign in to comment.