Skip to content

Commit

Permalink
fix(api): ensure numeric conversion and proper formatting in mx_api_c…
Browse files Browse the repository at this point in the history
…ontent
  • Loading branch information
esloch committed May 18, 2024
1 parent 154a598 commit 4df24e8
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions R/mx_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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) {
Expand Down

0 comments on commit 4df24e8

Please sign in to comment.