From b3ebaa85f9375160bb1870b15d94d935f6a43744 Mon Sep 17 00:00:00 2001 From: Dave Slager Date: Sat, 23 Mar 2024 19:58:22 -0700 Subject: [PATCH] fix checklistfeed for multiple media types --- R/ebirdchecklist.R | 14 +++++++-- man/ebirdchecklist.Rd | 6 ++++ man/rebird-package.Rd | 1 + tests/fixtures/ebirdchecklist.yml | 47 +++++++++++++++++++++++++--- tests/testthat/test-ebirdchecklist.R | 5 +++ 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/R/ebirdchecklist.R b/R/ebirdchecklist.R index 9e0948e..433e38d 100644 --- a/R/ebirdchecklist.R +++ b/R/ebirdchecklist.R @@ -34,6 +34,9 @@ #' @return "howManyStr" number of individuals reported for each taxon #' @return "exoticCategory" exotic species categories for each taxon #' @return "obsComments" observation comments for each taxon +#' @return "photoCounts" number of photos for each taxon +#' @return "audioCounts" number of audio files for each taxon +#' @return "videoCounts" number of video files for each taxon #' @return "auxCode" breding code for each taxon #' @export @@ -103,11 +106,18 @@ ebirdchecklist <- function(subId, sleep = 0, key = NULL, other = FALSE, ...) { obs_df$subnational1Code <- NULL obs_df$obsDt <- NULL obs_df$projId <- NULL - # mediaCounts appears to just be a nested integer vector (?) - obs_df$mediaCounts <- Reduce(c, obs_df$mediaCounts) # 'comments' column has name duplicated with checklist comments names(obs_df)[names(obs_df) == 'comments'] <- 'obsComments' + # deal with mediaCounts + if (! is.null(obs_df$mediaCounts)){ + mediaCounts <- obs_df$mediaCounts + idx <- match(names(mediaCounts), c('P','A','V')) + names(mediaCounts) <- c('photoCounts', 'audioCounts', 'videoCounts')[idx] + obs_df$mediaCounts <- NULL + obs_df <- cbind(obs_df, mediaCounts) + } + # join to get result df out_df <- sub_df if (! is.null(subAux_df) && other){ diff --git a/man/ebirdchecklist.Rd b/man/ebirdchecklist.Rd index 105ffbe..c61d229 100644 --- a/man/ebirdchecklist.Rd +++ b/man/ebirdchecklist.Rd @@ -67,6 +67,12 @@ A 'tibble' 'data.frame' containing checklist information: "obsComments" observation comments for each taxon +"photoCounts" number of photos for each taxon + +"audioCounts" number of audio files for each taxon + +"videoCounts" number of video files for each taxon + "auxCode" breding code for each taxon } \description{ diff --git a/man/rebird-package.Rd b/man/rebird-package.Rd index dfb5262..91a9de1 100644 --- a/man/rebird-package.Rd +++ b/man/rebird-package.Rd @@ -32,6 +32,7 @@ Other contributors: \item Guy Babineau \email{guy.babineau@gmail.com} [contributor] \item Marianna Foos \email{marianna.foos@gmail.com} [contributor] \item David Bradnum \email{david.bradnum@gmail.com} [contributor] + \item Dave Slager [contributor] } } diff --git a/tests/fixtures/ebirdchecklist.yml b/tests/fixtures/ebirdchecklist.yml index fddef93..38df8b2 100644 --- a/tests/fixtures/ebirdchecklist.yml +++ b/tests/fixtures/ebirdchecklist.yml @@ -18,7 +18,7 @@ http_interactions: cache-control: no-cache, no-store, max-age=0, must-revalidate content-encoding: gzip content-type: application/json;charset=utf-8 - date: Sat, 23 Mar 2024 05:57:03 GMT + date: Sun, 24 Mar 2024 02:48:14 GMT expires: '0' pragma: no-cache server: Apache @@ -41,7 +41,46 @@ http_interactions: 06:55","subnational1Code":"US-WA","howManyAtleast":2,"howManyAtmost":2,"present":false,"subId":"S117450946","projId":"EBIRD","obsId":"OBS1503894278","howManyStr":"2"},{"speciesCode":"houspa","hideFlags":[],"exoticCategory":"N","obsDt":"2022-05-30 06:55","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"comments":"ON","present":false,"subId":"S117450946","projId":"EBIRD","obsId":"OBS1503894274","howManyStr":"1","obsAux":[{"subId":"S117450946","fieldName":"breeding_code","entryMethodCode":"ebird_breeding_code","auxCode":"ON","obsId":"OBS1503894274","speciesCode":"houspa","value":"ON"}]},{"speciesCode":"pswspa1","hideFlags":[],"obsDt":"2022-05-30 06:55","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"present":false,"subId":"S117450946","projId":"EBIRD","obsId":"OBS1503894276","howManyStr":"1"}]}' - recorded_at: 2024-03-23 06:01:43 GMT + recorded_at: 2024-03-24 02:52:56 GMT + recorded_with: vcr/1.2.2, webmockr/0.9.0 +- request: + method: get + uri: https://ebird.org/ws2.0/product/checklist/view/S89475689 + body: + encoding: '' + string: '' + headers: + Accept: application/json, text/xml, application/xml, */* + X-eBirdApiToken: <<>> + response: + status: + status_code: 200 + category: Success + reason: OK + message: 'Success: (200) OK' + headers: + cache-control: no-cache, no-store, max-age=0, must-revalidate + content-encoding: gzip + content-type: application/json;charset=utf-8 + date: Sun, 24 Mar 2024 02:48:14 GMT + expires: '0' + pragma: no-cache + server: Apache + strict-transport-security: max-age=31536000 ; includeSubDomains + vary: Origin,Accept-Encoding,Access-Control-Request-Method,Access-Control-Request-Headers + x-content-type-options: nosniff + x-frame-options: DENY + x-xss-protection: 1; mode=block + content-length: '484' + body: + encoding: '' + file: no + string: '{"projId":"EBIRD","subId":"S89475689","protocolId":"P20","locId":"L15211376","groupId":"G6835020","allObsReported":false,"creationDt":"2021-06-02 + 10:01","lastEditedDt":"2023-01-19 09:41","obsDt":"2021-05-30 02:51","obsTimeValid":true,"checklistId":"CL23150","numObservers":2,"subnational1Code":"US-WA","submissionMethodCode":"EBIRD_android","submissionMethodVersion":"2.8.1_SDK24","userDisplayName":"Dave + Slager","numSpecies":2,"submissionMethodVersionDisp":"2.8.1","subAux":[{"subId":"S89475689","fieldName":"nocturnal","entryMethodCode":"ebird_nocturnal","auxCode":"1"}],"subAuxAi":[],"obs":[{"speciesCode":"flaowl","hideFlags":[],"obsDt":"2021-05-30 + 02:51","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"comments":"Ho","mediaCounts":{"A":1},"present":false,"subId":"S89475689","projId":"EBIRD","obsId":"OBS1173665676","howManyStr":"1"},{"speciesCode":"grhowl","hideFlags":[],"obsDt":"2021-05-30 + 02:51","subnational1Code":"US-WA","howManyAtleast":1,"howManyAtmost":1,"comments":"Ho","present":false,"subId":"S89475689","projId":"EBIRD","obsId":"OBS1173665675","howManyStr":"1"}]}' + recorded_at: 2024-03-24 02:52:56 GMT recorded_with: vcr/1.2.2, webmockr/0.9.0 - request: method: get @@ -62,7 +101,7 @@ http_interactions: cache-control: no-cache, no-store, max-age=0, must-revalidate content-encoding: gzip content-type: application/json - date: Sat, 23 Mar 2024 05:57:04 GMT + date: Sun, 24 Mar 2024 02:48:14 GMT expires: '0' pragma: no-cache server: Apache @@ -77,5 +116,5 @@ http_interactions: file: no string: '{"errors":[{"status":"400 BAD_REQUEST","code":"Pattern","title":"Field subId of checklistBySubIdCmd: subId is invalid."}]}' - recorded_at: 2024-03-23 06:01:43 GMT + recorded_at: 2024-03-24 02:52:56 GMT recorded_with: vcr/1.2.2, webmockr/0.9.0 diff --git a/tests/testthat/test-ebirdchecklist.R b/tests/testthat/test-ebirdchecklist.R index 2c3034d..b9a85d8 100644 --- a/tests/testthat/test-ebirdchecklist.R +++ b/tests/testthat/test-ebirdchecklist.R @@ -15,6 +15,11 @@ vcr::use_cassette("ebirdchecklist", { expect_true("checklistId" %in% names(out1)) expect_equal(out1$checklistId[1], "CL24321") + # works with media + expect_no_error(out2 <- ebirdchecklist("S89475689", other = TRUE)) + expect_true('audioCounts' %in% colnames(out2)) + expect_equal(nrow(out2), 2) + }) test_that("ebirdchecklist errors for bad input", {