R/golem_utils_server.R

Defines functions drop_nulls count_crash_flag

# List of all available crash flags
crshflag_selected_inputs <-
  c("speed_flag", "distracted_flag", "teendriver_flag", "olderdriver_flag", "CYCLFLAG",
    "PEDFLAG", "BIKEFLAG", "singlevehflag", "lanedepflag","deer_flag","intersection_flag", "impaired_flag")

count_crash_flag <-
  function(crash_df, flag, by_variables = c("CRSHSVR")) {
    # crash_df[get(flag) %in% c("Yes", "Y"), .(crash_count = .N, flag_type = flag), by = by_variables]
    crash_df |>
      dplyr::filter(!!rlang::sym(flag) %in% c("Yes" , "Y")) |> #group_by(!!rlang::sym(by_variables)) |>
      dplyr::count() |>
      dplyr::mutate(flag_type = flag)
  }
#' Inverted versions of in, is.null and is.na
#' 
#' @noRd
#' 
#' @examples
#' 1 %not_in% 1:10
#' not_null(NULL)
`%not_in%` <- Negate(`%in%`)

not_null <- Negate(is.null)

not_na <- Negate(is.na)

#' Removes the null from a vector
#' 
#' @noRd
#' 
#' @example 
#' drop_nulls(list(1, NULL, 2))
drop_nulls <- function(x){
  x[!sapply(x, is.null)]
}

#' If x is `NULL`, return y, otherwise return x
#' 
#' @param x,y Two elements to test, one potentially `NULL`
#' 
#' @noRd
#' 
#' @examples
#' NULL %||% 1
"%||%" <- function(x, y){
  if (is.null(x)) {
    y
  } else {
    x
  }
}

#' If x is `NA`, return y, otherwise return x
#' 
#' @param x,y Two elements to test, one potentially `NA`
#' 
#' @noRd
#' 
#' @examples
#' NA %||% 1
"%|NA|%" <- function(x, y){
  if (is.na(x)) {
    y
  } else {
    x
  }
}

#' Typing reactiveValues is too long
#' 
#' @inheritParams reactiveValues
#' @inheritParams reactiveValuesToList
#' 
#' @noRd
rv <- shiny::reactiveValues
rvtl <- shiny::reactiveValuesToList
jacciz/shiny_wisdot_crash_dashboard documentation built on May 4, 2023, 11:36 a.m.