From 4e837296d15f3d873b34ecff938e75dcd94d5d50 Mon Sep 17 00:00:00 2001 From: Maximilian Held Date: Tue, 16 Feb 2021 16:17:48 +0100 Subject: [PATCH] hack fix failed fetch, closes #110 protects against #117 --- DESCRIPTION | 2 +- R/cr_md_fetch.R | 31 ++++++++++++++++++++++++++++++ inst/app/index.Rmd | 32 +++++++++++++++++++++++++++---- tests/testthat/test-cr_md_fetch.R | 6 +++--- 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c37c1dce..a8b06a74 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -80,5 +80,5 @@ Remotes: subugoe/shinycaas@1.0.0, subugoe/rcrossref@51d036808c1dc1f46cf1bad1b136e97ad213e162, rstudio/shinyvalidate@420aec079dbd2c0e12bf8350f4452d105a9cc45c, - subugoe/biblids@d44010e7d0b7d2eef016ef33a05d29e67417dfe6 + subugoe/biblids@e7517c4a3d9a424616adca3282abd30fa994f713 Config/testthat/edition: 3 diff --git a/R/cr_md_fetch.R b/R/cr_md_fetch.R index 0110fc5b..81c1720d 100644 --- a/R/cr_md_fetch.R +++ b/R/cr_md_fetch.R @@ -104,3 +104,34 @@ license_normalise <- function(cr) { mutate(license = gsub("/", "", license)) %>% mutate(license = tolower(license)) } + +#' Test whether DOI as metadata on Crossref +#' +#' @param doi [biblids::doi()] of length 1 +#' +#' @noRd +has_cr_md <- function(doi) { + # TODO this is a bad hackjob, and should be replaced by proper biblids code asap + res <- suppressWarnings(rcrossref::cr_works(biblids::as_doi(doi))) + # TODO this is a very bad proxy; we actually mean the http response code, but rcrossref doesn't readily give that + nrow(res$data) != 0 +} + +#' @describeIn has_cr_md Vectorised version +#' @param dois [biblids::doi()] +#' @noRd +have_cr_md <- function(dois) purrr::map_lgl(dois, has_cr_md) + +assert_cr_md <- function(dois) { + with_cr_md <- have_cr_md(dois) + if (any(with_cr_md)) { + warning( + "Omitted some DOIs for which no metadata on Crossref could be found." + ) + } + if (all(!with_cr_md)) { + warning() + } + + return(NULL) +} diff --git a/inst/app/index.Rmd b/inst/app/index.Rmd index 7e44f1d6..787f05be 100644 --- a/inst/app/index.Rmd +++ b/inst/app/index.Rmd @@ -41,6 +41,29 @@ session_id <- as.character(floor(runif(1) * 1e20)) # get dois dois <- biblids::doiEntryServer(id = "dois") +dois2 <- reactive({ + with_cr_md <- have_cr_md(dois()) + if (sum(with_cr_md) < 3) { + shiny::showNotification( + ui = c( + "Please provide at least 2 DOIs that can be found on crossref." + ), + type = "error" + ) + return(NULL) + } else if (any(!with_cr_md)) { + shiny::showNotification( + ui = c( + "Omitted some DOIs for which no metadata on Crossref could be found." + ), + type = "warning" + ) + return(dois()[with_cr_md]) + } else { + dois() + } +}) + # email iv <- InputValidator$new() iv$add_rule("email", sv_required()) @@ -48,10 +71,11 @@ iv$add_rule("email", ~ if (!is_valid_email(.)) "Please provide a valid email") iv$enable() html_email <- eventReactive(input$create, { - if (length(dois()) > 1) { + print(dois2()) + if (isTruthy(dois2())) { withProgress( expr = { - render_email(dois = dois(), session_id = session_id)$html_html + render_email(dois = dois2(), session_id = session_id)$html_html }, message = "Generating report..." ) @@ -60,12 +84,12 @@ html_email <- eventReactive(input$create, { output$draft <- renderUI(html_email()) observeEvent(input$send, { - if (length(dois() > 1 & iv$is_valid())) { + if (isTruthy(dois2())) { withProgress( expr = { send_email( to = input$email, - email = render_email(dois = dois(), session_id = session_id) + email <- render_email(dois = dois2(), session_id = session_id) ) }, message = "Generating report and sending e-mail. You can close this window." diff --git a/tests/testthat/test-cr_md_fetch.R b/tests/testthat/test-cr_md_fetch.R index 94a74673..e06579f0 100644 --- a/tests/testthat/test-cr_md_fetch.R +++ b/tests/testthat/test-cr_md_fetch.R @@ -6,7 +6,7 @@ test_that("metadata can be retrieved", { ) }) - -test_that("returns NULL if no metadata was retrieved", { - expect_null(get_cr_md("10.1000/foo")) +test_that("DOIs not on CR are identified", { + expect_true(has_cr_md("10.5194/wes-2019-70")) + expect_equal(has_cr_md("10.1000/foo"), FALSE) })