R/golem_utils_server.R

Defines functions rvtl rv drop_nulls add_missing_years

#' 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)


# add missing rows for plotting years with 0 frequency
add_missing_years <- function(df) {
  year <- NULL # for devtools::check()

  for (name in unique(df$select_label)){
  df <- df %>%
    dplyr::bind_rows(
                     dplyr::tibble(year = setdiff(1917:2019, df$year),
                                   select_label = name,
                                   freq = 0
                                   ))
  }

  dplyr::arrange(df, year) %>%
    return()
}


#' 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 <- function(...) shiny::reactiveValues(...)
rvtl <- function(...) shiny::reactiveValuesToList(...)
chris31415926535/ontario.baby.names documentation built on April 7, 2022, 12:27 a.m.