diff --git a/NAMESPACE b/NAMESPACE index 62e8b3e..e9e1430 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,7 @@ # Generated by roxygen2: do not edit by hand +S3method("[",osem_measurements) +S3method("[",sensebox) S3method(osem_measurements,bbox) S3method(osem_measurements,default) S3method(osem_measurements,sensebox) @@ -8,6 +10,16 @@ S3method(plot,osem_measurements) S3method(plot,sensebox) S3method(print,sensebox) S3method(summary,sensebox) +export(filter.osem_measurements) +export(filter.sensebox) +export(filter_.osem_measurements) +export(filter_.sensebox) +export(mutate.osem_measurements) +export(mutate.sensebox) +export(mutate_.osem_measurements) +export(mutate_.sensebox) +export(osem_as_measurements) +export(osem_as_sensebox) export(osem_as_sf) export(osem_box) export(osem_boxes) diff --git a/R/utils.R b/R/00utils.R similarity index 74% rename from R/utils.R rename to R/00utils.R index 5c2ba2e..2c69a67 100644 --- a/R/utils.R +++ b/R/00utils.R @@ -28,3 +28,13 @@ utc_date = function (date) { # NOTE: cannot handle mixed vectors of POSIXlt and POSIXct date_as_isostring = function (date) format.Date(date, format = '%FT%TZ') + +#' Simple factory function meant to implement dplyr functions for other classes, +#' which call an callback to attach the original class again after the fact. +#' +#' @param callback The function to call after the dplyr function +#' @noRd +dplyr_class_wrapper = function(callback) { + function(.data, ..., .dots) callback(NextMethod()) +} + diff --git a/R/api.R b/R/api.R index d707782..c6e502f 100644 --- a/R/api.R +++ b/R/api.R @@ -41,8 +41,7 @@ get_measurements_ = function (..., endpoint) { )) }) - class(result) = c('osem_measurements', class(result)) - result + osem_as_measurements(result) } get_stats_ = function (endpoint) { diff --git a/R/box.R b/R/box.R index 7953ba6..ae44cb6 100644 --- a/R/box.R +++ b/R/box.R @@ -126,6 +126,5 @@ parse_senseboxdata = function (boxdata) { thebox$height = location[[1]]$geometry$coordinates[[3]] # attach a custom class for methods - class(thebox) = c('sensebox', class(thebox)) - thebox + osem_as_sensebox(thebox) } diff --git a/R/box_utils.R b/R/box_utils.R index 62e63c9..0edff4e 100644 --- a/R/box_utils.R +++ b/R/box_utils.R @@ -73,3 +73,30 @@ summary.sensebox = function(object, ...) { invisible(object) } + +#' Converts a foreign object to an sensebox data.frame. +#' @param x A data.frame to attach the class to +#' @export +osem_as_sensebox = function(x) { + ret = as.data.frame(x) + class(ret) = c('sensebox', class(x)) + ret +} + +#' @export +filter_.sensebox = dplyr_class_wrapper(osem_as_sensebox) +#' @export +filter.sensebox = dplyr_class_wrapper(osem_as_sensebox) +#' @export +mutate_.sensebox = dplyr_class_wrapper(osem_as_sensebox) +#' @export +mutate.sensebox = dplyr_class_wrapper(osem_as_sensebox) + +#' maintains class / attributes after subsetting +#' @noRd +#' @export +`[.sensebox` = function(x, i, ...) { + s = NextMethod('[') + mostattributes(s) = attributes(x) + s +} diff --git a/R/measurement_utils.R b/R/measurement_utils.R index 0b578a6..bb0b0de 100644 --- a/R/measurement_utils.R +++ b/R/measurement_utils.R @@ -6,3 +6,31 @@ plot.osem_measurements = function (x, ...) { par(mar = oldpar$mar) invisible(x) } + +#' Converts a foreign object to an osem_measurements data.frame. +#' @param x A data.frame to attach the class to +#' @export +osem_as_measurements = function(x) { + ret = as.data.frame(x) + class(ret) = c('osem_measurements', class(x)) + ret +} + +#' @export +filter_.osem_measurements = dplyr_class_wrapper(osem_as_measurements) +#' @export +filter.osem_measurements = dplyr_class_wrapper(osem_as_measurements) +#' @export +mutate_.osem_measurements = dplyr_class_wrapper(osem_as_measurements) +#' @export +mutate.osem_measurements = dplyr_class_wrapper(osem_as_measurements) + +#' maintains class / attributes after subsetting +#' @noRd +#' @export +`[.osem_measurements` = function(x, i, ...) { + s = NextMethod('[') + mostattributes(s) = attributes(x) + s +} + diff --git a/man/osem_as_measurements.Rd b/man/osem_as_measurements.Rd new file mode 100644 index 0000000..ccb0544 --- /dev/null +++ b/man/osem_as_measurements.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/measurement_utils.R +\name{osem_as_measurements} +\alias{osem_as_measurements} +\title{Converts a foreign object to an osem_measurements data.frame.} +\usage{ +osem_as_measurements(x) +} +\arguments{ +\item{x}{A data.frame to attach the class to} +} +\description{ +Converts a foreign object to an osem_measurements data.frame. +} diff --git a/man/osem_as_sensebox.Rd b/man/osem_as_sensebox.Rd new file mode 100644 index 0000000..904cca1 --- /dev/null +++ b/man/osem_as_sensebox.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/box_utils.R +\name{osem_as_sensebox} +\alias{osem_as_sensebox} +\title{Converts a foreign object to an sensebox data.frame.} +\usage{ +osem_as_sensebox(x) +} +\arguments{ +\item{x}{A data.frame to attach the class to} +} +\description{ +Converts a foreign object to an sensebox data.frame. +} diff --git a/man/osem_as_sf.Rd b/man/osem_as_sf.Rd index 833e1f3..7495a4d 100644 --- a/man/osem_as_sf.Rd +++ b/man/osem_as_sf.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R +% Please edit documentation in R/00utils.R \name{osem_as_sf} \alias{osem_as_sf} \title{Convert a \code{sensebox} or \code{osem_measurements} dataframe to an