From caf5f65b8b2cd11f7b45cf5c63cf0b6aee6ddafa Mon Sep 17 00:00:00 2001 From: William Gearty Date: Wed, 23 Oct 2024 15:49:20 -0400 Subject: [PATCH] Remove sapply; update README --- README.Rmd | 61 ++++++++++++++++++++++++++ README.md | 73 ++++++++++++++++++++++++++++++++ tests/testthat/test-rl_threats.R | 4 +- 3 files changed, 136 insertions(+), 2 deletions(-) diff --git a/README.Rmd b/README.Rmd index 8e5ba3f..0b5530d 100644 --- a/README.Rmd +++ b/README.Rmd @@ -2,6 +2,32 @@ rredlist ======== ```{r echo=FALSE} +library("knitr") +library("rredlist") +hook_output <- knitr::knit_hooks$get("output") +knitr::knit_hooks$set(output = function(x, options) { + lines <- options$output.lines + if (is.null(lines)) { + return(hook_output(x, options)) # pass to default hook + } + x <- unlist(strsplit(x, "\n")) + more <- "..." + if (length(lines)==1) { # first n lines + if (length(x) > lines) { + # truncate the output, but add .... + x <- c(head(x, lines), more) + } + } else { + x <- c(if (abs(lines[1])>1) more else NULL, + x[lines], + if (length(x)>lines[abs(length(lines))]) more else NULL + ) + } + # paste these lines together + x <- paste(c(x, ""), collapse = "\n") + hook_output(x, options) + }) + knitr::opts_chunk$set( warning = FALSE, message = FALSE, @@ -10,6 +36,7 @@ knitr::opts_chunk$set( ) ``` +[![Status at rOpenSci Software Peer Review](https://badges.ropensci.org/663_status.svg)](https://github.com/ropensci/software-review/issues/663) [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![cran version](https://www.r-pkg.org/badges/version/rredlist)](https://cran.r-project.org/package=rredlist) [![R-check](https://github.com/ropensci/rredlist/actions/workflows/R-check.yml/badge.svg)](https://github.com/ropensci/rredlist/actions/workflows/R-check.yml) @@ -34,6 +61,40 @@ remotes::install_github("ropensci/rredlist") install.packages("rredlist", repos = "https://ropensci.r-universe.dev/") ``` +## Authentication + +Use of this package requires an IUCN API key which can be acquired at https://api.iucnredlist.org/users/sign_up. There is a helper function to help you get the key and store it properly: + +```{r eval=FALSE} +rredlist::rl_use_iucn() +``` + +**Keep this key private.** You can pass the key in to each function via the key parameter, but it’s better to store the key either as a environment variable (`IUCN_REDLIST_KEY`) or an R option (`iucn_redlist_key`) - we recommend using the former option. Note that there is not a default API key that is used as a fallback, and the package will not function without providing/storing your own API key. + +## Example usage + +### Loading the package +```{r} +library("rredlist") +``` + +### Search for assessments for a particular species +```{r output.lines=1:10} +rl_species("Gorilla", "gorilla")$assessments +``` + +### Search for assessments that recommend particular conservation actions + +#### Get a list of all conservation actions +```{r output.lines=1:10} +rl_actions() +``` + +#### Return assessments with a particular conservation action +```{r output.lines=1:10} +rl_actions("2_2", all = FALSE)$assessments +``` + ## Meta * Please [report any issues or bugs](https://github.com/ropensci/rredlist/issues). diff --git a/README.md b/README.md index 0f9ee70..be967b7 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ rredlist ======== + [![Status at rOpenSci Software Peer Review](https://badges.ropensci.org/663_status.svg)](https://github.com/ropensci/software-review/issues/663) [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) [![cran version](https://www.r-pkg.org/badges/version/rredlist)](https://cran.r-project.org/package=rredlist) @@ -29,6 +30,78 @@ remotes::install_github("ropensci/rredlist") install.packages("rredlist", repos = "https://ropensci.r-universe.dev/") ``` +## Authentication + +Use of this package requires an IUCN API key which can be acquired at https://api.iucnredlist.org/users/sign_up. There is a helper function to help you get the key and store it properly: + + +``` r +rredlist::rl_use_iucn() +``` + +**Keep this key private.** You can pass the key in to each function via the key parameter, but it’s better to store the key either as a environment variable (`IUCN_REDLIST_KEY`) or an R option (`iucn_redlist_key`) - we recommend using the former option. Note that there is not a default API key that is used as a fallback, and the package will not function without providing/storing your own API key. + +## Example usage + +### Loading the package + +``` r +library("rredlist") +``` + +### Search for assessments for a particular species + +``` r +rl_species("Gorilla", "gorilla")$assessments +#> year_published latest sis_taxon_id url assessment_id scopes +#> 1 2016 FALSE 9404 https://www.iucnredlist.org/species/9404/17963949 17963949 Global, 1 +#> 2 2016 FALSE 9404 https://www.iucnredlist.org/species/9404/102330408 102330408 Global, 1 +#> 3 2018 TRUE 9404 https://www.iucnredlist.org/species/9404/136250858 136250858 Global, 1 +#> 4 2008 FALSE 9404 https://www.iucnredlist.org/species/9404/12983787 12983787 Global, 1 +#> 5 2007 FALSE 9404 https://www.iucnredlist.org/species/9404/12983966 12983966 Global, 1 +#> 6 2000 FALSE 9404 https://www.iucnredlist.org/species/9404/12983737 12983737 Global, 1 +#> 7 1996 FALSE 9404 https://www.iucnredlist.org/species/9404/12983764 12983764 Global, 1 +#> 8 1994 FALSE 9404 https://www.iucnredlist.org/species/9404/12984167 12984167 Global, 1 +#> 9 1990 FALSE 9404 https://www.iucnredlist.org/species/9404/12984186 12984186 Global, 1 +... +``` + +### Search for assessments that recommend particular conservation actions + +#### Get a list of all conservation actions + +``` r +rl_actions() +#> $conservation_actions +#> en code +#> 1 Land/water protection 1 +#> 2 Site/area protection 1_1 +#> 3 Resource & habitat protection 1_2 +#> 4 Land/water management 2 +#> 5 Site/area management 2_1 +#> 6 Invasive/problematic species control 2_2 +#> 7 Habitat & natural process restoration 2_3 +#> 8 Species management 3 +... +``` + +#### Return assessments with a particular conservation action + +``` r +rl_actions("2_2", all = FALSE)$assessments +#> year_published latest sis_taxon_id url assessment_id code +#> 1 2019 TRUE 132523146 https://www.iucnredlist.org/species/132523146/497499 497499 2_2 +#> 2 2019 TRUE 10767 https://www.iucnredlist.org/species/10767/498370 498370 2_2 +#> 3 2013 TRUE 1078 https://www.iucnredlist.org/species/1078/498639 498639 2_2 +#> 4 2019 TRUE 132521900 https://www.iucnredlist.org/species/132521900/498826 498826 2_2 +#> 5 2020 FALSE 1086 https://www.iucnredlist.org/species/1086/499235 499235 2_2 +#> 6 2019 TRUE 1117 https://www.iucnredlist.org/species/1117/500918 500918 2_2 +#> 7 2019 TRUE 11797 https://www.iucnredlist.org/species/11797/503908 503908 2_2 +#> 8 2021 TRUE 12124 https://www.iucnredlist.org/species/12124/505402 505402 2_2 +#> 9 2019 TRUE 12695 https://www.iucnredlist.org/species/12695/507698 507698 2_2 +... +``` + ## Meta * Please [report any issues or bugs](https://github.com/ropensci/rredlist/issues). diff --git a/tests/testthat/test-rl_threats.R b/tests/testthat/test-rl_threats.R index 7e99540..4d0c61d 100644 --- a/tests/testthat/test-rl_threats.R +++ b/tests/testthat/test-rl_threats.R @@ -79,8 +79,8 @@ test_that("secret query parameters work", { }) aa_filt <- subset(aa$assessments, latest & year_published == "2017" & - sapply(aa$assessments$scopes, - function(df) "2" %in% df$code)) + unlist(lapply(aa$assessments$scopes, + function(df) "2" %in% df$code))) expect_equal(nrow(aa_filt), nrow(bb$assessments)) })