-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
increased integration and use of utility functioning
### Patch to handle empty inputs on Mac OS & Solaris * The LC environmental variables are concatenated using different characters. * Simplifying the checking ### Intro of utilities functions * `ifnull` to do the replacement if a value is null. * `check_type` to do simple error checking based on type (a generalization of the R classes of objects). * `get_locale` as way to do OS-agnostic language and location of a system locale.
- Loading branch information
1 parent
33c2a40
commit 8215ba0
Showing
11 changed files
with
228 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(check_type) | ||
export(gendr) | ||
export(gendr_warning) | ||
export(get_locale) | ||
export(ifnull) | ||
importFrom(stats,setNames) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#' @title Replace a value with an alternative if it is NULL | ||
#' | ||
#' @description Replaces the focal input with the alternative value if it | ||
#' is \code{NULL}. | ||
#' | ||
#' @param x Focal input. | ||
#' | ||
#' @param alt Alternative value. | ||
#' | ||
#' @return \code{x} if not \code{NULL}, \code{alt} otherwise. | ||
#' | ||
#' @examples | ||
#' ifnull(NULL, 123) | ||
#' ifnull(TRUE, 123) | ||
#' ifnull(FALSE, 123) | ||
#' | ||
#' @export | ||
#' | ||
ifnull <- function(x = NULL, alt = NULL){ | ||
if(is.null(x)){ | ||
x <- alt | ||
} | ||
x | ||
} | ||
|
||
#' @title Verify that a value input is of appropriate type | ||
#' | ||
#' @description Throws an error if \code{x} isn't proper, based on the | ||
#' \code{type}. | ||
#' | ||
#' @param x Focal input. | ||
#' | ||
#' @param type Type of input that \code{x} should be. Presently available | ||
#' are \code{"character"} (standard) and \code{"integer"} (conformable to | ||
#' an integer if not explicitly classed as such)/ | ||
#' | ||
#' @return \code{NULL} if \code{x} is proper, throwing an error otherwise. | ||
#' | ||
#' @examples | ||
#' names <- "sam" | ||
#' check_type(x = names, type = "character") | ||
#' names <- 2019 | ||
#' #check_type(x = names, type = "character") # throws error | ||
#' years <- 2019 | ||
#' check_type(x = years, type = "integer") | ||
#' years <- "sam" | ||
#' #check_type(x = years, type = "integer") # throws error | ||
#' | ||
#' @export | ||
#' | ||
check_type <- function(x, type){ | ||
xname <- as.character((match.call())[["x"]]) | ||
testexpr <- switch(type, | ||
"character" = !is.character(x), | ||
"integer" = !is.numeric(x) || any(x %% 1 != 0)) | ||
msg <- switch(type, | ||
"character" = paste0("'`", xname, "` must be characters'"), | ||
"integer" = paste0("'`", xname, "` must be integers'")) | ||
if(testexpr){ | ||
stop(msg, call. = FALSE) | ||
} | ||
invisible(NULL) | ||
} | ||
|
||
#' @title Get the language and location of a system locale | ||
#' | ||
#' @description OS-flexible approach to determining the system locale with | ||
#' respect to language and location. | ||
#' | ||
#' @return \code{character} vector with elements \code{"language"} and | ||
#' \code{"location"}. | ||
#' | ||
#' @examples | ||
#' get_locale() | ||
#' | ||
#' @export | ||
#' | ||
get_locale <- function(){ | ||
ismac <- Sys.info()["sysname"] == "Darwin" | ||
issolaris <- Sys.info()["sysname"] == "SunOS" | ||
splitchar <- ifelse(ismac | issolaris, "/", ";") | ||
locale <- Sys.getlocale() | ||
lc_type <- "LC_TIME" | ||
locale <- strsplit(locale, splitchar)[[1]] | ||
locale <- locale[grep(lc_type, locale)] | ||
locale <- sub(".*=", "", locale) | ||
locale <- strsplit(locale, "_")[[1]] | ||
locale <- setNames(locale, c("language", "location")) | ||
locale[["location"]] <- sub("\\..*", "", locale[["location"]]) | ||
locale | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
context("Test utility functions") | ||
|
||
test_that("ifnull", { | ||
expect_equal(ifnull(NULL, 123), 123) | ||
expect_equal(ifnull(TRUE, 123), TRUE) | ||
}) | ||
|
||
|
||
test_that("check_type", { | ||
names <- "sam" | ||
expect_silent(check_type(x = names, type = "character")) | ||
names <- 2019 | ||
expect_error(check_type(x = names, type = "character")) | ||
years <- 2019 | ||
expect_silent(check_type(x = years, type = "integer")) | ||
years <- "sam" | ||
expect_error(check_type(x = years, type = "integer")) | ||
}) | ||
|
||
test_that("get_locale", { | ||
expect_equal(length(get_locale()), 2) | ||
expect_is(get_locale(), "character") | ||
}) |