mirror of
https://github.com/sensebox/opensensmapr
synced 2025-03-11 03:30:27 +01:00
dynamically load S3 methods from foreign packages
namely dplyr::mutate, dplyr::filter, sf::st_as_sf this is due to changes in the upcoming R release 3.6.0. the approach taken is copied from the sf package.
This commit is contained in:
parent
aa453d6afe
commit
6a42357ec3
11 changed files with 49 additions and 23 deletions
|
@ -39,5 +39,5 @@ Description: Download environmental measurements and sensor station metadata
|
|||
License: GPL (>= 2) | file LICENSE
|
||||
Encoding: UTF-8
|
||||
LazyData: true
|
||||
RoxygenNote: 6.0.1
|
||||
RoxygenNote: 6.1.0
|
||||
VignetteBuilder: knitr
|
||||
|
|
|
@ -11,10 +11,6 @@ S3method(plot,sensebox)
|
|||
S3method(print,osem_measurements)
|
||||
S3method(print,sensebox)
|
||||
S3method(summary,sensebox)
|
||||
export(filter.osem_measurements)
|
||||
export(filter.sensebox)
|
||||
export(mutate.osem_measurements)
|
||||
export(mutate.sensebox)
|
||||
export(osem_as_measurements)
|
||||
export(osem_as_sensebox)
|
||||
export(osem_box)
|
||||
|
@ -24,8 +20,6 @@ export(osem_counts)
|
|||
export(osem_endpoint)
|
||||
export(osem_measurements)
|
||||
export(osem_phenomena)
|
||||
export(st_as_sf.osem_measurements)
|
||||
export(st_as_sf.sensebox)
|
||||
importFrom(graphics,legend)
|
||||
importFrom(graphics,par)
|
||||
importFrom(graphics,plot)
|
||||
|
|
|
@ -87,7 +87,6 @@ osem_as_sensebox = function(x) {
|
|||
#' @param .dots see corresponding function in package \code{\link{dplyr}}
|
||||
#' @param ... other arguments
|
||||
#' @seealso \code{\link[dplyr]{filter}}
|
||||
#' @export
|
||||
filter.sensebox = dplyr_class_wrapper(osem_as_sensebox)
|
||||
|
||||
#' Add new variables to the data, while maintaining class & attributes
|
||||
|
@ -95,7 +94,6 @@ filter.sensebox = dplyr_class_wrapper(osem_as_sensebox)
|
|||
#' @param .dots see corresponding function in package \code{\link{dplyr}}
|
||||
#' @param ... other arguments
|
||||
#' @seealso \code{\link[dplyr]{mutate}}
|
||||
#' @export
|
||||
mutate.sensebox = dplyr_class_wrapper(osem_as_sensebox)
|
||||
|
||||
# ==============================================================================
|
||||
|
@ -116,7 +114,6 @@ mutate.sensebox = dplyr_class_wrapper(osem_as_sensebox)
|
|||
#' @param x The object to convert
|
||||
#' @param ... maybe more objects to convert
|
||||
#' @return The object with an st_geometry column attached.
|
||||
#' @export
|
||||
st_as_sf.sensebox = function (x, ...) {
|
||||
NextMethod(x, ..., coords = c('lon', 'lat'), crs = 4326)
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@ osem_as_measurements = function(x) {
|
|||
#' @param .dots see corresponding function in package \code{\link{dplyr}}
|
||||
#' @param ... other arguments
|
||||
#' @seealso \code{\link[dplyr]{filter}}
|
||||
#' @export
|
||||
filter.osem_measurements = dplyr_class_wrapper(osem_as_measurements)
|
||||
|
||||
#' Add new variables to the data, while maintaining class & attributes
|
||||
|
@ -35,7 +34,6 @@ filter.osem_measurements = dplyr_class_wrapper(osem_as_measurements)
|
|||
#' @param .dots see corresponding function in package \code{\link{dplyr}}
|
||||
#' @param ... other arguments
|
||||
#' @seealso \code{\link[dplyr]{mutate}}
|
||||
#' @export
|
||||
mutate.osem_measurements = dplyr_class_wrapper(osem_as_measurements)
|
||||
|
||||
#' maintains class / attributes after subsetting
|
||||
|
@ -54,7 +52,6 @@ mutate.osem_measurements = dplyr_class_wrapper(osem_as_measurements)
|
|||
#' @param x The object to convert
|
||||
#' @param ... maybe more objects to convert
|
||||
#' @return The object with an st_geometry column attached.
|
||||
#' @export
|
||||
st_as_sf.osem_measurements = function (x, ...) {
|
||||
NextMethod(x, ..., coords = c('lon', 'lat'), crs = 4326)
|
||||
}
|
||||
|
|
39
R/tidyverse.R
Normal file
39
R/tidyverse.R
Normal file
|
@ -0,0 +1,39 @@
|
|||
# helpers for the dplyr & co related functions
|
||||
# also custom method registration
|
||||
|
||||
# they need to be registered, but not exported, see https://github.com/klutometis/roxygen/issues/796
|
||||
# we're using a different workaround than suggested, copied from edzer's sf package:
|
||||
# dynamically register the methods only when the related package is loaded as well.
|
||||
|
||||
# from: https://github.com/tidyverse/hms/blob/master/R/zzz.R
|
||||
# Thu Apr 19 10:53:24 CEST 2018
|
||||
register_s3_method <- function(pkg, generic, class, fun = NULL) {
|
||||
stopifnot(is.character(pkg), length(pkg) == 1)
|
||||
stopifnot(is.character(generic), length(generic) == 1)
|
||||
stopifnot(is.character(class), length(class) == 1)
|
||||
|
||||
if (is.null(fun)) {
|
||||
fun <- get(paste0(generic, ".", class), envir = parent.frame())
|
||||
} else {
|
||||
stopifnot(is.function(fun))
|
||||
}
|
||||
|
||||
if (pkg %in% loadedNamespaces()) {
|
||||
registerS3method(generic, class, fun, envir = asNamespace(pkg))
|
||||
}
|
||||
|
||||
# Always register hook in case package is later unloaded & reloaded
|
||||
setHook(
|
||||
packageEvent(pkg, "onLoad"),
|
||||
function(...) {
|
||||
registerS3method(generic, class, fun, envir = asNamespace(pkg))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
register_s3_method('dplyr', 'filter', 'sensebox')
|
||||
register_s3_method('dplyr', 'mutate', 'sensebox')
|
||||
register_s3_method('dplyr', 'filter', 'osem_measurements')
|
||||
register_s3_method('dplyr', 'mutate', 'osem_measurements')
|
||||
register_s3_method('sf', 'st_as_sf', 'sensebox')
|
||||
register_s3_method('sf', 'st_as_sf', 'osem_measurements')
|
|
@ -4,7 +4,7 @@
|
|||
\alias{filter.osem_measurements}
|
||||
\title{Return rows with matching conditions, while maintaining class & attributes}
|
||||
\usage{
|
||||
filter.osem_measurements(.data, ..., .dots)
|
||||
\method{filter}{osem_measurements}(.data, ..., .dots)
|
||||
}
|
||||
\arguments{
|
||||
\item{.data}{A osem_measurements data.frame to filter}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
\alias{filter.sensebox}
|
||||
\title{Return rows with matching conditions, while maintaining class & attributes}
|
||||
\usage{
|
||||
filter.sensebox(.data, ..., .dots)
|
||||
\method{filter}{sensebox}(.data, ..., .dots)
|
||||
}
|
||||
\arguments{
|
||||
\item{.data}{A sensebox data.frame to filter}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
\alias{mutate.osem_measurements}
|
||||
\title{Add new variables to the data, while maintaining class & attributes}
|
||||
\usage{
|
||||
mutate.osem_measurements(.data, ..., .dots)
|
||||
\method{mutate}{osem_measurements}(.data, ..., .dots)
|
||||
}
|
||||
\arguments{
|
||||
\item{.data}{A osem_measurements data.frame to mutate}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
\alias{mutate.sensebox}
|
||||
\title{Add new variables to the data, while maintaining class & attributes}
|
||||
\usage{
|
||||
mutate.sensebox(.data, ..., .dots)
|
||||
\method{mutate}{sensebox}(.data, ..., .dots)
|
||||
}
|
||||
\arguments{
|
||||
\item{.data}{A sensebox data.frame to mutate}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
\name{opensensmapr}
|
||||
\alias{opensensmapr}
|
||||
\alias{opensensmapr-package}
|
||||
\alias{opensensmapr-package}
|
||||
\title{opensensmapr: Get sensor data from opensensemap.org}
|
||||
\description{
|
||||
The opensensmapr package provides functions for
|
||||
|
|
|
@ -11,13 +11,13 @@ osem_measurements(x, ...)
|
|||
|
||||
\method{osem_measurements}{default}(x, ...)
|
||||
|
||||
\method{osem_measurements}{bbox}(x, phenomenon, exposure = NA, from = NA,
|
||||
to = NA, columns = NA, ..., endpoint = osem_endpoint(), progress = T,
|
||||
cache = NA)
|
||||
\method{osem_measurements}{bbox}(x, phenomenon, exposure = NA,
|
||||
from = NA, to = NA, columns = NA, ...,
|
||||
endpoint = osem_endpoint(), progress = T, cache = NA)
|
||||
|
||||
\method{osem_measurements}{sensebox}(x, phenomenon, exposure = NA,
|
||||
from = NA, to = NA, columns = NA, ..., endpoint = osem_endpoint(),
|
||||
progress = T, cache = NA)
|
||||
from = NA, to = NA, columns = NA, ...,
|
||||
endpoint = osem_endpoint(), progress = T, cache = NA)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{Depending on the method, either
|
||||
|
|
Loading…
Add table
Reference in a new issue