| Title: | Dependency-Free Object Tests |
|---|---|
| Description: | Convenience functions for checking class inheritance, extracting attributes, basic type conversion, and miscellaneous string manipulation. working with sf, ggplot2, and other packages. |
| Authors: | Eli Pousson [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8280-1706>) |
| Maintainer: | Eli Pousson <[email protected]> |
| License: | CC0 |
| Version: | 0.1.0.9000 |
| Built: | 2026-05-09 20:41:57 UTC |
| Source: | https://github.com/elipousson/isstatic |
Integers and NA values are passed as is. Double or characters with no corresponding Roman numeral are converting to NA values.
alpha_to_int(x, dict = LETTERS, n = 1, quiet = TRUE, call = parent.frame())alpha_to_int(x, dict = LETTERS, n = 1, quiet = TRUE, call = parent.frame())
x |
Character vector of length n strings to compare to dict. Typically, letters from "A" to "Z". Case sensitive. |
dict |
Character vector to match to x. Default: LETTERS. |
n |
Maximum character length for non-NA objects permitted. Set to NULL or >1 if dict includes objects with more than one character. |
quiet |
If |
call |
Default: |
A length 1 integer between 1 and 26.
Convert a numeric bearing value to the closest cardinal bearing
as_cardinal_bearing(x, winds = 8, cols = c("bearing", "cardinal_bearing"))as_cardinal_bearing(x, winds = 8, cols = c("bearing", "cardinal_bearing"))
x |
A numeric vector with degrees or a data.frame with column name matching the first name in cols. |
winds |
Number of winds to use for results (4, 8, or 16). |
cols |
A length 2 character vector where the first value is a column name containing bearing values and the second is the name of the new column added to the data.frame. Required if x is a data.frame. |
A named numeric vector with cardinal bearings (and wind names) or a data.frame with an added column containing the cardinal bearings.
This function should be updated to support stars and terra objects.
as_crs(x, input = TRUE) is_lonlat_crs(x, crs = c("EPSG:4326", "EPSG:4269"))as_crs(x, input = TRUE) is_lonlat_crs(x, crs = c("EPSG:4326", "EPSG:4269"))
x |
A sf, sfc, or bbox object to coerce into a CRS. |
input |
If |
crs |
For is_lonlat_crs, coordinate reference system to use as lonlat crs. |
as.integer with option to suppress warnings for NA coercion
as_integer(x, quiet = TRUE)as_integer(x, quiet = TRUE)
x |
object to be coerced or tested. |
quiet |
If |
This function allows the creation of numbered labels for a vector using a range of numbering styles.
as_numbered_labels( x, labels = "arabic", start = NULL, suffix = NULL, base = 26, cols = "num_label", pad = NULL, side = "left", quiet = TRUE, call = parent.frame() )as_numbered_labels( x, labels = "arabic", start = NULL, suffix = NULL, base = 26, cols = "num_label", pad = NULL, side = "left", quiet = TRUE, call = parent.frame() )
x |
An integer or other vector or a data.frame. An integer vector or
integer column is used as the number that is converted based on the label
style. If x is not an integer or data.frame with an integer column, the
numbering is created based on |
labels |
Label style. Options include "arabic", "alph", "Alph", "alpha", "Alpha", "roman", or "Roman". |
start |
Starting number or value. Letters are supported if label style is "alph", "Alph", "alpha", or "Alpha" and Roman numerals are supported if label is "roman" or "Roman". |
suffix |
Suffix character to follow number labels. For example, if |
base |
Base used in alphabetical number labels. Highest letter to use for alphabetical numbers. Single digit letters (A to Z) or numbers 1 to 26 are supported. For example, if base is 3, alphabetical labels for numbers higher than 3 have the prior value prefixed so 3 would be "C" and 4 would be "AA". Defaults to 26 which converts 27 to "AA", 53 to "BA", etc. |
cols |
Column name to use for added column for number labels when x is a data.frame. Defaults to "num_label". If cols is length 2, the first item in the vector is assumed to be the column name from the data.frame to use as x and the second item is used as the column name for the added column with number labels. |
pad, side
|
If pad is not |
quiet |
If |
call |
Default: |
If x is a vector, function returns numeric vector if labels is "arabic" or a character vector otherwise.
If x is a data.frame, as_numbered_labels() returns a modified data.frame
with an added column with a name matching the second value of the cols
parameter.
What is the orientation of a numeric aspect ratio?
as_orientation(x, tolerance = 0.1, cols = c("width", "height"))as_orientation(x, tolerance = 0.1, cols = c("width", "height"))
x |
A numeric vector with an aspect ratio or a data.frame with width and height column (using width and height values from columns matching the cols parameter). |
tolerance |
Positive numeric value above or below 1 used to determine if an aspect ratio is square, landscape, or portrait. |
cols |
Name of width and height column if x is a data.frame object. |
A character vector of orientations of the same length as x or, if x is a data.frame, the same length as the number of rows in x.
as.roman with option to suppress warnings for NA coercion
as_roman(x, quiet = TRUE)as_roman(x, quiet = TRUE)
x |
a numeric or character vector of arabic or roman numerals. |
quiet |
If |
If possible, function should be updated to support bbox objects or other spatial data classes.
as_sfc(x)as_sfc(x)
x |
A sf object to coerce. |
This is a wrapper function of xfun::join_words().
combine_words( words, sep = ", ", and = " and ", before = "", after = before, oxford_comma = TRUE )combine_words( words, sep = ", ", and = " and ", before = "", after = before, oxford_comma = TRUE )
A character string
Yihui Xie [email protected] (ORCID)
Adapted from knitr::combine_words() in the
knitr package.
Helper to return a regex based on side
digit_pattern(pattern = "[0-9]+", side = NULL)digit_pattern(pattern = "[0-9]+", side = NULL)
A replacement for file.path()
file_path( ..., path = NULL, filename = NULL, fsep = .Platform$file.sep, allow_null = FALSE, call = parent.frame() )file_path( ..., path = NULL, filename = NULL, fsep = .Platform$file.sep, allow_null = FALSE, call = parent.frame() )
... |
Additional strings to pass before path and filename. |
path |
Path name. Optional if filename is supplied. |
filename |
File name. Optional if path is supplied. |
fsep |
the path separator to use (assumed to be ASCII). |
allow_null |
If |
Check if string contains any filetype or the provided filetype. If string is
NULL, returns FALSE.
has_fileext(string = NULL, fileext = NULL, ignore.case = FALSE)has_fileext(string = NULL, fileext = NULL, ignore.case = FALSE)
string |
String to be tested with or without filetype. Defaults to
|
fileext |
File type to test against. Optional. |
ignore.case |
If |
Alternate naming convention for has_fileext()
has_filetype(string = NULL, filetype = NULL, ignore.case = FALSE)has_filetype(string = NULL, filetype = NULL, ignore.case = FALSE)
string |
String to be tested with or without filetype. Defaults to
|
filetype |
File type to test against. Optional. |
ignore.case |
If |
Is the length of x between two values?
has_len_between(x, left = 1, right = left) has_min_length(x, min) has_max_length(x, max)has_len_between(x, left = 1, right = left) has_min_length(x, min) has_max_length(x, max)
x |
Object to check. |
left, right
|
Min and max values to check if the length of x is between. |
min |
Min value used by |
max |
Max value used by |
has_same_crs(): Do two sf, sfc, or bbox objects use the same coordinate
reference system?
has_same_crs(): Do two sf, sfc, or bbox objects use the same coordinate
reference system?
has_same_crs(x, y, ...)has_same_crs(x, y, ...)
x, y
|
sf, sfc, or bbox objects to be compared. |
... |
Additional parameters passed to |
Do two object have an identical length?
Do two object have an identical length?
has_same_len(x, y, ...) has_same_len(x, y, ...)has_same_len(x, y, ...) has_same_len(x, y, ...)
x, y
|
Two strings or character vectors to compare. |
... |
Additional parameters passed to |
Character values in the provided dict (default to letters "A" to "Z") are passed as is. Non-integer numeric values or characters that are not found in the provided dict are converting to NA values.
int_to_alpha(x, suffix = NULL, base = 26, dict = LETTERS, quiet = TRUE)int_to_alpha(x, suffix = NULL, base = 26, dict = LETTERS, quiet = TRUE)
x |
An integer vector or a vector that can be coerced to an integer vector |
suffix |
Suffix character to follow alpha character, e.g. if |
base |
If base is not numeric, it is converted to an integer with
|
dict |
Character vector to compare to x. Default: LETTERS. |
quiet |
If |
An integer vector composed of objects between 1 and 26 with the same length as x.
Adapted from the recursive solution provided by G. Grothendieck in a May 31, 2017 StackOverflow answer.
Do all items in a list or vector return TRUE from a predicate function?
is_all_null(): Are all items in a list or vector NULL values?
is_all_na(): Are all items in a list or vector NA values?
is_all(x, FUN, ...) is_all_null(x) is_all_na(x)is_all(x, FUN, ...) is_all_null(x) is_all_na(x)
x |
A list or vector passed to X parameter of |
FUN |
the function to be applied to each element of |
... |
Arguments passed on to
|
TRUE if FUN returns TRUE for all elements of x or FALSE if any
element returns FALSE.
Is all of x in y?
is_all_in(x, y)is_all_in(x, y)
x |
Object to be tested. |
y |
Vector to compare x to. |
TRUE from a predicate function?Do any items in a list or vector return TRUE from a predicate function?
is_any_null(): Is any item in a list or vector a NULL value?
is_any_na(): Is any item in a list or vector a NA value?
is_none(): Is no item in a list or vector return TRUE from a predicate function?
is_none_null(): Is no item in a list or vector is NULL?
Do any items in a list or vector return TRUE from a predicate function?
is_any_null(): Is any item in a list or vector a NULL value?
is_any_na(): Is any item in a list or vector a NA value?
is_any(x, FUN, ...) is_any_null(x) is_any_na(x) is_none(x, FUN, ...) is_none_null(x) is_any(x, FUN, ...) is_any_null(x) is_any_na(x)is_any(x, FUN, ...) is_any_null(x) is_any_na(x) is_none(x, FUN, ...) is_none_null(x) is_any(x, FUN, ...) is_any_null(x) is_any_na(x)
x |
A list or vector passed to |
FUN |
the function to be applied to each element of |
... |
Arguments passed on to
|
TRUE if FUN returns TRUE for any element of x or FALSE if all
elements return FALSE.
TRUE if FUN returns TRUE for any element of x or FALSE if all
elements return FALSE.
Are any of x in y?
is_any_in(x, y)is_any_in(x, y)
x |
Object to be tested. |
y |
Vector to compare x to. |
Return a logical vector indicating if elements of a character vector are blank (white spaces or empty strings).
is_blank(x)is_blank(x)
x |
A character vector. |
TRUE for blank elements, or FALSE otherwise.
Yihui Xie [email protected] (ORCID)
Adapted from xfun::is_blank() in the
xfun package.
is_blank("") is_blank("abc") is_blank(c("", " ", "\n\t")) is_blank(c("", " ", "abc"))is_blank("") is_blank("abc") is_blank(c("", " ", "\n\t")) is_blank(c("", " ", "abc"))
is_file() is a wrapper for base::file.exists() that allows the exclusion
of directories and returning named vectors. is_dir() is a wrapper for
base::dir.exists() that supports vector inputs rather than single strings.
character(0) inputs return FALSE.
is_file(x, include_dirs = FALSE, use_names = FALSE) is_dir(x, use_names = FALSE)is_file(x, include_dirs = FALSE, use_names = FALSE) is_dir(x, use_names = FALSE)
include_dirs |
If |
use_names |
If |
Does this text end in the provided file extension?
is_geojson_fileext: Does this text end with a GeoJSON file extension?
is_csv_fileext: Does this text end with a CSV file extension?
is_excel_fileext: Does this text end with a XLS or XLSX file extension?
is_rdata_fileext: Does this text end with a rds, rda, or RData file extension?
is_rds_fileext: Does this text end with a rds file extension?
is_rda_fileext: Does this text end with a rda file extension?
is_zip_fileext: Does this text end with a zip file extension?
is_fileext_path(x, fileext, ignore.case = TRUE) is_geojson_fileext(x, ignore.case = TRUE) is_csv_fileext(x, ignore.case = TRUE) is_excel_fileext(x, ignore.case = TRUE) is_rdata_fileext(x, ignore.case = TRUE) is_rds_fileext(x, ignore.case = TRUE) is_rda_fileext(x, ignore.case = TRUE) is_zip_fileext(x, ignore.case = TRUE)is_fileext_path(x, fileext, ignore.case = TRUE) is_geojson_fileext(x, ignore.case = TRUE) is_csv_fileext(x, ignore.case = TRUE) is_excel_fileext(x, ignore.case = TRUE) is_rdata_fileext(x, ignore.case = TRUE) is_rds_fileext(x, ignore.case = TRUE) is_rda_fileext(x, ignore.case = TRUE) is_zip_fileext(x, ignore.case = TRUE)
x |
A character vector to check for matches, or an object which can be
coerced by |
fileext |
A file extension to compare to x. Required. If a vector of
multiple extensions are provided, returns |
ignore.case |
logical. if |
Does this text end in the specified filetype?
is_filetype_path(x, filetype, ignore.case = TRUE)is_filetype_path(x, filetype, ignore.case = TRUE)
x |
A character vector to check for matches, or an object which can be
coerced by |
filetype |
A file extension (or multiple file extensions) to compare to x. Required. |
ignore.case |
logical. if |
Is this a gg class object?
Is this a ggplot class object?
Is this a ggproto class object?
Is this a patchwork class object?
is_gg(x) is_ggplot(x) is_ggproto(x) is_patchwork(x)is_gg(x) is_ggplot(x) is_ggproto(x) is_patchwork(x)
x |
Object to be tested. |
Do all items in this list inherit the gg class?
is_gg_list(x)is_gg_list(x)
x |
Object to be tested. |
Do all items in this list inherit the provided class?
is_list_all(x, what = NULL)is_list_all(x, what = NULL)
x |
Object to be tested. |
what |
A character vector naming classes. |
Is this object a named list or character vector?
Does this object have all of the provided names?
Does this object have any of the provided names?
is_named(x) has_all_names(x, name) has_any_names(x, name)is_named(x) has_all_names(x, name) has_any_names(x, name)
x |
A data frame or another named object. |
name |
Element name(s) to check. |
Are none of x in y?
is_none_in(x, y)is_none_in(x, y)
x |
Object to be tested. |
y |
Vector to compare x to. |
Is this a sf class object?
Is this a sfc class object?
Is this a sfg class object?
Is this a bbox class object?
Is this a sf, sfc, or bbox class object?
Is this a RasterLayer class object?
Is this a Spatial class (sp) object?
is_sf(x) is_sfc(x) is_sfg(x) is_bbox(x) is_sf_ext(x, ext = TRUE) is_raster(x) is_sp(x)is_sf(x) is_sfc(x) is_sfg(x) is_bbox(x) is_sf_ext(x, ext = TRUE) is_raster(x) is_sp(x)
x |
An object to be tested with |
ext |
If |
Do all items in this list inherit the sf class?
is_sf_list(x)is_sf_list(x)
x |
Object to be tested. |
Is this a unit class object?
Is this a margin class object?
is_unit(x) is_margin(x)is_unit(x) is_margin(x)
x |
Object to be tested. |
Is this a units class object?
is_units(x)is_units(x)
x |
Object to be tested. |
Is an object a URL?
is_esri_url(): Is an object an ArcGIS MapServer or FeatureServer URL?
is_gsheet_url(): Is an object a Google Sheets URL?
is_gist_url(): Is an object a URL for a GitHub Gist?
is_gmap_url(): Is an object a Google Maps URL?
is_url(x) is_esri_url(x) is_gsheet_url(x) is_gist_url(x) is_gmap_url(x)is_url(x) is_esri_url(x) is_gsheet_url(x) is_gist_url(x) is_gmap_url(x)
x |
A object to be tested. |
Apply a function to each element of a vector.
map_chr(.x, .f, ...)map_chr(.x, .f, ...)
Winston Chang [email protected]
purr-like functions in staticimports package
Simple helper for pluralizing words
plural_words( words, n = 1, suffix = "s", before = "", after = "", replacement = NULL )plural_words( words, n = 1, suffix = "s", before = "", after = "", replacement = NULL )
Integers and NA objects are passed as is. Double numeric objects or characters with no corresponding Roman numeral are converting to NA values.
roman_to_int(x, quiet = TRUE)roman_to_int(x, quiet = TRUE)
x |
An integer vector or a character vector with characters representing Roman numerals. |
quiet |
If |
Helper for as_numbered_labels().
set_start_number(x, start = NULL, labels = "arabic")set_start_number(x, start = NULL, labels = "arabic")
x |
An integer or other vector or a data.frame. An integer vector or
integer column is used as the number that is converted based on the label
style. If x is not an integer or data.frame with an integer column, the
numbering is created based on |
start |
Starting number or value. Letters are supported if label style is "alph", "Alph", "alpha", or "Alpha" and Roman numerals are supported if label is "roman" or "Roman". |
labels |
Label style. Options include "arabic", "alph", "Alph", "alpha", "Alpha", "roman", or "Roman". |
Dependency-free drop-in alternative for stringr::str_c().
str_c(..., sep = "", collapse = NULL)str_c(..., sep = "", collapse = NULL)
... |
One or more character vectors. Zero length arguments are removed. Short arguments are recycled to the length of the longest. Like most other R functions, missing values are "infectious":
whenever a missing value is combined with another string
the result will always be missing.
Use |
sep |
String to insert between input vectors. |
collapse |
Optional string used to combine input vectors into single string. |
If collapse = NULL (the default) a character vector
with length equal to the longest input string.
If collapse is non-NULL, a character vector of length 1.
Adapted from the stringr package.
Dependency-free drop-in alternative for stringr::str_detect().
str_detect(string, pattern, negate = FALSE)str_detect(string, pattern, negate = FALSE)
string |
Input vector. Either a character vector, or something coercible to one. |
pattern |
Pattern to look for. The default interpretation is a regular expression,
as described in base::regex.
Control options with Match a fixed string (i.e. by comparing only bytes), using |
negate |
If |
A logical vector.
Adapted from the stringr package.
Dependency-free drop-in alternative for stringr::str_extract().
str_extract(string, pattern)str_extract(string, pattern)
string |
Input vector. Either a character vector, or something coercible to one. |
pattern |
Pattern to look for. The default interpretation is a regular expression,
as described in base::regex.
Control options with Match a fixed string (i.e. by comparing only bytes), using |
A character matrix. The first column is the complete match, followed by one column for each capture group.
Adapted from the stringr package.
These function uses stringstatic::str_c(), stringstatic::str_remove() and
stringstatic::str_extract() and works to:
Add file extensions (or replace existing file extensions) with
str_add_fileext()
Remove file extensions with str_remove_fileext()
Extract existing file names str_extract_fileext() (returning NA values if
a string has no file extension)
str_add_fileext(string, fileext = NULL) str_remove_fileext(string, fileext = NULL) str_extract_fileext(string, fileext = NULL)str_add_fileext(string, fileext = NULL) str_remove_fileext(string, fileext = NULL) str_extract_fileext(string, fileext = NULL)
string |
Character vector of any length. Required. |
fileext |
File extension. Optional. Defaults to |
str_add_fileext("image", "jpeg") str_remove_fileext(c("file.txt", "word.docx"), "docx") str_extract_fileext(c("file1.pdf", "file2")) str_extract_fileext(c("image1.png", "image2.jpeg"), "jpeg")str_add_fileext("image", "jpeg") str_remove_fileext(c("file.txt", "word.docx"), "docx") str_extract_fileext(c("file1.pdf", "file2")) str_extract_fileext(c("image1.png", "image2.jpeg"), "jpeg")
Dependency-free drop-in alternative for stringr::str_length().
str_length(string)str_length(string)
string |
Input vector. Either a character vector, or something coercible to one. |
A numeric vector the same length as string.
Adapted from the stringr package.
This function does not address ties in frequency.
str_n_freq(string = NULL, n = NULL, decreasing = TRUE)str_n_freq(string = NULL, n = NULL, decreasing = TRUE)
string |
A character vector. |
n |
The number of values to return based on frequency of appearance.
Defaults to |
decreasing |
Passed to |
Dependency-free drop-in alternative for stringr::str_pad().
str_pad( string, width, side = c("left", "right", "both"), pad = " ", use_width = TRUE )str_pad( string, width, side = c("left", "right", "both"), pad = " ", use_width = TRUE )
string |
Input vector. Either a character vector, or something coercible to one. |
width |
Minimum width of padded strings. |
side |
Side on which padding character is added (left, right or both). |
pad |
Single padding character (default is a space). |
use_width |
If |
A character vector.
Eli Pousson [email protected] (ORCID)
Alexander Rossell Hayes [email protected] (ORCID)
Adapted from the stringr package.
str_replace_digits(): Replace digits with a string optionally
incrementing the digits
str_pad_digits(): Pad a string with digits
str_extract_digits(): Extract digits from a string
str_pad_digits(string, pad = "0", side = "left", width = NULL) str_extract_digits(string, pattern = "[0-9]+", side = NULL) str_replace_digits(string, replacement, pad = "0", side = "left", width = NULL) str_increment_digits(string, increment = TRUE, ...)str_pad_digits(string, pad = "0", side = "left", width = NULL) str_extract_digits(string, pattern = "[0-9]+", side = NULL) str_replace_digits(string, replacement, pad = "0", side = "left", width = NULL) str_increment_digits(string, increment = TRUE, ...)
string |
Input vector. Either a character vector, or something coercible to one. |
pad |
Single padding character added to digits in string; defaults to "0" |
side |
Side on which padding character is added (left, right or both). |
width |
Minimum width of padded strings. |
pattern |
Pattern to look for. The default interpretation is a regular expression,
as described in base::regex.
Control options with Match a fixed string (i.e. by comparing only bytes), using |
replacement |
A character vector of replacements.
Should be either length one, or the same length as To replace the complete string with Using a function for |
increment |
If |
... |
Passed to |
Dependency-free drop-in alternative for stringr::str_remove().
str_remove(string, pattern)str_remove(string, pattern)
string |
Input vector. Either a character vector, or something coercible to one. |
pattern |
Pattern to look for. The default interpretation is a regular expression,
as described in base::regex.
Control options with Match a fixed string (i.e. by comparing only bytes), using |
A character vector.
Adapted from the stringr package.
Dependency-free drop-in alternative for stringr::str_replace().
str_replace(string, pattern, replacement)str_replace(string, pattern, replacement)
string |
Input vector. Either a character vector, or something coercible to one. |
pattern |
Pattern to look for. The default interpretation is a regular expression,
as described in base::regex.
Control options with Match a fixed string (i.e. by comparing only bytes), using |
replacement |
A character vector of replacements.
Should be either length one, or the same length as To replace the complete string with Using a function for |
A character vector.
Adapted from the stringr package.
Dependency-free drop-in alternative for stringr::str_replace_all().
str_replace_all(string, pattern, replacement)str_replace_all(string, pattern, replacement)
string |
Input vector. Either a character vector, or something coercible to one. |
pattern |
Pattern to look for. The default interpretation is a regular expression,
as described in base::regex.
Control options with Match a fixed string (i.e. by comparing only bytes), using |
replacement |
A character vector of replacements.
Should be either length one, or the same length as To perform multiple replacements in each element of To replace the complete string with Using a function for |
A character vector.
Adapted from the stringr package.
Dependency-free drop-in alternative for stringr::str_width().
Results for non-ASCII characters may be inaccurate in R < 4.0.
str_width(string)str_width(string)
string |
Input vector. Either a character vector, or something coercible to one. |
A numeric vector the same length as string.
Adapted from the stringr package.
Convert to a common sentence case
tosentence(x)tosentence(x)
x |
a character vector, or an object that can be coerced to character by
|
Joachim Schork [email protected]
Replace spaces in string with underscores
underscore(string)underscore(string)
string |
String to transform by replacing spaces with underscores. |