Skip to content

Commit

Permalink
Merge pull request #88 from sebpardo/fix-ebirdfreq
Browse files Browse the repository at this point in the history
Disable `ebirdfreq()`, throws error by default and provides url as message, close #82
  • Loading branch information
sebpardo authored Jan 24, 2021
2 parents f50ad1f + 6900d42 commit 4852787
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 38 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: rebird
Version: 1.1.4
Version: 1.1.5
Title: R Client for the eBird Database of Bird Observations
Description: A programmatic client for the eBird database
(<https://ebird.org/home>), including functions for searching for bird
Expand Down Expand Up @@ -32,7 +32,7 @@ Suggests:
knitr,
testthat,
covr
RoxygenNote: 6.1.1
RoxygenNote: 7.1.1
X-schema.org-applicationCategory: Data Access
X-schema.org-keywords: birds, birding, ebird, database, data, biology, observations, sightings, ornithology
X-schema.org-isPartOf: https://ropensci.org
Expand Down
28 changes: 28 additions & 0 deletions R/ebirdfreq.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#' Download historical frequencies of bird observations from eBird
#'
#' *NOTE: Currently disabled.*
#'
#' @details This function was the only `rebird` function to not use
#' the API and formulated a url-based query instead. Now you need to
#' be logged into eBird to download the frequency data, but we can't
#' authenticate through R, so this function does not work. This
#' functionality is likely to be added to the API in the future,
#' so we are keeping the function in the meantime, but it throws
#' an informative error, and provides the constructed url to
#' obtain the frequency data manually through your browser.
#'
#' @param loctype String with location type. Either "states", "counties",
#' or "hotspots".
Expand Down Expand Up @@ -27,6 +38,9 @@
#' to TRUE. If FALSE then output will be in wide format.
#' @param ... Curl options passed on to \code{\link[httr]{GET}}
#'
#' @return *This function currently returns an error, but also provides
#' the constructed url to manually obtain the datafor the location and
#' dates requested through your browser.*
#' @return A data frame containing the collected information. If in long format:
#' @return "monthQt": month and week (eBird data divides each month by four weeks)
#' @return "comName": species common name
Expand All @@ -43,7 +57,9 @@
#' @importFrom dplyr tbl_df
#' @importFrom httr GET
#' @importFrom httr stop_for_status
#' @importFrom httr modify_url
#' @export
#' @md
#' @examples \dontrun{
#' ebirdfreq("states", "US-NY", 2014, 2014, 1, 12)
#' ebirdfreq("counties", "CA-BC-GV", 1900, 2015, 1, 3)
Expand Down Expand Up @@ -78,6 +94,18 @@ ebirdfreq <- function(loctype, loc, startyear = 1900,
eyr = endyear, personal = "false", fmt = "tsv")

url <- "http://ebird.org/ebird/barchartData"
url_full <- modify_url(url, query = args)
message(url_full)
stop(
"This function has been made temporarily defunct as you now need to be
logged into the eBird website to download frequency data, but we cannot
authenticate via the API to do so. This function will be reinstated
when the frequency data become available through the API. In the meantime,
you can paste the url above in your browser to obtain the web download of
the frequency data for the desired location and date. See
https://github.com/ropensci/rebird/issues/82 for more information."
)

ret <- GET(url, query = args, ...)
stop_for_status(ret)
asChar <- readBin(ret$content, "character")
Expand Down
66 changes: 44 additions & 22 deletions man/ebirdfreq.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions tests/testthat/test-ebirdfreq.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ context("ebirdfreq")

test_that("ebirdfreq works correctly", {
skip_on_cran()
expect_error(ebirdfreq("counties", "CA-BC-GV"), "This function has been made temporarily defunct")

long <- ebirdfreq("counties", "CA-BC-GV")
wide <- ebirdfreq("counties", "CA-BC-GV", long=FALSE)
expect_is(long, "data.frame")
expect_is(wide, "data.frame")
expect_is(long$comName, "character")
expect_is(wide$comName, "character")
expect_equal(NCOL(long), 4)
expect_equal(NCOL(wide), 49)
TL1 <- ebirdfreq("hotspots", "L196159", 2015, 2015, 1, 1)
TL12 <- ebirdfreq("hotspots", "L196159", 2015, 2015, 1, 12)
expect_that(NROW(TL1), is_less_than(NROW(TL12)))
expect_equal(NCOL(TL1), NCOL(TL12))
# # Temporary commenting of tests while function is made defunct
# long <- ebirdfreq("counties", "CA-BC-GV")
# wide <- ebirdfreq("counties", "CA-BC-GV", long=FALSE)
# expect_is(long, "data.frame")
# expect_is(wide, "data.frame")
# expect_is(long$comName, "character")
# expect_is(wide$comName, "character")
# expect_equal(NCOL(long), 4)
# expect_equal(NCOL(wide), 49)
# TL1 <- ebirdfreq("hotspots", "L196159", 2015, 2015, 1, 1)
# TL12 <- ebirdfreq("hotspots", "L196159", 2015, 2015, 1, 12)
# expect_that(NROW(TL1), is_less_than(NROW(TL12)))
# expect_equal(NCOL(TL1), NCOL(TL12))
})

test_that("ebirdfreq fails correctly", {
Expand All @@ -24,8 +26,8 @@ test_that("ebirdfreq fails correctly", {
expect_error(ebirdfreq("foo"))
expect_error(ebirdfreq("hotspots", "foo123"), "Invalid hotspot code")
expect_error(ebirdfreq("country"), "Not a valid location type")
expect_error(ebirdfreq("counties", "CA-BC-ZZ"), class = "http_500")
expect_error(ebirdfreq("hotspots", "L196159", 1900, 1902, long = FALSE), class = "http_500")
# expect_error(ebirdfreq("counties", "CA-BC-ZZ"), class = "http_500")
# expect_error(ebirdfreq("hotspots", "L196159", 1900, 1902, long = FALSE), class = "http_500")
expect_error(ebirdfreq("foo", "CA-BC-ZZ"), "Not a valid location type")
})

0 comments on commit 4852787

Please sign in to comment.