diff --git a/R/package/DESCRIPTION b/R/package/DESCRIPTION index 193b688..44a2c6c 100644 --- a/R/package/DESCRIPTION +++ b/R/package/DESCRIPTION @@ -1,6 +1,6 @@ Package: eddington Title: Compute a Cyclist's Eddington Number -Version: 4.1.3 +Version: 4.2.0 Authors@R: c( person('Paul', 'Egeler', email = 'paulegeler@gmail.com', role = c('aut','cre')), person('Tashi', 'Reigle', role = 'ctb')) @@ -26,12 +26,12 @@ Imports: Rcpp, R6, methods, - stats, - XML + xml2 Suggests: testthat, knitr, rmarkdown, + stats, dplyr SystemRequirements: C++17 VignetteBuilder: knitr diff --git a/R/package/NEWS.md b/R/package/NEWS.md index f173fc9..032ece5 100644 --- a/R/package/NEWS.md +++ b/R/package/NEWS.md @@ -1,3 +1,9 @@ +# eddington 4.2.0 (Release date: 2024-03-23) + +Changes: + +- Substituted `XML` package dependency for `xml2`. + # eddington 4.1.2 (Release date: 2023-12-12) Changes: diff --git a/R/package/R/RcppExports.R b/R/package/R/RcppExports.R index c98b583..5286b10 100644 --- a/R/package/R/RcppExports.R +++ b/R/package/R/RcppExports.R @@ -32,7 +32,7 @@ #' rides <- rgamma(15, shape = 2, scale = 10) #' #' # View the rides sorted in decreasing order -#' setNames(sort(rides, decreasing = TRUE), seq_along(rides)) +#' stats::setNames(sort(rides, decreasing = TRUE), seq_along(rides)) #' #' # Get the Eddington number #' E_num(rides) diff --git a/R/package/R/eddington-R6.r b/R/package/R/eddington-R6.r index 736c534..f8785c1 100644 --- a/R/package/R/eddington-R6.r +++ b/R/package/R/eddington-R6.r @@ -17,7 +17,7 @@ #' rides <- rgamma(15, shape = 2, scale = 10) #' #' # View the rides sorted in decreasing order -#' setNames(sort(rides, decreasing = TRUE), seq_along(rides)) +#' stats::setNames(sort(rides, decreasing = TRUE), seq_along(rides)) #' #' # Create the Eddington object #' e <- Eddington$new(rides, store.cumulative = TRUE) diff --git a/R/package/R/gpx.r b/R/package/R/gpx.r index d6cc740..5b80c71 100644 --- a/R/package/R/gpx.r +++ b/R/package/R/gpx.r @@ -99,8 +99,7 @@ get_haversine_distance <- function(lat_1, #' } #' @export read_gpx <- function(file, units = c("miles", "kilometers")) { - x <- XML::xmlInternalTreeParse(file) - on.exit(XML::free(x)) + x <- xml2::read_xml(file) distances <- get_trkseg_dist(x, match.arg(units)) @@ -108,7 +107,7 @@ read_gpx <- function(file, units = c("miles", "kilometers")) { # No trkseg nodes were found. Returning an empty data frame return( data.frame( - date = double(0L), + date = structure(double(0L), class = "Date"), distance = double(0L) ) ) @@ -117,8 +116,7 @@ read_gpx <- function(file, units = c("miles", "kilometers")) { dates <- get_dates(x) data.frame( - # do.call preserves "Date" class - date = if (length(dates) == length(distances)) do.call(c, dates) else as.Date(NA), + date = if (length(dates) == length(distances)) dates else as.Date(NA), distance = unlist(distances) ) } @@ -126,58 +124,37 @@ read_gpx <- function(file, units = c("miles", "kilometers")) { # XML parsing helper functions -------------------------------------------- get_dates <- function(x) { - timestamps <- XML::xpathApply( - x, - "//d:trkseg/d:trkpt[1]/d:time", - XML::xmlValue, - namespaces = NAMESPACES - ) - lapply(timestamps, as.Date, TIMESTAMP_FORMAT) + x |> + xml2::xml_find_all("//d:trkseg/d:trkpt[1]/d:time", ns = NAMESPACES) |> + xml2::xml_text() |> + as.Date.character(TIMESTAMP_FORMAT) } get_trkseg_dist <- function(x, units = c("miles", "kilometers")) { r <- switch(match.arg(units), miles = R_E_MI, kilometers = R_E_KM) lapply( - XML::getNodeSet(x, "//d:trkseg", namespaces = NAMESPACES), - \(trkseg) { - trkpts <- XML::xmlChildren(trkseg) - if (length(trkpts) < 2) return(0.) - Reduce( - `+`, - apply( - cbind(trkpts[-1], trkpts[-length(trkpts)]), - 1, - \(pair) .Call(`_eddington_get_haversine_distance_`, - as.double(XML::xmlGetAttr(pair[[1]], "lat")), - as.double(XML::xmlGetAttr(pair[[1]], "lon")), - as.double(XML::xmlGetAttr(pair[[2]], "lat")), - as.double(XML::xmlGetAttr(pair[[2]], "lon")), + xml2::xml_find_all(x, "//d:trkseg", ns = NAMESPACES), + \(trkseg) { # get sum of distances for a single trkseg + trkpts <- xml2::xml_children(trkseg) + + if (length(trkpts) < 2L) + return(0) + + sum( + vapply( + seq_along(trkpts[-1L]), + \(i) .Call( + `_eddington_get_haversine_distance_`, + as.double(xml2::xml_attr(trkpts[[i]], "lat")), + as.double(xml2::xml_attr(trkpts[[i]], "lon")), + as.double(xml2::xml_attr(trkpts[[i + 1L]], "lat")), + as.double(xml2::xml_attr(trkpts[[i + 1L]], "lon")), r - ) + ), + double(1) ) ) - }) -} - -# Unused ------------------------------------------------------------------ - -get_trkseg_coords <- function(x) { - trksegs <- lapply( - XML::getNodeSet(x, "//d:trkseg", namespaces = NAMESPACES), - \(trkseg) XML::xmlApply( - trkseg, - \(trkpt) as.double(c(XML::xmlGetAttr(trkpt, "lat"), - XML::xmlGetAttr(trkpt, "lon"))) - ) - ) - lapply( - trksegs, - \(trkseg) stats::setNames( - as.data.frame( - do.call(rbind, trkseg), - row.names = FALSE), - c("lat", "lon") - ) + } ) } diff --git a/R/package/man/E_num.Rd b/R/package/man/E_num.Rd index 0293517..f4c6a1f 100644 --- a/R/package/man/E_num.Rd +++ b/R/package/man/E_num.Rd @@ -36,7 +36,7 @@ observable universe. rides <- rgamma(15, shape = 2, scale = 10) # View the rides sorted in decreasing order -setNames(sort(rides, decreasing = TRUE), seq_along(rides)) +stats::setNames(sort(rides, decreasing = TRUE), seq_along(rides)) # Get the Eddington number E_num(rides) diff --git a/R/package/man/Eddington.Rd b/R/package/man/Eddington.Rd index 045c474..c444af1 100644 --- a/R/package/man/Eddington.Rd +++ b/R/package/man/Eddington.Rd @@ -28,7 +28,7 @@ or \link[base:load]{base::load}. rides <- rgamma(15, shape = 2, scale = 10) # View the rides sorted in decreasing order -setNames(sort(rides, decreasing = TRUE), seq_along(rides)) +stats::setNames(sort(rides, decreasing = TRUE), seq_along(rides)) # Create the Eddington object e <- Eddington$new(rides, store.cumulative = TRUE) diff --git a/R/package/src/eddington.cpp b/R/package/src/eddington.cpp index 9a88c8e..580c3c0 100644 --- a/R/package/src/eddington.cpp +++ b/R/package/src/eddington.cpp @@ -33,7 +33,7 @@ //' rides <- rgamma(15, shape = 2, scale = 10) //' //' # View the rides sorted in decreasing order -//' setNames(sort(rides, decreasing = TRUE), seq_along(rides)) +//' stats::setNames(sort(rides, decreasing = TRUE), seq_along(rides)) //' //' # Get the Eddington number //' E_num(rides)