Skip to content

Commit

Permalink
feat: added monitor_selectWhere()
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathancallahan committed Sep 23, 2022
1 parent b32fbb1 commit 095b6be
Show file tree
Hide file tree
Showing 83 changed files with 664 additions and 89 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Type: Package
Package: AirMonitor
Version: 0.3.1
Version: 0.3.2
Title: Air Quality Data Analysis
Authors@R: c(
person("Jonathan", "Callahan", email="[email protected]", role=c("aut","cre")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export(monitor_nowcast)
export(monitor_reorder)
export(monitor_replaceValues)
export(monitor_select)
export(monitor_selectWhere)
export(monitor_timeInfo)
export(monitor_timeRange)
export(monitor_timeseriesPlot)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# AirMonitor 0.3.2

* Added `monitor_selectWhere()` for data based selection.

# AirMonitor 0.3.1

* Updated `monitor_toPWFSLSmoke()` and `monitor_fromPWFSLSmoke()` to support
Expand Down
92 changes: 92 additions & 0 deletions R/monitor_selectWhere.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#' @export
#' @importFrom rlang .data
#'
#' @title Data-based subsetting of time series within an \emph{mts_monitor} object.
#'
#' @param monitor \emph{mts_monitor} object.
#' @param FUN A function applied to time series data that returns TRUE or FALSE.
#'
#' @description
#' Subsetting of \code{monitor} acts similarly to \code{tidyselect::where()} working on
#' \code{monitor$data}. The returned \emph{mts_monitor} object will contain only
#' those time series where \code{FUN} applied to the time series data returns \code{TRUE}.
#'
#' @return A subset of the incoming \emph{mts_monitor} object. (A list with
#' \code{meta} and \code{data} dataframes.)
#'
#' @seealso \link{monitor_select}
#'
#' @examples
#' library(AirMonitor)
#'
#' # Show all Camp_Fire locations
#' Camp_Fire$meta$locationName
#'
#' # Use package US_AQI data for HAZARDOUS
#' name <- US_AQI$names_eng[6]
#' threshold <- US_AQI$breaks_PM2.5[6]
#'
#' # Find HAZARDOUS locations
#' worst_sites <-
#' Camp_Fire %>%
#' monitor_selectWhere(
#' function(x) { any(x >= threshold, na.rm = TRUE) }
#' )
#'
#' # Show the worst locations
#' worst_sites$meta$locationName
#'

monitor_selectWhere <- function(
monitor,
FUN
) {

# ----- Validate parameters --------------------------------------------------

# A little involved to catch the case where the user forgets to pass in 'monitor'
result <- try({
if ( !monitor_isValid(monitor) )
stop("First argument is not a valid 'mts_monitor' object.")
}, silent = TRUE)

if ( class(result) %in% "try-error" ) {
err_msg <- geterrmessage()
if ( stringr::str_detect(err_msg, "object .* not found") ) {
stop(paste0(err_msg, "\n(Did you forget to pass in the 'monitor' object?)"))
}
}

if ( monitor_isEmpty(monitor) )
stop("Parameter 'monitor' has no data.")

if ( !is.function(FUN) )
stop("'FUN' is not a function.")

if ( !is.logical(FUN(c(1:5,NA,6:10))) )
stop("'FUN' does not return a logical value. Do you need to include 'na.rm = TRUE'?")

# ----- Apply function -------------------------------------------------------

# See https://dplyr.tidyverse.org/articles/colwise.html

# Get dataBrick
tbl <- monitor$data[,-1]

# Apply function
mask <-
tbl %>%
dplyr::summarize(dplyr::across(.cols = dplyr::everything(), FUN)) %>%
as.logical()

# Get deviceDeploymentIDs where FUN returns TRUE
ids <- names(tbl)[mask]

# Select those monitors
monitor <- monitor %>% monitor_select(ids)

# ----- Return ---------------------------------------------------------------

return(invisible(monitor))

}
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions docs/articles/AirMonitor.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/articles/Data_Model.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/Developer_Style_Guide.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/articles/Citations.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ articles:
Data_Model: Data_Model.html
Developer_Style_Guide: Developer_Style_Guide.html
articles/Citations: Citations.html
last_built: 2022-08-10T22:28Z
last_built: 2022-09-23T23:58Z

2 changes: 1 addition & 1 deletion docs/reference/AirFire_S3_archiveBaseUrl.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/AirMonitor.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/CONUS.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/Camp_Fire.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/Carmel_Valley.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/NW_Megafires.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/US_52.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/US_AQI.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/addAQILegend.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/addAQILines.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/addAQIStackedBar.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/addShadedNight.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/airnow_loadAnnual.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/airnow_loadDaily.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/airnow_loadLatest.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/airnow_loadMonthly.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/airsis_loadAnnual.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/airsis_loadDaily.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/airsis_loadLatest.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 095b6be

Please sign in to comment.