From 4df24e8836de8a0553f4a1a058bcc79bee8fa364 Mon Sep 17 00:00:00 2001 From: Sandro Loch Date: Fri, 17 May 2024 23:17:52 -0300 Subject: [PATCH] fix(api): ensure numeric conversion and proper formatting in mx_api_content --- R/mx_api.R | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/R/mx_api.R b/R/mx_api.R index 1e45b47..d37b5ce 100644 --- a/R/mx_api.R +++ b/R/mx_api.R @@ -39,12 +39,10 @@ mx_api_content <- function(from_date = "2013-01-01", server = "medrxiv", include_info = FALSE) { - # Check that the user is connected to the internet internet_check() # Check server - "%notin%" <- Negate("%in%") if (server %notin% c("medrxiv", "biorxiv")) { @@ -56,10 +54,13 @@ mx_api_content <- function(from_date = "2013-01-01", # Get descriptive details and page number details_link <- api_link(server, from_date, to_date, "0") - details <- api_to_df(details_link) - count <- details$messages[1, 6] + # Ensure 'count' is numeric + count <- as.numeric(details$messages[1, 6]) + if (is.na(count)) { + stop("Count value is not numeric.") + } pages <- floor(count / 100) message("Estimated total number of records as per API metadata: ", count) @@ -69,14 +70,13 @@ mx_api_content <- function(from_date = "2013-01-01", dplyr::filter(doi == "") # Get data - pb <- - progress::progress_bar$new( - format = paste0( - "Downloading... [:bar] :current/:total ", - "(:percent) Est. time remaining: :eta" - ), - total = count - ) + pb <- progress::progress_bar$new( + format = paste0( + "Downloading... [:bar] :current/:total ", + "(:percent) Est. time remaining: :eta" + ), + total = count + ) pb$tick(0) @@ -99,12 +99,11 @@ mx_api_content <- function(from_date = "2013-01-01", } # Clean data - message("Number of records retrieved from API: ", nrow(df)) - if (nrow(df)!= count) { + if (nrow(df) != count) { message(paste0("The estimated \"total number\" as per the metadata ", #nocov - "can sometimes be artifically inflated.")) #nocov + "can sometimes be artificially inflated.")) #nocov } if (clean == TRUE) {