mirror of
https://github.com/sensebox/opensensmapr
synced 2025-06-07 17:06:10 +02:00
add S3 utility functions
attach the class to an object: - osem_as_sensebox - osem_as_measurements retain the class after filtering / mutating / subsetting: - filter.sensebox - filter.osem_measurements - mutate.sensebox - mutate.osem_measurements - `[.sensebox` - `[.osem_measurements`
This commit is contained in:
parent
c216ec9f61
commit
ef2baa6559
9 changed files with 108 additions and 5 deletions
12
NAMESPACE
12
NAMESPACE
|
@ -1,5 +1,7 @@
|
||||||
# Generated by roxygen2: do not edit by hand
|
# Generated by roxygen2: do not edit by hand
|
||||||
|
|
||||||
|
S3method("[",osem_measurements)
|
||||||
|
S3method("[",sensebox)
|
||||||
S3method(osem_measurements,bbox)
|
S3method(osem_measurements,bbox)
|
||||||
S3method(osem_measurements,default)
|
S3method(osem_measurements,default)
|
||||||
S3method(osem_measurements,sensebox)
|
S3method(osem_measurements,sensebox)
|
||||||
|
@ -8,6 +10,16 @@ S3method(plot,osem_measurements)
|
||||||
S3method(plot,sensebox)
|
S3method(plot,sensebox)
|
||||||
S3method(print,sensebox)
|
S3method(print,sensebox)
|
||||||
S3method(summary,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_as_sf)
|
||||||
export(osem_box)
|
export(osem_box)
|
||||||
export(osem_boxes)
|
export(osem_boxes)
|
||||||
|
|
|
@ -28,3 +28,13 @@ utc_date = function (date) {
|
||||||
|
|
||||||
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
|
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
|
||||||
date_as_isostring = function (date) format.Date(date, format = '%FT%TZ')
|
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())
|
||||||
|
}
|
||||||
|
|
3
R/api.R
3
R/api.R
|
@ -41,8 +41,7 @@ get_measurements_ = function (..., endpoint) {
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
|
||||||
class(result) = c('osem_measurements', class(result))
|
osem_as_measurements(result)
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get_stats_ = function (endpoint) {
|
get_stats_ = function (endpoint) {
|
||||||
|
|
3
R/box.R
3
R/box.R
|
@ -126,6 +126,5 @@ parse_senseboxdata = function (boxdata) {
|
||||||
thebox$height = location[[1]]$geometry$coordinates[[3]]
|
thebox$height = location[[1]]$geometry$coordinates[[3]]
|
||||||
|
|
||||||
# attach a custom class for methods
|
# attach a custom class for methods
|
||||||
class(thebox) = c('sensebox', class(thebox))
|
osem_as_sensebox(thebox)
|
||||||
thebox
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,3 +73,30 @@ summary.sensebox = function(object, ...) {
|
||||||
|
|
||||||
invisible(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
|
||||||
|
}
|
||||||
|
|
|
@ -6,3 +6,31 @@ plot.osem_measurements = function (x, ...) {
|
||||||
par(mar = oldpar$mar)
|
par(mar = oldpar$mar)
|
||||||
invisible(x)
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
14
man/osem_as_measurements.Rd
Normal file
14
man/osem_as_measurements.Rd
Normal file
|
@ -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.
|
||||||
|
}
|
14
man/osem_as_sensebox.Rd
Normal file
14
man/osem_as_sensebox.Rd
Normal file
|
@ -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.
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
% Generated by roxygen2: do not edit by hand
|
% 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}
|
\name{osem_as_sf}
|
||||||
\alias{osem_as_sf}
|
\alias{osem_as_sf}
|
||||||
\title{Convert a \code{sensebox} or \code{osem_measurements} dataframe to an
|
\title{Convert a \code{sensebox} or \code{osem_measurements} dataframe to an
|
||||||
|
|
Loading…
Add table
Reference in a new issue