Title: | Miscellaneous, Analytic R Kernels |
---|---|
Description: | Miscellaneous functions and wrappers for development in other packages created, maintained by Jordan Mark Barbone. |
Authors: | Jordan Mark Barbone [aut, cph, cre] |
Maintainer: | Jordan Mark Barbone <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.8.2 |
Built: | 2024-12-07 20:32:41 UTC |
Source: | CRAN |
Adds a timestamp to a file
add_file_timestamp( x, ts = Sys.time(), format = "%Y-%m-%d %H%M%S", sep = " " )
add_file_timestamp( x, ts = Sys.time(), format = "%Y-%m-%d %H%M%S", sep = " " )
x |
A vector of files |
ts |
A single timestamp or vector of timestamps (default: |
format |
A format to be applied to the times; set to |
sep |
A |
The full name paths with the appended time stamp
file1 <- tempfile(fileext = ".txt") file2 <- tempfile() add_file_timestamp(file1) add_file_timestamp(file2) file.remove(file1, file2)
file1 <- tempfile(fileext = ".txt") file2 <- tempfile() add_file_timestamp(file1) add_file_timestamp(file2) file.remove(file1, file2)
Extensions for the use of base::identical()
are_identical(..., params = NULL)
are_identical(..., params = NULL)
... |
Vectors of values to compare, element-wise of equal length |
params |
Additional params (as a named list of arguments for base::identical) |
A logical
vector of TRUE
/FALSE
of equal length of each ...
vector
x <- y <- z <- 1:5 y[2] <- 3L z[5] <- NA_integer_ identical(x, y) # compare entire vector are_identical(x, y) # element-wise are_identical(x, y, z) # 3 or more vectors
x <- y <- z <- 1:5 y[2] <- 3L z[5] <- NA_integer_ identical(x, y) # compare entire vector are_identical(x, y) # element-wise are_identical(x, y, z) # 3 or more vectors
Extract dimensions from an array
array_extract(.arr, ..., default = "1")
array_extract(.arr, ..., default = "1")
.arr |
An array |
... |
A named list by array dimension number and the value |
default |
The default dimension index |
A value from the array arr
x <- array(rep(NA, 27), dim = c(3, 3, 3)) x[1, 2, 3] <- TRUE x[1, 2, 3] x array_extract(x, `2` = 2, `3` = 3)
x <- array(rep(NA, 27), dim = c(3, 3, 3)) x[1, 2, 3] <- TRUE x[1, 2, 3] x array_extract(x, `2` = 2, `3` = 3)
As ordered
as_ordered(x) ## Default S3 method: as_ordered(x)
as_ordered(x) ## Default S3 method: as_ordered(x)
x |
A vector of values |
Simple implementation of ordered
. If x
is ordered
it is
simply returned. If x
is a factor
the ordered
class is added.
Otherwise, x
is made into a factor
with fact()
and then the
ordered
class is added. Unlike just fact
, ordered
will replace the NA
levels with NA_integer_
to work appropriately with other functions.
An ordered
vector
Other factors:
char2fact()
,
drop_levels()
,
fact()
,
fact2char()
,
fact_na()
x <- c("a", NA, "b") x <- fact(x) str(x) # NA is 3L y <- x class(y) <- c("ordered", class(y)) max(y) max(y, na.rm = TRUE) # returns NA -- bad # as_ordered() removes the NA level x <- as_ordered(x) str(x) max(x, na.rm = TRUE) # returns b -- correct
x <- c("a", NA, "b") x <- fact(x) str(x) # NA is 3L y <- x class(y) <- c("ordered", class(y)) max(y) max(y, na.rm = TRUE) # returns NA -- bad # as_ordered() removes the NA level x <- as_ordered(x) str(x) max(x, na.rm = TRUE) # returns b -- correct
Base 26 conversion with letters
base_alpha(x, base = 26)
base_alpha(x, base = 26)
x |
A string of letters. Non characters are removed. |
base |
A numeric |
A vector of integers
base_alpha("AB") base_alpha("XFD") base_alpha(c("JMB", "Jordan Mark", "XKCD")) sum(base_alpha(c("x", "k", "c", "d")))
base_alpha("AB") base_alpha("XFD") base_alpha(c("JMB", "Jordan Mark", "XKCD")) sum(base_alpha(c("x", "k", "c", "d")))
Convert between base numbers
base_n(x, from = 10, to = 10)
base_n(x, from = 10, to = 10)
x |
A vector of integers |
from , to
|
An integer base to convert to and from; |
The A vector of integers converted from base from
to base to
base_n(c(24, 22, 16), from = 7)
base_n(c(24, 22, 16), from = 7)
Detect blank values; select, remove columns that are entirely blank
is_blank(x, na_blank = FALSE, ws = TRUE) select_blank_cols(x, na_blank = FALSE, ws = TRUE) remove_blank_cols(x, na_blank = FALSE, ws = TRUE) is_blank_cols(x, names = TRUE, na_blank = FALSE, ws = TRUE)
is_blank(x, na_blank = FALSE, ws = TRUE) select_blank_cols(x, na_blank = FALSE, ws = TRUE) remove_blank_cols(x, na_blank = FALSE, ws = TRUE) is_blank_cols(x, names = TRUE, na_blank = FALSE, ws = TRUE)
x |
An object, or |
na_blank |
Logical, if |
ws |
Logical, when |
names |
Logical, if |
Blank values are values that do not contain any text
is_blank()
a logical
vector indicating blank elements in x
select_blank_cols()
x
with only columns that are all blank
remove_blank_cols()
x
without columns of only blank
is_blank_cols()
a logical vector: TRUE
all rows of column are blank,
otherwise FALSE
Converts characters to factors
char2fact(x, n = 5) ## Default S3 method: char2fact(x, n = 5) ## S3 method for class 'character' char2fact(x, n = 5) ## S3 method for class 'factor' char2fact(x, n = 5) ## S3 method for class 'data.frame' char2fact(x, n = 5)
char2fact(x, n = 5) ## Default S3 method: char2fact(x, n = 5) ## S3 method for class 'character' char2fact(x, n = 5) ## S3 method for class 'factor' char2fact(x, n = 5) ## S3 method for class 'data.frame' char2fact(x, n = 5)
x |
A vector of characters |
n |
The limit to the number of unique values for the factor |
Other factors:
as_ordered()
,
drop_levels()
,
fact()
,
fact2char()
,
fact_na()
For each name in x
checks the current option value and reports if there
is a difference in a message
. This does not change the options
checkOptions(x)
checkOptions(x)
x |
A named list of new options |
Checks and reports on options
Invisible, a list of the current options from options()
op <- options() x <- list(width = -20, warning.length = 2, probably_not_a_real_option = 2) checkOptions(x) # pointless, but shows that no messages are given identical(options(), checkOptions(options())) options(op)
op <- options() x <- list(width = -20, warning.length = 2, probably_not_a_real_option = 2) checkOptions(x) # pointless, but shows that no messages are given identical(options(), checkOptions(options())) options(op)
Split apart a string by each character
chr_split(x)
chr_split(x)
x |
A vector of strings to split |
A character
vector of length nchar(x)
chr_split("split this")
chr_split("split this")
Wrappers for working with the clipboard
write_clipboard(x, ...) ## Default S3 method: write_clipboard(x, ...) ## S3 method for class 'data.frame' write_clipboard(x, sep = "\t", row.names = FALSE, ...) ## S3 method for class 'matrix' write_clipboard(x, sep = "\t", ...) ## S3 method for class 'list' write_clipboard(x, sep = "\t", ...) read_clipboard(method = read_clipboard_methods(), ...) read_clipboard_methods()
write_clipboard(x, ...) ## Default S3 method: write_clipboard(x, ...) ## S3 method for class 'data.frame' write_clipboard(x, sep = "\t", row.names = FALSE, ...) ## S3 method for class 'matrix' write_clipboard(x, sep = "\t", ...) ## S3 method for class 'list' write_clipboard(x, sep = "\t", ...) read_clipboard(method = read_clipboard_methods(), ...) read_clipboard_methods()
x |
An object |
... |
Additional arguments sent to methods or to |
sep |
the field separator string. Values within each row of
|
row.names |
either a logical value indicating whether the row
names of |
method |
Method switch for loading the clipboard |
As these functions rely on clipr::read_clip()
and
utils::writeClipboard()
they are only available for Windows 10. For copying
and pasting floats, there may be some rounding that can occur.
write_clipboard()
None, called for side effects read_clipboard()
Either a vector, data.frame
, or tibble
depending on the method
chosen.
Unlike utils::readClipboard()
, an empty clipboard value returns NA
rather
than ""
# Will only run on windows if (Sys.info()[["sysname"]] == "Windows") { foo <- function(x) { write_clipboard(x) y <- read_clipboard() res <- all.equal(x, y) if (isTRUE(res)) return("All equal") print(x) print(y) } foo(1:4) foo(seq(-1, 1, .02)) foo(Sys.Date() + 1:4) # May have some rounding issues x <- "0.316362437326461129" write_clipboard(x) res <- as.character(read_clipboard()) all.equal(x, res) x; res }
# Will only run on windows if (Sys.info()[["sysname"]] == "Windows") { foo <- function(x) { write_clipboard(x) y <- read_clipboard() res <- all.equal(x, y) if (isTRUE(res)) return("All equal") print(x) print(y) } foo(1:4) foo(seq(-1, 1, .02)) foo(Sys.Date() + 1:4) # May have some rounding issues x <- "0.316362437326461129" write_clipboard(x) res <- as.character(read_clipboard()) all.equal(x, res) x; res }
Return completed cases of a data.frame
complete_cases(data, cols = NULL, invert = FALSE)
complete_cases(data, cols = NULL, invert = FALSE)
data |
A data.frame |
cols |
Colnames or numbers to remove |
invert |
Logical, if |
A data.frame
x <- data.frame( a = 1:5, b = c(1, NA, 3, 4, 5), c = c(1, NA, NA, 4, 5) ) complete_cases(x) complete_cases(x, invert = TRUE) # returns the incomplete rows complete_cases(x, "a") complete_cases(x, "b") complete_cases(x, "c")
x <- data.frame( a = 1:5, b = c(1, NA, 3, 4, 5), c = c(1, NA, NA, 4, 5) ) complete_cases(x) complete_cases(x, invert = TRUE) # returns the incomplete rows complete_cases(x, "a") complete_cases(x, "b") complete_cases(x, "c")
Variables will be return by the order in which they appear. Even factors are shown by their order of appearance in the vector.
There are 2 methods for counting vectors. The default
method uses
base::tabulate()
(the workhorse for base::table()
with a call to
pseudo_id()
to transform all inputs into integers. The logical
method
counts TRUE
, FALSE
and NA
values, which is much quicker.
counts(x, ...) ## S3 method for class 'data.frame' counts(x, cols, sort = FALSE, ..., .name = "freq") props(x, ...) ## Default S3 method: props(x, sort = FALSE, na.rm = FALSE, ...) ## S3 method for class 'data.frame' props(x, cols, sort = FALSE, na.rm = FALSE, ..., .name = "prop")
counts(x, ...) ## S3 method for class 'data.frame' counts(x, cols, sort = FALSE, ..., .name = "freq") props(x, ...) ## Default S3 method: props(x, sort = FALSE, na.rm = FALSE, ...) ## S3 method for class 'data.frame' props(x, cols, sort = FALSE, na.rm = FALSE, ..., .name = "prop")
x |
A vector or |
... |
Arguments passed to other methods |
cols |
A vector of column names or indexes |
sort |
Logical, if |
.name |
The name of the new column |
na.rm |
If |
Get counts or proportions of unique observations in a vector or columns in a
data.frame
A named vector of integer
s or double
s (for counts
, and props
,
respectively) or data.frame
with columns for each column chosen and the
.name
chosen for the summary
x <- sample(1:5, 10, TRUE) counts(x) props(x) x <- quick_df(list( a = c("a", "c", "a", "c", "d", "b"), b = c("a", "a", "a", "c", "c", "b"), c = c("a", "a", "a", "c", "b", "b") )) counts(x, "a") counts(x, c("a", "b", "c")) props(x, 2) props(x, 1:3) props(c(1, 1, 3, NA, 4)) props(c(1, 1, 3, NA, 4), na.rm = TRUE)
x <- sample(1:5, 10, TRUE) counts(x) props(x) x <- quick_df(list( a = c("a", "c", "a", "c", "d", "b"), b = c("a", "a", "a", "c", "c", "b"), c = c("a", "a", "a", "c", "b", "b") )) counts(x, "a") counts(x, c("a", "b", "c")) props(x, 2) props(x, 1:3) props(c(1, 1, 3, NA, 4)) props(c(1, 1, 3, NA, 4), na.rm = TRUE)
Derive a date vector from a partial date string
date_from_partial( x, format = "ymd", method = c("min", "max"), year_replacement = NA_integer_ )
date_from_partial( x, format = "ymd", method = c("min", "max"), year_replacement = NA_integer_ )
x |
A vector of dates written as characters |
format |
Format order of the date (accepts only combinations of |
method |
Method for reporting partial dates as either the earliest
possible date ( |
year_replacement |
(Default: |
Takes a character as an argument and attempts to create a date object when part of the date string is missing.
A vector of Dates
x <- c("2020-12-17", NA_character_, "", "2020-12-UN", "2020-12-UN", "2019-Unknown-00", "UNK-UNK-UNK", "1991-02-UN", " ", "2020January20") data.frame( x = x, min = date_from_partial(x), max = date_from_partial(x, method = "max"), year = date_from_partial(x, year_replacement = 1900) )
x <- c("2020-12-17", NA_character_, "", "2020-12-UN", "2020-12-UN", "2019-Unknown-00", "UNK-UNK-UNK", "1991-02-UN", " ", "2020January20") data.frame( x = x, min = date_from_partial(x), max = date_from_partial(x, method = "max"), year = date_from_partial(x, year_replacement = 1900) )
Functions to extract the 'depth' of an object
depth(x, ...) ## Default S3 method: depth(x, ...) ## S3 method for class 'list' depth(x, ...)
depth(x, ...) ## Default S3 method: depth(x, ...) ## S3 method for class 'list' depth(x, ...)
x |
An object |
... |
Possible additional arguments passed to methods (not in use) |
This function does not count an empty lists (list()
) as a level or NULL
objects.
A single integer
a <- c(1, 2, 3) depth(a) # Vectors are 1L b <- list(a = 1, b = list(list(1))) depth(b)
a <- c(1, 2, 3) depth(a) # Vectors are 1L b <- list(a = 1, b = list(list(1))) depth(b)
Provides details about an object
detail(x, ...) ## Default S3 method: detail(x, factor_n = 5L, ...) ## S3 method for class 'data.frame' detail(x, factor_n = 5L, ...)
detail(x, ...) ## Default S3 method: detail(x, factor_n = 5L, ...) ## S3 method for class 'data.frame' detail(x, factor_n = 5L, ...)
x |
An object |
... |
Additional arguments passed to methods |
factor_n |
An |
x <- sample(letters[1:4], 10, TRUE) detail(x) df <- quick_df(list( x = x, y = round(runif(10), 2), z = Sys.Date() + runif(10) * 100 )) detail(df)
x <- sample(letters[1:4], 10, TRUE) detail(x) df <- quick_df(list( x = x, y = round(runif(10), 2), z = Sys.Date() + runif(10) * 100 )) detail(df)
Wrappers for computing diff times
diff_time( x, y, method = c("secs", "mins", "hours", "days", "weeks", "months", "years", "dyears", "wyears", "myears"), tzx = NULL, tzy = tzx ) diff_time_days(x, y, ...) diff_time_weeks(x, y, ...) diff_time_hours(x, y, ...) diff_time_mins(x, y, ...) diff_time_secs(x, y, ...) diff_time_months(x, y, ...) diff_time_years(x, y, ...) diff_time_dyears(x, y, ...) diff_time_wyears(x, y, ...) diff_time_myears(x, y, ...)
diff_time( x, y, method = c("secs", "mins", "hours", "days", "weeks", "months", "years", "dyears", "wyears", "myears"), tzx = NULL, tzy = tzx ) diff_time_days(x, y, ...) diff_time_weeks(x, y, ...) diff_time_hours(x, y, ...) diff_time_mins(x, y, ...) diff_time_secs(x, y, ...) diff_time_months(x, y, ...) diff_time_years(x, y, ...) diff_time_dyears(x, y, ...) diff_time_wyears(x, y, ...) diff_time_myears(x, y, ...)
x , y
|
Vectors of times |
method |
A method to report the difference in units of time (see Units section) |
tzx , tzy
|
time zones (see Time zones section) |
... |
Additional arguments passed to |
A few significant differences exist with these functions * The class
of the object returned is no longer difftime
(but does print) with the
difftime
method. This makes the exporting process easier as the data
will not have to be converted back to numeric
* difftime()
computes the
difference of time1
- time2
, but the inverse feels a bit more nature:
time difference from x
to y
* Additional units can be used (detailed
below) * Differences can be sensitive to time zones if time zones are
passed to the tz
parameter as a character vector
A diff_time
vector, object
Units can be used beyond those available in
base::difftime()
. Some of these use assumptions in how units of time
should be standardized and can be changed in the corresponding options. Any
of these can be calculated with base::difftime()
through using units = "days"
but the dtime
class will print out with these specifications into
the console for less potential confusion.
Months by number of days mark.days_in_month
(defaults: 30
)
Years by number of days mark.days_in_year
(defaults: 365
)
Years by number of days mark.days_in_year
(defaults: 365
) (same as years
)
Years by number of days in a month mark.days_in_month
(defaults: 30
)
Years by number of weeks in a year mark.weeks_in_year
(defaults: 52
)
Time zones can be passed as either a numeric vector of
GMT/UTC offsets (the number of seconds from GMT) or as a character vector.
If the letter, these need to conform with values from base::OlsonNames()
.
A default timezone can be set with options(mark.default_tz = .)
. The
value can either be a numeric
Drop unused levels of a factor
drop_levels(x, ...) ## S3 method for class 'data.frame' drop_levels(x, ...) ## S3 method for class 'fact' drop_levels(x, ...) ## S3 method for class 'factor' drop_levels(x, ...)
drop_levels(x, ...) ## S3 method for class 'data.frame' drop_levels(x, ...) ## S3 method for class 'fact' drop_levels(x, ...) ## S3 method for class 'factor' drop_levels(x, ...)
x |
A |
... |
Additional arguments passed to methods (not used) |
Other factors:
as_ordered()
,
char2fact()
,
fact()
,
fact2char()
,
fact_na()
A wrapper for eval(parse(text = .))
ept(x, envir = parent.frame())
ept(x, envir = parent.frame())
x |
A character string to parse |
envir |
The environment in which to evaluate the code |
The evaluation of x
after parsing
Evaluate a named chunk from an Rmd file.
eval_named_chunk(rmd_file, label_name)
eval_named_chunk(rmd_file, label_name)
rmd_file |
Absolute path to rmd file |
label_name |
Name of label |
The value from the evaluated code chunk
temp_rmd <- tempfile(fileext = ".rmd") text <- ' ```{r not this label} print("that is wrong") ``` ```{r hello label} text <- "hello, world" print(text) print(TRUE) ``` ```{r another label} warning("wrong label") ``` ' ## Not run: writeLines(text, con = temp_rmd) eval_named_chunk(temp_rmd, "hello label") # [1] "hello, world" # [1] TRUE file.remove(temp_rmd) ## End(Not run)
temp_rmd <- tempfile(fileext = ".rmd") text <- ' ```{r not this label} print("that is wrong") ``` ```{r hello label} text <- "hello, world" print(text) print(TRUE) ``` ```{r another label} warning("wrong label") ``` ' ## Not run: writeLines(text, con = temp_rmd) eval_named_chunk(temp_rmd, "hello label") # [1] "hello, world" # [1] TRUE file.remove(temp_rmd) ## End(Not run)
Expands vector x by y
expand_by(x, y, expand = c("x", "y", "intersect", "both"), sort = FALSE)
expand_by(x, y, expand = c("x", "y", "intersect", "both"), sort = FALSE)
x , y
|
Vectors |
expand |
Character switch to expand or keep only the values that intersect, all values in x or y, or retain all values found. |
sort |
Logical, if |
A vector with expanded
x <- letters[c(3:2, 5, 9)] y <- letters[c(1:4, 8)] expand_by(x, y, "x") expand_by(x, y, "y") expand_by(x, y, "intersect") expand_by(x, y, "both")
x <- letters[c(3:2, 5, 9)] y <- letters[c(1:4, 8)] expand_by(x, y, "x") expand_by(x, y, "y") expand_by(x, y, "intersect") expand_by(x, y, "both")
Quickly create a factor
fact(x) ## Default S3 method: fact(x) ## S3 method for class 'character' fact(x) ## S3 method for class 'numeric' fact(x) ## S3 method for class 'integer' fact(x) ## S3 method for class 'Date' fact(x) ## S3 method for class 'POSIXt' fact(x) ## S3 method for class 'logical' fact(x) ## S3 method for class 'factor' fact(x) ## S3 method for class 'fact' fact(x) ## S3 method for class 'pseudo_id' fact(x) ## S3 method for class 'haven_labelled' fact(x)
fact(x) ## Default S3 method: fact(x) ## S3 method for class 'character' fact(x) ## S3 method for class 'numeric' fact(x) ## S3 method for class 'integer' fact(x) ## S3 method for class 'Date' fact(x) ## S3 method for class 'POSIXt' fact(x) ## S3 method for class 'logical' fact(x) ## S3 method for class 'factor' fact(x) ## S3 method for class 'fact' fact(x) ## S3 method for class 'pseudo_id' fact(x) ## S3 method for class 'haven_labelled' fact(x)
x |
A vector of values |
fact()
can be about 5 times quicker than factor()
or
as.factor()
as it doesn't bother sorting the levels for non-numeric data
or have other checks or features. It simply converts a vector to a factor
with all unique values as levels with NA
s included.
fact.factor()
will perform several checks on a factor to include NA
levels and to check if the levels should be reordered to conform with the
other methods. The fact.fact()
method simple returns x
.
A vector of equal length of x
with class fact
and factor
. If
x
was ordered
, that class is added in between.
The order of the levels may be adjusted to these rules depending on the class
of x
:
character
The order of appearance
numeric
/integer
/Date
/POSIXt
By the numeric order
logical
As TRUE
, FALSE
, then NA
if present
factor
Numeric if levels can be safely converted, otherwise as they are
Other factors:
as_ordered()
,
char2fact()
,
drop_levels()
,
fact2char()
,
fact_na()
fact
with NA
Included NA
values into fact()
fact_na(x, remove = FALSE)
fact_na(x, remove = FALSE)
x |
A |
remove |
If |
This re-formats the x
value so that NA
s are found immediately within the
object rather than accessed through its attributes.
A fact
vector
Other factors:
as_ordered()
,
char2fact()
,
drop_levels()
,
fact()
,
fact2char()
Reverse the levels of a fact
fact_reverse(x)
fact_reverse(x)
x |
A |
Convert factor columns to characters in a data.frame
fact2char(data, threshold = 10)
fact2char(data, threshold = 10)
data |
A |
threshold |
A threshold for the number of levels to be met/exceeded for transforming into a character |
The data.frame
data
with factors converted by the rule above
Other factors:
as_ordered()
,
char2fact()
,
drop_levels()
,
fact()
,
fact_na()
Expands an ordered factor from one level to another
fct_expand_seq( x, min_lvl = min(x, na.rm = TRUE), max_lvl = max(x, na.rm = TRUE), by = 1L )
fct_expand_seq( x, min_lvl = min(x, na.rm = TRUE), max_lvl = max(x, na.rm = TRUE), by = 1L )
x |
An ordered factor |
min_lvl |
The start of the level sequence |
max_lvl |
The end of the level sequence |
by |
Integer, number of steps in between |
Defaults for min_lvl
and max_lvl
are the minimum and maximum
levels in the ordered vector x
.
An ordered
vector
x <- ordered(letters[c(5:15, 2)], levels = letters) fct_expand_seq(x) fct_expand_seq(x, "g", "s", 3L) # from "g" to "s" by 3 fct_expand_seq(x, "g", "t", 3L) # same as above # from the first inherit level to the last observed fct_expand_seq(x, min(levels(x)))
x <- ordered(letters[c(5:15, 2)], levels = letters) fct_expand_seq(x) fct_expand_seq(x, "g", "s", 3L) # from "g" to "s" by 3 fct_expand_seq(x, "g", "t", 3L) # same as above # from the first inherit level to the last observed fct_expand_seq(x, min(levels(x)))
File copy with md5 hash check
file_copy_md5(path, new_path, overwrite = NA, quiet = FALSE)
file_copy_md5(path, new_path, overwrite = NA, quiet = FALSE)
path |
A character vector of one or more paths. |
new_path |
A character vector of paths to the new locations. |
overwrite |
When |
quiet |
When |
The path(s) of the new file(s), invisibly. When overwrite
is
NA
, the paths will be returned with two addition attributes, "changed"
,
a logical vector indicating whether the file was changed (NA
for when the
file is new), and "md5sum"
, a list of the md5sums of the old and new md5
sums.
Other utility functions for dealing with files
newest_file(x) newest_dir(x) oldest_file(x) oldest_dir(x) largest_file(x) smallest_file(x)
newest_file(x) newest_dir(x) oldest_file(x) oldest_dir(x) largest_file(x) smallest_file(x)
x |
A vector of file paths |
A full file path
Basename of file without extension
file_name(x, compression = FALSE)
file_name(x, compression = FALSE)
x |
character vector giving file paths. |
compression |
logical: should compression extension ‘.gz’, ‘.bz2’ or ‘.xz’ be removed first? |
The file name of the path without the extension
Opens the given files(s)
open_file(x) file_open(x) shell_exec(x) list_files( x = ".", pattern = utils::glob2rx(glob), glob = NULL, ignore_case = FALSE, all = FALSE, negate = FALSE, basename = FALSE ) list_dirs( x = ".", pattern = NULL, ignore_case = FALSE, all = FALSE, basename = FALSE, negate = FALSE )
open_file(x) file_open(x) shell_exec(x) list_files( x = ".", pattern = utils::glob2rx(glob), glob = NULL, ignore_case = FALSE, all = FALSE, negate = FALSE, basename = FALSE ) list_dirs( x = ".", pattern = NULL, ignore_case = FALSE, all = FALSE, basename = FALSE, negate = FALSE )
x |
A character vector of paths |
pattern , glob
|
Pattern to search for files. |
ignore_case |
logical. Should pattern-matching be case-insensitive? |
all |
a logical value. If FALSE, only the names of visible files are returned (following Unix-style visibility, that is files whose name does not start with a dot). If TRUE, all file names will be returned. |
negate |
Logical, if |
basename |
If |
open_file
is an alternative to shell.exec()
that can take take
multiple files. list_files
and list_dirs
are mostly wrappers for
fs::dir_ls()
with preferred defaults and pattern searching on the full file
path.
file_open
is simply an alias.
open_file()
, shell_exec()
: A logical vector where TRUE
successfully
opened, FALSE
did not and NA
did not try to open (file not found)
list_files()
, list_dirs()
: A vector of full paths
For when someone asked you to do something you've done before, you can argue that the quickest way to do it is to just take the work someone else did and utilize that. No reason to reinvent the wheel.
fizzbuzz(n, show_numbers = TRUE) fizzbuzz_lazy(n) .fizzbuzz_vector
fizzbuzz(n, show_numbers = TRUE) fizzbuzz_lazy(n) .fizzbuzz_vector
n |
The number of numbers |
show_numbers |
If |
An object of class character
of length 1000000.
Multiples of 3
are shown as "Fizz"
; multiples of 5
as "Buzz"
;
multiple of both (i.e., 15
) are "FizzBuzz"
.
fizzbuzz_lazy()
subsets the .fizzbuzz_vector
object, which is a solution
with default parameters up to 1e6
A character
vector of 1, 2, Fizz, 3, Buzz
, etc
fizzbuzz(15) fizzbuzz(30, show_numbers = FALSE) cat(fizzbuzz(30), sep = "\n") # show them how fast your solution is: if (package_available("bench")) { bench::mark(fizzbuzz(1e5), fizzbuzz_lazy(1e5)) }
fizzbuzz(15) fizzbuzz(30, show_numbers = FALSE) cat(fizzbuzz(30), sep = "\n") # show them how fast your solution is: if (package_available("bench")) { bench::mark(fizzbuzz(1e5), fizzbuzz_lazy(1e5)) }
Finds the directory where the number is the greatest. This can be useful for when folders are created as run IDs.
get_dir_max_number(x)
get_dir_max_number(x)
x |
The directory to look in |
A full path to a directory
Looks at the directories and assumes the date
get_dir_recent_date(x = ".", dt_pattern = NULL, dt_format = NULL, all = FALSE)
get_dir_recent_date(x = ".", dt_pattern = NULL, dt_format = NULL, all = FALSE)
x |
A directory |
dt_pattern |
A pattern to be passed to filter for the directory |
dt_format |
One or more formats to try |
all |
Logical, if |
A full path to a directory
Finds the recent subdirectory in a directory.
get_recent_dir(x = ".", ...)
get_recent_dir(x = ".", ...)
x |
The root directory |
... |
Additional arguments passed to |
The full path of the most recent directory
A function where you can detect the most recent file from a directory.
get_recent_file(x, exclude_temp = TRUE, ...)
get_recent_file(x, exclude_temp = TRUE, ...)
x |
The directory in which to search the file |
exclude_temp |
Logical, if |
... |
Additional arguments passed to |
The full name of the most recent file from the stated directory
Will read the DESCRIPTION
file and to get and adjust the
version
bump_date_version()
will not check if the version is actually a date. When
the current version is the same as today's date(equal by character strings)
it will append a .1
.
get_version() bump_version(version = NULL) bump_date_version(version = NULL) update_version(version = NULL, date = FALSE)
get_version() bump_version(version = NULL) bump_date_version(version = NULL) update_version(version = NULL, date = FALSE)
version |
A new version to be added; default of |
date |
If |
Get and bump package version for dates
get_version()
: A package_version
bump_version()
: None, called for its side-effects
bump_date_version()
: None, called for its side-effects
update_version()
: None, called for its side-effects
Helper function for globbing character vectors
glob(x, pattern = NULL, value = TRUE, ...)
glob(x, pattern = NULL, value = TRUE, ...)
x |
A vector of characters |
pattern |
Wildcard globbing pattern |
value , ...
|
Additional parameters passed to |
x <- c("apple", "banana", "peach", "pear", "orange") glob(x, "*e") glob(x, "pea*", value = FALSE) glob(x, "*an*", value = NA) path <- system.file("R", package = "mark") glob(list.files(path), "r*")
x <- c("apple", "banana", "peach", "pear", "orange") glob(x, "*e") glob(x, "pea*", value = FALSE) glob(x, "*an*", value = NA) path <- system.file("R", package = "mark") glob(list.files(path), "r*")
Catch and report handlers
has_warning(x, FUN, ...) has_error(x, FUN, ...) has_message(x, FUN, ...) get_warning(x, FUN, ..., .null = TRUE) get_message(x, FUN, ..., .null = TRUE) get_error(x, FUN, ..., .null = TRUE)
has_warning(x, FUN, ...) has_error(x, FUN, ...) has_message(x, FUN, ...) get_warning(x, FUN, ..., .null = TRUE) get_message(x, FUN, ..., .null = TRUE) get_error(x, FUN, ..., .null = TRUE)
x |
A vector |
FUN |
A function |
... |
Additional params passed to |
.null |
Logical, if |
These functions can be used to catch whether an evaluation will return an error or warning without raising.
The has_*()
functions will return TRUE
/FALSE
for if the handler
is found in the execution of the code. The get_*()
functions provide the
text of the message
Function for catching has been adapted from https://stackoverflow.com/a/4952908/12126576
has_warning(c(1, "no"), as.integer) # 1 no # FALSE TRUE get_warning(c(1, "no"), as.integer) # drop NULLs get_warning(c(1, "no"), as.integer, .null = FALSE) foo <- function(x) { stopifnot(x > 0) x } has_error(c(1, 0, 2), foo) # 1 0 2 # FALSE TRUE FALSE get_error(c(1, 0, 2), foo) # drop NULLs get_error(c(1, 0, 2), foo, .null = FALSE)
has_warning(c(1, "no"), as.integer) # 1 no # FALSE TRUE get_warning(c(1, "no"), as.integer) # drop NULLs get_warning(c(1, "no"), as.integer, .null = FALSE) foo <- function(x) { stopifnot(x > 0) x } has_error(c(1, 0, 2), foo) # 1 0 2 # FALSE TRUE FALSE get_error(c(1, 0, 2), foo) # drop NULLs get_error(c(1, 0, 2), foo, .null = FALSE)
Import a single function from a package
import(pkg, fun, overwrite = FALSE)
import(pkg, fun, overwrite = FALSE)
pkg |
String, name of the package |
fun |
String, fun name of the function |
overwrite |
Logical, if TRUE and |
None, called for side effects
# assigns `add` -- test with caution import("magrittr", "add")
# assigns `add` -- test with caution import("magrittr", "add")
Insert values at a position
insert(x, positions, values)
insert(x, positions, values)
x |
A vector of values |
positions |
Integer of positions of |
values |
A vector of values to insert into |
A vector with the intended values inserted
insert(letters[1:5], c(2, 4), c("X", "Y"))
insert(letters[1:5], c(2, 4), c("X", "Y"))
Is the path a file/directory?
is_dir(x) is_file(x)
is_dir(x) is_file(x)
x |
A vector of file paths |
These are essentially taken from utils::file_test()
for op = '-d'
and
op = -f
but separated.
A logical
vector
Assign labels to a vector or data.frame.
assign_labels(x, ...) ## Default S3 method: assign_labels(x, label, ...) ## S3 method for class 'data.frame' assign_labels( x, ..., .missing = c("error", "warn", "skip"), .ls = rlang::list2(...) ) get_labels(x) ## Default S3 method: get_labels(x) ## S3 method for class 'data.frame' get_labels(x) view_labels(x, title) remove_labels(x, ...) ## Default S3 method: remove_labels(x, ...) ## S3 method for class 'data.frame' remove_labels(x, cols, ...)
assign_labels(x, ...) ## Default S3 method: assign_labels(x, label, ...) ## S3 method for class 'data.frame' assign_labels( x, ..., .missing = c("error", "warn", "skip"), .ls = rlang::list2(...) ) get_labels(x) ## Default S3 method: get_labels(x) ## S3 method for class 'data.frame' get_labels(x) view_labels(x, title) remove_labels(x, ...) ## Default S3 method: remove_labels(x, ...) ## S3 method for class 'data.frame' remove_labels(x, cols, ...)
x |
A vector of data.frame |
... |
One or more unquoted expressed separated by commas. If assigning
to a data.frame, |
label |
A single length string of a label to be assigned |
.missing |
A control setting for dealing missing columns in a list;
can be set to |
.ls |
A named list of columns and labels to be set if |
title |
Title for the viewer window – if not supplemented will show as
|
cols |
A character vector of column names; if missing will remove the label attribute across all columns |
When labels are assigned to a data.frame they can make viewing the object
(with View()
inside Rstudio). The view_labels()
has a call to View()
inside and will retrieve the labels and show them in the viewer as a
data.frame.
A labelled vector or data.frame
labs <- assign_labels( iris, Sepal.Length = "cms", Sepal.Width = "cms", Petal.Length = "cms", Petal.Width = "cms", Species = "Iris ..." ) labs$dummy <- "" get_labels(labs) # shows label as <NA> for dummy column labs0 <- remove_labels(labs, c("Sepal.Length", "Sepal.Width")) get_labels(labs0) # No labels for Sepal.Length and Sepal.Width
labs <- assign_labels( iris, Sepal.Length = "cms", Sepal.Width = "cms", Petal.Length = "cms", Petal.Width = "cms", Species = "Iris ..." ) labs$dummy <- "" get_labels(labs) # shows label as <NA> for dummy column labs0 <- remove_labels(labs, c("Sepal.Length", "Sepal.Width")) get_labels(labs0) # No labels for Sepal.Length and Sepal.Width
Limit a numeric vector by lower and upper bounds
limit(x, lower = min(x), upper = max(x))
limit(x, lower = min(x), upper = max(x))
x |
A numeric vector |
lower |
A lower limit (as |
upper |
An upper limit (as |
The vector x
with lower
and upper
as the minimum, maximum
values
Find the total number of lines of R code
lines_of_r_code(x = ".", skip_empty = TRUE)
lines_of_r_code(x = ".", skip_empty = TRUE)
x |
Directory to search for files |
skip_empty |
Logical, if TRUE will not count lines that are empty or only contain a bracket or quotation mark. |
Tries to read each file in the directory that ends in .R or .r and sums together. Files that fail to read are not counted.
An integer
for the number of lines in all applicable files
lines_of_r_code(system.file()) lines_of_r_code(system.file(), skip_empty = FALSE)
lines_of_r_code(system.file()) lines_of_r_code(system.file(), skip_empty = FALSE)
Functions to list out all environments and objects
environments() ls_all(all.names = FALSE) objects_all(all.names = FALSE)
environments() ls_all(all.names = FALSE) objects_all(all.names = FALSE)
all.names |
a logical value. If |
environments()
is basically a printing wrapper for base::search()
ls_all()
and objects_all()
can be used retrieved all objects from all
environments in the search()
path, which may print out a large result
into the console.
environments()
: Invisibly, a character
vector of environment names
ls_all()
, objects_all()
: A named list for each of the environments
the search()
path with all the objects found in that environment
Converts a list object into a data.frame
list2df(x, name = "name", value = "value", show_NA, warn = TRUE)
list2df(x, name = "name", value = "value", show_NA, warn = TRUE)
x |
A (preferably) named |
name , value
|
Names of the new key and value columns, respectively |
show_NA |
Ignored; if set will trigger a warning |
warn |
Logical; if TRUE will show a warning when |
Unlike base::list2DF()
, list2df()
tries to format the data.frame by using
the names of the list as values rather than variables. This creates a
longer form list that may be more tidy.
a data.frame
object with columns "name"
and "value"
for the
names of the list
and the values in each
x <- list(a = 1, b = 2:4, c = letters[10:20], "unnamed", "unnamed2") list2df(x, "col1", "col2", warn = FALSE) if (getRversion() >= as.package_version('4.0')) { # contrast with `base::list2DF()` and `base::as.data.frame()` x <- list(a = 1:3, b = 2:4, c = letters[10:12]) list2df(x, warn = FALSE) list2DF(x) as.data.frame(x) }
x <- list(a = 1, b = 2:4, c = letters[10:20], "unnamed", "unnamed2") list2df(x, "col1", "col2", warn = FALSE) if (getRversion() >= as.package_version('4.0')) { # contrast with `base::list2DF()` and `base::as.data.frame()` x <- list(a = 1:3, b = 2:4, c = letters[10:12]) list2df(x, warn = FALSE) list2DF(x) as.data.frame(x) }
All functions take logical or logical-like (i.e., 1, 0, or NA as integer or doubles) and return logical values.
Extensions to the base logical operations to account for NA
values.
base::isTRUE()
and base::isFALSE()
will only return single length TRUE
or FALSE
as it checks for valid lengths in the evaluation. When needing to
check over a vector for the presence of TRUE
or FALSE
and not being held
back by NA
values, is_true
and is_false
will always provide a TRUE
FALSE
when the vector is logical or return NA
is the vector x
is not
logical.
%or%
is just a wrapper for base::xor()
is_true(x) ## Default S3 method: is_true(x) ## S3 method for class 'logical' is_true(x) is_false(x) ## Default S3 method: is_false(x) ## S3 method for class 'logical' is_false(x) x %xor% y OR(..., na.rm = FALSE) AND(..., na.rm = FALSE) either(x, y) is_boolean(x) none(..., na.rm = FALSE)
is_true(x) ## Default S3 method: is_true(x) ## S3 method for class 'logical' is_true(x) is_false(x) ## Default S3 method: is_false(x) ## S3 method for class 'logical' is_false(x) x %xor% y OR(..., na.rm = FALSE) AND(..., na.rm = FALSE) either(x, y) is_boolean(x) none(..., na.rm = FALSE)
x , y
|
A vector of logical values. If |
... |
Vectors or a list of logical values |
na.rm |
Logical, if |
Logical operations, extended
is_true()
, is_false()
, either()
, %or%
, AND()
, OR()
: A logical
vector, equal length of x
(or y
or of all ...
lengths)
is_boolean()
: TRUE
or FALSE
none()
: TRUE
, FALSE
, or NA
x <- c(TRUE, FALSE, NA) y <- c(FALSE, FALSE, TRUE) z <- c(TRUE, NA, TRUE) isTRUE(x) is_true(x) isFALSE(x) is_false(x) x %xor% TRUE TRUE %xor% TRUE TRUE %xor% FALSE NA %xor% FALSE OR(x, y, z) OR(x, y, z, na.rm = TRUE) AND(x, y, z) AND(x, y, z, na.rm = TRUE) either(x, FALSE) either(TRUE, FALSE) either(FALSE, NA) either(TRUE, NA) none(x) none(x & y, na.rm = TRUE) is_boolean(x) is_boolean(c(1L, NA_integer_, 0L)) is_boolean(c(1.01, 0, -1))
x <- c(TRUE, FALSE, NA) y <- c(FALSE, FALSE, TRUE) z <- c(TRUE, NA, TRUE) isTRUE(x) is_true(x) isFALSE(x) is_false(x) x %xor% TRUE TRUE %xor% TRUE TRUE %xor% FALSE NA %xor% FALSE OR(x, y, z) OR(x, y, z, na.rm = TRUE) AND(x, y, z) AND(x, y, z, na.rm = TRUE) either(x, FALSE) either(TRUE, FALSE) either(FALSE, NA) either(TRUE, NA) none(x) none(x & y, na.rm = TRUE) is_boolean(x) is_boolean(c(1L, NA_integer_, 0L)) is_boolean(c(1.01, 0, -1))
List Objects - extensions
ls_dataframe(pattern, all.names = FALSE, envir = parent.frame()) ls_function(pattern, all.names = FALSE, envir = parent.frame()) ls_object(pattern, all.names = FALSE, envir = parent.frame())
ls_dataframe(pattern, all.names = FALSE, envir = parent.frame()) ls_function(pattern, all.names = FALSE, envir = parent.frame()) ls_object(pattern, all.names = FALSE, envir = parent.frame())
pattern |
an optional regular expression. Only names
matching |
all.names |
a logical value. If |
envir |
an alternative argument to |
A character
vector of names
Simple wrapper for package specific function for internal packages
make_sf(package)
make_sf(package)
package |
The name of the package |
Miscellaneous, Analytic R Kernels
Maintainer: Jordan Mark Barbone [email protected] (ORCID) [copyright holder]
Useful links:
Report bugs at https://github.com/jmbarbone/mark/issues
This function is essentially a clear version of base::match.arg()
which
produces a cleaner warning message and does not restrict the table
param
to character
vectors only.
match_arg(x, table)
match_arg(x, table)
x |
An argument |
table |
A table of choices |
Match arguments
A single value from x
matched on table
x <- c("apple", "banana", "orange") match_arg("b", x) # Produces error try(match_arg("pear", x)) foo <- function(x, op = c(1, 2, 3)) { op <- match_arg(op) x / op } foo(10, 3) # Error try(foo(1, 0))
x <- c("apple", "banana", "orange") match_arg("b", x) # Produces error try(match_arg("pear", x)) foo <- function(x, op = c(1, 2, 3)) { op <- match_arg(op) x / op } foo(10, 3) # Error try(foo(1, 0))
Much like base::match.arg()
with a few key differences:
Will not perform partial matching
Will not return error messages with ugly quotation marks
match_param( param, choices, null = TRUE, partial = getOption("mark.match_param.partial", FALSE), multiple = FALSE, simplify = TRUE )
match_param( param, choices, null = TRUE, partial = getOption("mark.match_param.partial", FALSE), multiple = FALSE, simplify = TRUE )
param |
The parameter |
choices |
The available choices; named lists will return the name (a character) for when matched to the value within the list element. A list of formula objects (preferred) retains the LHS of the formula as the return value when matched to the RHS of the formula. |
null |
If |
partial |
If |
multiple |
If |
simplify |
If |
Param matching for an argument
A single value from param
matched on choices
fruits <- function(x = c("apple", "banana", "orange")) { match_param(x) } fruits() # apple try(fruits("b")) # must be exact fruits("banana") pfruits <- function(x = c("apple", "apricot", "banana")) { match_param(x, partial = TRUE) } pfruits() # apple try(pfruits("ap")) # matchParamMatchError pfruits("app") # apple afruits <- function(x = c("apple", "banana", "orange")) { match_param(x, multiple = TRUE) } afruits() # apple, banana, orange # can have multiple responses how_much <- function(x = list(too_few = 0:2, ok = 3:5, too_many = 6:10)) { match_param(x) } how_much(1) how_much(3) how_much(9) # use a list of formulas instead ls <- list(1L ~ 0:1, 2L, 3L ~ 3:5) sapply(0:5, match_param, choices = ls)
fruits <- function(x = c("apple", "banana", "orange")) { match_param(x) } fruits() # apple try(fruits("b")) # must be exact fruits("banana") pfruits <- function(x = c("apple", "apricot", "banana")) { match_param(x, partial = TRUE) } pfruits() # apple try(pfruits("ap")) # matchParamMatchError pfruits("app") # apple afruits <- function(x = c("apple", "banana", "orange")) { match_param(x, multiple = TRUE) } afruits() # apple, banana, orange # can have multiple responses how_much <- function(x = list(too_few = 0:2, ok = 3:5, too_many = 6:10)) { match_param(x) } how_much(1) how_much(3) how_much(9) # use a list of formulas instead ls <- list(1L ~ 0:1, 2L, 3L ~ 3:5) sapply(0:5, match_param, choices = ls)
Wrapper for calling tools::md5sum()
on objects rather than files.
md5(x)
md5(x)
x |
An object |
All x
objects are serialized to a temporary
file before tools::md5sum()
is called.
A md5sum
object
md5("hello") md5(1:10) md5(data.frame(a = 1:10, b = letters[1:10]))
md5("hello") md5(1:10) md5(data.frame(a = 1:10, b = letters[1:10]))
Median as the 50th quantile with an option to select quantile algorithm
median2(x, type = 7, na.rm = FALSE) q50(x, type = 7, na.rm = FALSE)
median2(x, type = 7, na.rm = FALSE) q50(x, type = 7, na.rm = FALSE)
x |
numeric vector whose sample quantiles are wanted, or an
object of a class for which a method has been defined (see also
‘details’). |
type |
an integer between 1 and 9 selecting one of the nine quantile algorithms detailed below to be used. |
na.rm |
logical; if true, any |
q50
is an alias for median2
See stats::quantile()
set.seed(42) x <- rnorm(100) median(x) # 0.08979677 median2(x, type = 7) # 0.08979677 - default type is 7 median2(x, type = 3) # 0.08976065
set.seed(42) x <- rnorm(100) median(x) # 0.08979677 median2(x, type = 7) # 0.08979677 - default type is 7 median2(x, type = 3) # 0.08976065
Merge lists with different or intersecting names
merge_list(x, y, keep = c("x", "y"), null = c("ignore", "drop", "keep")[1:2])
merge_list(x, y, keep = c("x", "y"), null = c("ignore", "drop", "keep")[1:2])
x , y
|
Lists to merge |
keep |
When matching names are found, from which object should the
values be retained; |
null |
Method for handling
|
x <- list(a = 1, b = 2, c = NULL, d = NULL) y <- list(a = 2, b = NULL, c = 3) # compared to: utils::modifyList(x, y) utils::modifyList(x, y, keep.null = TRUE) merge_list(x, y) merge_list(x, y, keep = "y") merge_list(x, y, null = "drop")
x <- list(a = 1, b = 2, c = NULL, d = NULL) y <- list(a = 2, b = NULL, c = 3) # compared to: utils::modifyList(x, y) utils::modifyList(x, y, keep.null = TRUE) merge_list(x, y) merge_list(x, y, keep = "y") merge_list(x, y, null = "drop")
Multiple search pattern searches
multi_grepl(x, patterns, ..., simplify = TRUE) multi_grep(x, patterns, ..., simplify = TRUE)
multi_grepl(x, patterns, ..., simplify = TRUE) multi_grep(x, patterns, ..., simplify = TRUE)
x |
Passed to |
patterns |
A list or vector of patterns to search across |
... |
Additional arguments passed to |
simplify |
if |
The name or position of the pattern that is matched
x <- c("apple", "banana", "lemon") multi_grepl(x, c("a" = "^[ab]", "b" = "lem")) multi_grepl(x, c("a" = "^[ab]", "b" = "q")) # lemon not matches on either multi_grepl(x, c("a" = "^[ab]", "b" = "e")) # apple matches "a" before "b" multi_grepl(x, c("a" = "^[ab]", "b" = "e"), simplify = FALSE) # shows all matches multi_grepl(x, c("^[ab]", "e")) # returned as positions multi_grepl(x, c("^[ab]", "e"), simplify = FALSE)
x <- c("apple", "banana", "lemon") multi_grepl(x, c("a" = "^[ab]", "b" = "lem")) multi_grepl(x, c("a" = "^[ab]", "b" = "q")) # lemon not matches on either multi_grepl(x, c("a" = "^[ab]", "b" = "e")) # apple matches "a" before "b" multi_grepl(x, c("a" = "^[ab]", "b" = "e"), simplify = FALSE) # shows all matches multi_grepl(x, c("^[ab]", "e")) # returned as positions multi_grepl(x, c("^[ab]", "e"), simplify = FALSE)
Converts select elements of a vector into NA
s
This is how the end results are
NA_at
and NA_if
require a suitable index value (x[y] <- NA
)
NA_at
expects y
(or the result of function y
) to be integers
NA_if
expects y
(or the result of function y
) to be logical
NA_in
and NA_out
expect some values to match on
NA_in
checks x[x %in% y] <- NA
NA_out
checks x[x %out% y] <- NA
(see fuj::match_ext)
NA_at(x, y, ...) NA_if(x, y, ...) NA_in(x, y, ...) NA_out(x, y, ...)
NA_at(x, y, ...) NA_if(x, y, ...) NA_in(x, y, ...) NA_out(x, y, ...)
x |
A vector of values |
y |
Either a suitable value (see |
... |
Additional values passed to |
Convert specific values to NA
x
with assigned NA
values
Inspired by dplyr::na_if()
let <- ordered(letters[1:5]) NA_at(let, c(1, 3, 5)) # [1] <NA> b <NA> d <NA> NA_if(let, let <= "b") # [1] <NA> <NA> c d e NA_in(let, c("a", "c")) # [1] <NA> b <NA> d e NA_out(let, c("a", "c")) # [1] a <NA> c <NA> <NA>
let <- ordered(letters[1:5]) NA_at(let, c(1, 3, 5)) # [1] <NA> b <NA> d <NA> NA_if(let, let <= "b") # [1] <NA> <NA> c d e NA_in(let, c("a", "c")) # [1] <NA> b <NA> d e NA_out(let, c("a", "c")) # [1] a <NA> c <NA> <NA>
Select or remove columns that are entirely NA
select_na_cols(x) remove_na_cols(x) is_na_cols(x, names = TRUE)
select_na_cols(x) remove_na_cols(x) is_na_cols(x, names = TRUE)
x |
A |
names |
Logical, if |
select_na_cols()
x
with only columns that are all NA
remove_na_cols()
x
without columns of only NA
is_na_cols()
a logical vector: TRUE
all rows of column are NA
,
otherwise FALSE
Normalize and check a vector of paths
norm_path(x = ".", check = FALSE, remove = check) file_path(..., check = FALSE, remove = check) user_file(..., check = FALSE, remove = check)
norm_path(x = ".", check = FALSE, remove = check) file_path(..., check = FALSE, remove = check) user_file(..., check = FALSE, remove = check)
x |
A character vector of paths |
check |
Logical, if TRUE will check if the path exists and output a warning if it does not. |
remove |
Logical, if TRUE will remove paths that are not found |
... |
Character vectors for creating a path |
A vector of full file paths
Normalizes values based on possible range and new bounds
normalize(x, ...) ## Default S3 method: normalize(x, range = base::range(x, na.rm = TRUE), bounds = 0:1, ...) ## S3 method for class 'data.frame' normalize(x, ...)
normalize(x, ...) ## Default S3 method: normalize(x, range = base::range(x, na.rm = TRUE), bounds = 0:1, ...) ## S3 method for class 'data.frame' normalize(x, ...)
x |
An object that is (coercible to) |
... |
Additional arguments passed to methods |
range |
The range of possible values of |
bounds |
The new boundaries for the normalized values of |
Parameters range
and bounds
are modified with base::range()
.
The largest and smallest values are then used to determine the
minimum/maximum values and lower/upper bounds. This allows for a vector of
more than two values to be passed.
The current implementation of normalize.data.frame()
allows for list
of
parameters passed for each column. However, it is probably best suited for
default values.
x
with transformed values where range
values are transformed to
bounds
.
x <- c(0.23, 0.32, 0.12, 0.61, 0.26, 0.24, 0.23, 0.32, 0.29, 0.27) data.frame( x = normalize(x), v = normalize(x, range = 0:2), b = normalize(x, bounds = 0:10), vb = normalize(x, range = 0:2, bounds = 0:10) ) # maintains matrix mat <- structure(c(0.24, 0.92, 0.05, 0.37, 0.19, 0.69, 0.43, 0.22, 0.85, 0.73, 0.89, 0.68, 0.57, 0.89, 0.61, 0.98, 0.75, 0.37, 0.24, 0.24, 0.34, 0.8, 0.25, 0.46, 0.03, 0.71, 0.79, 0.56, 0.83, 0.97), dim = c(10L, 3L)) mat normalize(mat, bounds = -1:1) normalize(as.data.frame(mat), bounds = -1:1)
x <- c(0.23, 0.32, 0.12, 0.61, 0.26, 0.24, 0.23, 0.32, 0.29, 0.27) data.frame( x = normalize(x), v = normalize(x, range = 0:2), b = normalize(x, bounds = 0:10), vb = normalize(x, range = 0:2, bounds = 0:10) ) # maintains matrix mat <- structure(c(0.24, 0.92, 0.05, 0.37, 0.19, 0.69, 0.43, 0.22, 0.85, 0.73, 0.89, 0.68, 0.57, 0.89, 0.61, 0.98, 0.75, 0.37, 0.24, 0.24, 0.34, 0.8, 0.25, 0.46, 0.03, 0.71, 0.79, 0.56, 0.83, 0.97), dim = c(10L, 3L)) mat normalize(mat, bounds = -1:1) normalize(as.data.frame(mat), bounds = -1:1)
Create NA vectors
not_available(type = "logical", length = 0L) set_not_available(type, value) NA_Date_ NA_POSIXct_ NA_POSIXlt_
not_available(type = "logical", length = 0L) set_not_available(type, value) NA_Date_ NA_POSIXct_ NA_POSIXlt_
type |
Type of NA (see details) |
length |
Length of the vector |
value |
A value to return in |
An object of class Date
of length 1.
An object of class POSIXct
(inherits from POSIXt
) of length 1.
An object of class POSIXlt
(inherits from POSIXt
) of length 1.
If length is a text it will search for an appropriate match.
A vector of NA
values
x <- not_available("Date", 3) x class(x)
x <- not_available("Date", 3) x class(x)
An alternative to the base::comment()
.
note(x) <- value set_note(x, value) note(x)
note(x) <- value set_note(x, value) note(x)
x |
An object |
value |
The note to attach; if |
When the note is assigned to an object a new class will be added,
note
, so that a print
function can call an S3 method. The print for
this can be adjusted for it's width by using the option mark.note.width
which defaults to the option width
when not set.
The type of object assigned to the note is not restricted, so user beware of odd prints or additional features added to the notes fun.
When assigning a note (with note<-
, and its alias set_note()
) the
noted
class is added to the object. This allows the print.noted
class
to be dispatched and for the note to be printed every time the object is
called/printed and the next print method used. However, it will not be
called when not interactive()
note<-
, set_note()
will return x
(with the "note"
attribute
assigned)
note()
will retrieve the "note"
attribute
x <- c("x", "k", "c", "d") comment(x) <- "This is just a comment" comment(x) # Comment is intentionally hidden x note(x) <- "Just some random letters" note(x) # Note is now present every time x # Assigning `NULL` will remove note (and class) note(x) <- NULL note(x) # NULL x # No more note
x <- c("x", "k", "c", "d") comment(x) <- "This is just a comment" comment(x) # Comment is intentionally hidden x note(x) <- "Just some random letters" note(x) # Note is now present every time x # Assigning `NULL` will remove note (and class) note(x) <- NULL note(x) # NULL x # No more note
Omit NA values
omit_na(x)
omit_na(x)
x |
A vector of values |
x
which NA
values removes and two attributes of integers
: na
which is the position of NA
values, and valid
for the position of
non-NA
values; empty positions reported as integer(0)
# Like stats::na.omit but always provides x <- letters[1:5] omit_na(x) x[c(3, 5)] <- NA omit_na(x)
# Like stats::na.omit but always provides x <- letters[1:5] omit_na(x) x[c(3, 5)] <- NA omit_na(x)
A wrapped requireNamespace
package_available(namespace)
package_available(namespace)
namespace |
One or more packages to to require. |
require_namespace()
: None, called for side effects
package_available()
: Visibly, TRUE
or FALSE
The bounds of the percentile rank are > 0 and < 1 (see Boundaries)
A percentile rank here is the proportion of scores that are less than the current score.
Where
is the frequency of scores less than the score of interest
is the frequency of the score of interest
percentile_rank(x, weights = times, times)
percentile_rank(x, weights = times, times)
x |
A vector of values to rank |
weights , times
|
A vector of the number of times to repeat |
Computes a percentile rank for each score in a set.
The percentile rank of x
between 0 and 1 (see Boundaries)
While the percentile rank of a score in a set must be exclusively within the
boundaries of 0
and 1
, this function may produce a percentile rank that
is exactly 0
or 1
. This may occur when the number of values are so large
that the value within the boundaries is too small to be differentiated.
Additionally, when using the weights
parameter, if the lowest or highest
number has a value of 0
, the number will then have a theoretical 0
or
1
, as these values are not actually within the set.
percentile_rank(0:9) x <- c(1, 2, 1, 7, 5, NA_integer_, 7, 10) percentile_rank(x) if (package_available("dplyr")) { dplyr::percent_rank(x) } # with times percentile_rank(7:1, c(1, 0, 2, 2, 3, 1, 1))
percentile_rank(0:9) x <- c(1, 2, 1, 7, 5, NA_integer_, 7, 10) percentile_rank(x) if (package_available("dplyr")) { dplyr::percent_rank(x) } # with times percentile_rank(7:1, c(1, 0, 2, 2, 3, 1, 1))
Prints a vector to paste into an R script
print_c(x = read_clipboard(), sorted = TRUE, null = TRUE)
print_c(x = read_clipboard(), sorted = TRUE, null = TRUE)
x |
A vector (defaults to reading the clipboard) |
sorted |
If |
null |
If |
This sorts (if set) and provides unique values for each element in x
and
prints then as a call to c
. This can be useful for copying data that you
want to save as a vector in an R script.
The result is both called in cat()
as well as copied to the clipboard.
Invisibly, as a character
vector, the object printed to the console
print_c(1:10) print_c(letters[1:3]) print_c(month.abb)
print_c(1:10) print_c(letters[1:3]) print_c(month.abb)
Print bib dataframe, or as list
## S3 method for class 'mark_bib_df' print(x, list = FALSE, ...)
## S3 method for class 'mark_bib_df' print(x, list = FALSE, ...)
x |
The |
list |
If |
... |
Additional arguments passed to methods |
x
, invisibly, called for its side effects
pseudo_id
Print pseudo_id
## S3 method for class 'pseudo_id' print(x, ..., all = FALSE)
## S3 method for class 'pseudo_id' print(x, ..., all = FALSE)
x |
An object of class pseudo_id |
... |
Not implemented |
all |
if |
x
, invisibly. Called for its side effects.
Generates a data frame of values from bibs
process_bib_dataframe(categories, values, fields, keys)
process_bib_dataframe(categories, values, fields, keys)
categories |
A list of categories |
values |
A list of values |
fields |
a Vector of fields |
keys |
a Vector of keys |
A wide data.frame
with explicit NA
s
Transforms a vector into an integer of IDs.
pseudo_id(x, ...) ## S3 method for class 'pseudo_id' pseudo_id(x, ...) ## Default S3 method: pseudo_id(x, na_last = TRUE, ...) ## S3 method for class 'factor' pseudo_id(x, ...)
pseudo_id(x, ...) ## S3 method for class 'pseudo_id' pseudo_id(x, ...) ## Default S3 method: pseudo_id(x, na_last = TRUE, ...) ## S3 method for class 'factor' pseudo_id(x, ...)
x |
A vector of values |
... |
Additional arguments passed to methods |
na_last |
|
A pseudo_id
object where the integer
value of the vector
correspond to the position of the unique values in the attribute "uniques"
.
set.seed(42) (x <- sample(letters, 10, TRUE)) (pid <- pseudo_id(x)) attr(pid, "uniques")[pid]
set.seed(42) (x <- sample(letters, 10, TRUE)) (pid <- pseudo_id(x)) attr(pid, "uniques")[pid]
Quietly calls stop
quiet_stop()
quiet_stop()
None, called for side effects
Employs min()
and max()
. However, base::range()
, there is no argument
for removing Inf
values.
range2(x, na.rm = FALSE)
range2(x, na.rm = FALSE)
x |
A numeric (or character) vector (see Note in base::min) |
na.rm |
Logical, if |
A numeric
vector of length 2 of the minimum and maximum values,
respectively
x <- rep(1:1e5, 100) system.time(rep(range(x), 100)) system.time(rep(range2(x), 100)) x[sample(x, 1e5)] <- NA system.time(rep(range(x, na.rm = TRUE), 100)) system.time(rep(range2(x, na.rm = TRUE), 100))
x <- rep(1:1e5, 100) system.time(rep(range(x), 100)) system.time(rep(range2(x), 100)) x[sample(x, 1e5)] <- NA system.time(rep(range(x, na.rm = TRUE), 100)) system.time(rep(range2(x, na.rm = TRUE), 100))
Read a bib file into a data.frame
read_bib(file, skip = 0L, max_lines = NULL, encoding = "UTF-8")
read_bib(file, skip = 0L, max_lines = NULL, encoding = "UTF-8")
file |
File or connection |
skip |
The lines to skip |
max_lines |
The maximum number of lines to read |
encoding |
Assumed encoding of file (passed to |
Inspired and partially credited to bib2df::bib2df()
although this has no
dependencies outside of base functions and much quicker. This speed seems
to come from removing stringr
functions and simplifying a few *apply
functions.
This will also include as many categories as possible from the entry.
A data.frame
with each row as a bib entry and each column as a
field
?bib2df::bib2df
file <- "https://raw.githubusercontent.com/jmbarbone/bib-references/master/references.bib" bibdf <- read_bib(file, max_lines = 51L) if (package_available("tibble")) { tibble::as_tibble(bibdf) } else { head(bibdf) } if (package_available("bib2df") & package_available("bench")) { file <- system.file("extdata", "bib2df_testfile_3.bib", package = "bib2df") # Doesn't include the 'tidying' up foo <- function(file) { bib <- ("bib2df" %colons% "bib2df_read")(file) ("bib2df" %colons% "bib2df_gather")(bib) } bench::mark( read_bib = read_bib(file), bib2df = bib2df::bib2df(file), foo = foo(file), check = FALSE )[1:9] }
file <- "https://raw.githubusercontent.com/jmbarbone/bib-references/master/references.bib" bibdf <- read_bib(file, max_lines = 51L) if (package_available("tibble")) { tibble::as_tibble(bibdf) } else { head(bibdf) } if (package_available("bib2df") & package_available("bench")) { file <- system.file("extdata", "bib2df_testfile_3.bib", package = "bib2df") # Doesn't include the 'tidying' up foo <- function(file) { bib <- ("bib2df" %colons% "bib2df_read")(file) ("bib2df" %colons% "bib2df_gather")(bib) } bench::mark( read_bib = read_bib(file), bib2df = bib2df::bib2df(file), foo = foo(file), check = FALSE )[1:9] }
A simple implementation of recoding
recode_by(x, by, vals = NULL, mode = "any") recode_only(x, by, vals = NULL)
recode_by(x, by, vals = NULL, mode = "any") recode_only(x, by, vals = NULL)
x |
A vector to recode |
by |
A names vector ( |
vals |
An optional vector of values to use in lieu of a names in the
vector; this takes priority over |
mode |
passed to |
This can be comparable to dplyr::recode()
expect that the values are
arranged as new = old
rather than old = new
and allows for a separate
vector to be passed for new
.
recode_only()
will only recode the values matches in by
/val
.
The mode
is automatically set according to mode(x)
. This functions
more like base::replace()
but with extra features
A vector of values from x
recode_by(1:3, c(a = 1, b = 2)) recode_by(letters[1:3], c(`1` = "a", `2` = "b")) # will not guess mode recode_by(letters[1:3], c(`1` = "a", `2` = "b"), mode = "integer") # make as integer recode_by(letters[1:3], c("a", "b"), vals = 1:2) # or pass to vals recode_only(letters[1:3], c("zzz" = "a")) recode_only(letters[1:3], c(`1` = "a")) # returns as "1" recode_only(1:3, c("a" = 1)) # coerced to NA # Pass list for multiples recode_only(letters[1:10], list(abc = c("a", "b", "c"), ef = c("e", "f")))
recode_by(1:3, c(a = 1, b = 2)) recode_by(letters[1:3], c(`1` = "a", `2` = "b")) # will not guess mode recode_by(letters[1:3], c(`1` = "a", `2` = "b"), mode = "integer") # make as integer recode_by(letters[1:3], c("a", "b"), vals = 1:2) # or pass to vals recode_only(letters[1:3], c("zzz" = "a")) recode_only(letters[1:3], c(`1` = "a")) # returns as "1" recode_only(1:3, c("a" = 1)) # coerced to NA # Pass list for multiples recode_only(letters[1:10], list(abc = c("a", "b", "c"), ef = c("e", "f")))
Reindexes a data.frame with a reference
reindex( x, index = NULL, new_index, expand = c("intersect", "both"), sort = FALSE )
reindex( x, index = NULL, new_index, expand = c("intersect", "both"), sort = FALSE )
x |
A data.frame |
index |
The column name or number of an index to use; if |
new_index |
A column vector of the new index value |
expand |
Character switch to expand or keep only the values that intersect (none), all values in x or index, or retain all values found. |
sort |
Logical, if |
A data.frame
with rows of index
iris1 <- head(iris, 5) iris1$index <- 1:5 reindex(iris1, "index", seq(2, 8, 2)) reindex(iris1, "index", seq(2, 8, 2), expand = "both") # Using letters will show changes in rownames iris1$index <- letters[1:5] reindex(iris1, "index", letters[seq(2, 8, 2)]) reindex(iris1, "index", seq(2, 8, 2)) reindex(iris1, "index", seq(2, 8, 2), expand = "both")
iris1 <- head(iris, 5) iris1$index <- 1:5 reindex(iris1, "index", seq(2, 8, 2)) reindex(iris1, "index", seq(2, 8, 2), expand = "both") # Using letters will show changes in rownames iris1$index <- letters[1:5] reindex(iris1, "index", letters[seq(2, 8, 2)]) reindex(iris1, "index", seq(2, 8, 2)) reindex(iris1, "index", seq(2, 8, 2), expand = "both")
Remove NAs from a vector
remove_na(x) ## Default S3 method: remove_na(x) ## S3 method for class 'list' remove_na(x) ## S3 method for class 'factor' remove_na(x) ## S3 method for class 'fact' remove_na(x)
remove_na(x) ## Default S3 method: remove_na(x) ## S3 method for class 'list' remove_na(x) ## S3 method for class 'factor' remove_na(x) ## S3 method for class 'fact' remove_na(x)
x |
A vector of values |
remove_na.factor
will remove NA
values as identified by the levels()
or by the integer value of the level. factors
are recreated with all
NA
values and, if present, the NA
level
removed.
x
without values where is.na(x)
is TRUE
For factors, a new factor (ordered
if is.ordered(x)
)
remove_na(c(4, 1, 2, NA, 4, NA, 3, 2)) # removes based on levels remove_na(fact(c("b", NA, "a", "c"))) # removes based on values x <- as_ordered(c("b", "d", "a", "c")) x[2:3] <- NA str(remove_na(x))
remove_na(c(4, 1, 2, NA, 4, NA, 3, 2)) # removes based on levels remove_na(fact(c("b", NA, "a", "c"))) # removes based on values x <- as_ordered(c("b", "d", "a", "c")) x[2:3] <- NA str(remove_na(x))
Remove NULL results from a list
remove_null(x)
remove_null(x)
x |
A list |
The list x
without NULL
x <- list(a = letters[1:5], b = NULL, c = complex(3)) x remove_null(x)
x <- list(a = letters[1:5], b = NULL, c = complex(3)) x remove_null(x)
Rounds a number or vector of numbers by another
round_by(x, by = 1, method = c("round", "ceiling", "floor"), include0 = TRUE)
round_by(x, by = 1, method = c("round", "ceiling", "floor"), include0 = TRUE)
x |
A number or vector to round. |
by |
The number by which to round |
method |
An option to explicitly specify automatic rounding, ceiling, or floor |
include0 |
If |
A vector of doubles
of the same length of x
x <- seq(1, 13, by = 4/3) cbind( x, by_1 = round_by(x, 1), by_2 = round_by(x, 2), by_3 = round_by(x, 3) )
x <- seq(1, 13, by = 4/3) cbind( x, by_1 = round_by(x, 1), by_2 = round_by(x, 2), by_3 = round_by(x, 3) )
Bind a list of data.frames
row_bind(...)
row_bind(...)
... |
A list of |
A data.frame
combining all the rows from data.frame
s in ...
and all the columns, as they appear. An empty data.frame
with 0
columns
and 0
rows is returned if ...
has no length
dplyr::bind_rows()
base::rbind()
Implements Rscript
with system2
rscript(x, ops = NULL, args = NULL, ...)
rscript(x, ops = NULL, args = NULL, ...)
x |
An R file to run |
ops |
A character vector of options ( |
args |
A character vector of other arguments to pass |
... |
Additional arguments passed to |
A character
vector of the result from calling Rscript
via
system2()
Source a file and save as file
save_source(env = parent.frame(), file = mark_temp("Rds"), name = NULL)
save_source(env = parent.frame(), file = mark_temp("Rds"), name = NULL)
env |
The parent environment |
file |
The file to save the environment to |
name |
An optional name for the environment (mostly cosmetic) |
A source_env
/environment
object, created from env
Sets or removes names
set_names0(x, nm = x) names_switch(x)
set_names0(x, nm = x) names_switch(x)
x |
A vector of values |
nm |
A vector of names |
set_names0()
: x
with nm
values assigned to names (if x
is NULL
,
NULL
is returned)
remove_names()
: x
without names
names_switch()
: character
vector of equal length x
where names and
values are switched
This function can be used to evaluate an expression line-by-line to capture outputs, errors, messages, and evaluation time.
simpleTimeReport(title = NULL, expr, envir = parent.frame())
simpleTimeReport(title = NULL, expr, envir = parent.frame())
title |
The title to be printed |
expr |
The expression to run |
envir |
The environment from which to evaluate the |
Evaluate code and report on the time difference
A reported_results
/list
object containing results, outputs,
messages, warnings, and errors
simpleTimeReport("example", { print("1") Sys.sleep(1) warning("this is a warning") for (i in 1:5) { Sys.sleep(0.5) } sample(1e6, 1e6, TRUE) })
simpleTimeReport("example", { print("1") Sys.sleep(1) warning("this is a warning") for (i in 1:5) { Sys.sleep(0.5) } sample(1e6, 1e6, TRUE) })
Sort an object by another object
sort_by(x, by, ...)
sort_by(x, by, ...)
x |
A vector |
by |
Another vector |
... |
Additional arguments passed to |
The values of x
, resorted
l3 <- letters[1:3] sort_by(l3, c(3, 2, 1)) # make a factor object with the reversed order f <- factor(l3, levels = rev(l3)) sort_by(f, l3) sort_by(1:3, rev(l3))
l3 <- letters[1:3] sort_by(l3, c(3, 2, 1)) # make a factor object with the reversed order f <- factor(l3, levels = rev(l3)) sort_by(f, l3) sort_by(1:3, rev(l3))
Sort a vector by it's name
sort_names(x, numeric = FALSE)
sort_names(x, numeric = FALSE)
x |
A named vector of values |
numeric |
If |
x
sorted by its names()
Walk through files in a directory and output them. Files are sources in order of names
source_r_dir(dir, echo = FALSE, quiet = FALSE, ...) source_r_file(path, echo = FALSE, quiet = FALSE, ...)
source_r_dir(dir, echo = FALSE, quiet = FALSE, ...) source_r_file(path, echo = FALSE, quiet = FALSE, ...)
dir |
The location of your R scripts |
echo |
logical; if |
quiet |
Logical. Whether to print out a message for each file. |
... |
Additional arguments passed to |
path |
The location of the R file. |
None, called for side effects
Source an R script to an environment
source_to_env(x, ops = NULL)
source_to_env(x, ops = NULL)
x |
An R script |
ops |
Options to be passed to rscript |
Invisibly, and environment variable of the objects/results created
from x
Functions for extending sourcing features
ksource(file, ..., quiet = TRUE, cd = FALSE, env = parent.frame()) try_source(file, cd = FALSE, ...) try_ksource(file, ...)
ksource(file, ..., quiet = TRUE, cd = FALSE, env = parent.frame()) try_source(file, cd = FALSE, ...) try_ksource(file, ...)
file |
An R or Rmd file. |
... |
Additional arguments passed to |
quiet |
Logical; Determines whether to apply silence to |
cd |
Logical; if TRUE, the R working directory is temporarily changed to the directory containing file for evaluating |
env |
An environment determining where the parsed expressions are evaluated |
try_source()
will output an error message rather than completely preventing
the execution.
This can be useful for when a script calls on multiple, independent files to
be sourced and a single failure shouldn't prevent the entire run to fail as
well.
ksource()
: Invisibly, the result of calling source()
on the .R
file conversion of file
try_source()
, try_ksource()
: attempts of source()
and ksource()
but converts errors to warnings
Extract date from string
str_extract_date(x, format = "%Y-%m-%d") str_extract_datetime(x, format = "%Y-%m-%d %H%M%S")
str_extract_date(x, format = "%Y-%m-%d") str_extract_datetime(x, format = "%Y-%m-%d %H%M%S")
x |
A character vector |
format |
A date format to find |
A Date
(if found) or NA
str_extract_date("This is a file name 2020-02-21.csv") str_extract_date(c("This is a file name 2020-02-21.csv", "Date of 2012-06-15 here")) str_extract_date(c("This is a file name 2020-02-21.csv", "No date")) str_extract_date("Last saved 17 December 2019", format = "%d %B %Y") str_extract_datetime(c("2020-02-21 235033", "2012-12-12 121212")) str_extract_datetime("This is a file name 2020-02-21 235033.csv")
str_extract_date("This is a file name 2020-02-21.csv") str_extract_date(c("This is a file name 2020-02-21.csv", "Date of 2012-06-15 here")) str_extract_date(c("This is a file name 2020-02-21.csv", "No date")) str_extract_date("Last saved 17 December 2019", format = "%d %B %Y") str_extract_datetime(c("2020-02-21 235033", "2012-12-12 121212")) str_extract_datetime("This is a file name 2020-02-21 235033.csv")
Slice/split a string into multiple lines by the desired length of the line.
str_slice(x, n = 80L) str_slice_by_word(x, n = 80L)
str_slice(x, n = 80L) str_slice_by_word(x, n = 80L)
x |
A character vector |
n |
Integer, the length of the line split |
A character
vector
if (requireNamespace("stringi")) { x <- stringi::stri_rand_lipsum(1) str_slice(x) str_slice_by_word(x, n = 50L) }
if (requireNamespace("stringi")) { x <- stringi::stri_rand_lipsum(1) str_slice(x) str_slice_by_word(x, n = 50L) }
switch_params()
is a vectorized version of switch
switch_case()
uses a formula syntax to return the value to the right of the
tilde (~
) when x
is TRUE
switch_in_case()
is a special case of switch_case()
for match()
-ing x
in the values on the left to return the value on the right.
switch_params(x, ...) switch_in_case(x, ..., .default = NULL, .envir = parent.frame()) switch_case(..., .default = NULL, .envir = parent.frame())
switch_params(x, ...) switch_in_case(x, ..., .default = NULL, .envir = parent.frame()) switch_case(..., .default = NULL, .envir = parent.frame())
x |
A vector of values |
... |
Case evaluations (named for |
.default |
The default value if no matches are found in |
.envir |
The environment in which to evaluate the LHS of |
Switch with a list of params
A named vector of values of same length x
; or for switch_case
,
an unnamed vector of values matching the rhs of ...
Inspired from:
https://stackoverflow.com/a/32835930/12126576
https://github.com/tidyverse/dplyr/issues/5811
# by single switch_params(c("j", "m", "b"), j = 10, b = 2, m = 13) # match with TRUE switch_case( 1:10 == 9 ~ NA_integer_, 1:10 %% 3 == 0 ~ 1:10, 1:10 %% 4 == 0 ~ 11:20, 1:10 %% 5 == 0 ~ 21:30, 1:10 %% 2 == 0 ~ 31:40, .default = -1L ) # match within a vector switch_in_case( c(1, 2, 12, 4, 20, 21), 1:10 ~ 1, 11:20 ~ 2 ) switch_in_case( c("a", "b", "d", "e", "g", "j"), letters[1:3] ~ "a", letters[5:6] ~ "e" ) use_these <- c(1, 3, 2, 5) switch_in_case( 1:10, use_these ~ TRUE, .default = FALSE ) ne <- new.env() ne$use_these2 <- use_these # error try(switch_in_case( 1:10, use_these2 ~ TRUE )) switch_in_case( 1:10, use_these2 ~ TRUE, .envir = ne ) switch_in_case( seq.int(1, 60, 6), 1:10 ~ "a", 11:20 ~ "b", c(22, 24, 26) ~ "c", 30:Inf ~ "d" ) # Use functions switch_in_case( 1:6, c(1, 3, 5) ~ exp, c(2, 4) ~ log )
# by single switch_params(c("j", "m", "b"), j = 10, b = 2, m = 13) # match with TRUE switch_case( 1:10 == 9 ~ NA_integer_, 1:10 %% 3 == 0 ~ 1:10, 1:10 %% 4 == 0 ~ 11:20, 1:10 %% 5 == 0 ~ 21:30, 1:10 %% 2 == 0 ~ 31:40, .default = -1L ) # match within a vector switch_in_case( c(1, 2, 12, 4, 20, 21), 1:10 ~ 1, 11:20 ~ 2 ) switch_in_case( c("a", "b", "d", "e", "g", "j"), letters[1:3] ~ "a", letters[5:6] ~ "e" ) use_these <- c(1, 3, 2, 5) switch_in_case( 1:10, use_these ~ TRUE, .default = FALSE ) ne <- new.env() ne$use_these2 <- use_these # error try(switch_in_case( 1:10, use_these2 ~ TRUE )) switch_in_case( 1:10, use_these2 ~ TRUE, .envir = ne ) switch_in_case( seq.int(1, 60, 6), 1:10 ~ "a", 11:20 ~ "b", c(22, 24, 26) ~ "c", 30:Inf ~ "d" ) # Use functions switch_in_case( 1:6, c(1, 3, 5) ~ exp, c(2, 4) ~ log )
This transposes a data.frame with t()
but transforms back into a data.frame
with column and row names cleaned up. Because the data types may be mixed
and reduced to characters, this may only be useful for a visual viewing of
the data.frame.
t_df(x, id = NULL)
t_df(x, id = NULL)
x |
A data.frame |
id |
No longer used |
Transposes a data.frame as a data.frame
A transposed data.frame
with columns ("colname"
, "row_1"
, ...,
for each row in x
.
x <- data.frame(col_a = Sys.Date() + 1:5, col_b = letters[1:5], col_c = 1:5) t_df(x)
x <- data.frame(col_a = Sys.Date() + 1:5, col_b = letters[1:5], col_c = 1:5) t_df(x)
Tables out whether data are NAs are not
tableNA(..., .list = FALSE)
tableNA(..., .list = FALSE)
... |
one or more objects which can be interpreted as factors
(including numbers or character strings), or a |
.list |
Logical, if |
All data are checked with is.na()
and the resulting TRUE
or FALSE
is
are tabulated.
table()
returns a contingency table, an object of
class "table"
, an array of integer values.
Note that unlike S the result is always an array
, a 1D
array if one factor is given.
as.table
and is.table
coerce to and test for contingency
table, respectively.
The as.data.frame
method for objects inheriting from class
"table"
can be used to convert the array-based representation
of a contingency table to a data frame containing the classifying
factors and the corresponding entries (the latter as component
named by responseName
). This is the inverse of xtabs
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
tabulate
is the underlying function and allows finer
control.
Use ftable
for printing (and more) of
multidimensional tables. margin.table
,
prop.table
, addmargins
.
addNA
for constructing factors with NA
as
a level.
xtabs
for cross tabulation of data frames with a
formula interface.
x <- list( a = c(1, 2, NA, 3), b = c("A", NA, "B", "C"), c = as.Date(c("2020-01-02", NA, NA, "2020-03-02")) ) tableNA(x) # entire list tableNA(x, .list = TRUE) # counts for each tableNA(x[1], x[2]) tableNA(x[1], x[2], x[3]) # equivalent ot tableNA(x, .list = TRUE)
x <- list( a = c(1, 2, NA, 3), b = c("A", NA, "B", "C"), c = as.Date(c("2020-01-02", NA, NA, "2020-03-02")) ) tableNA(x) # entire list tableNA(x, .list = TRUE) # counts for each tableNA(x[1], x[2]) tableNA(x[1], x[2], x[3]) # equivalent ot tableNA(x, .list = TRUE)
Grammatical correctness
that(x, arr.ind = FALSE, useNames = TRUE)
that(x, arr.ind = FALSE, useNames = TRUE)
x |
a |
arr.ind |
logical; should array indices be returned
when |
useNames |
logical indicating if the value of |
See fortunes::fortune(175)
.
see base::which()
Convert a vector to boolean/logical
to_boolean(x, ...) ## S3 method for class 'logical' to_boolean(x, ...) ## S3 method for class 'numeric' to_boolean(x, true = 1L, false = 0L, ...) ## S3 method for class 'character' to_boolean(x, true = NULL, false = NULL, ...) ## S3 method for class 'factor' to_boolean(x, true = NULL, false = NULL, ...)
to_boolean(x, ...) ## S3 method for class 'logical' to_boolean(x, ...) ## S3 method for class 'numeric' to_boolean(x, true = 1L, false = 0L, ...) ## S3 method for class 'character' to_boolean(x, true = NULL, false = NULL, ...) ## S3 method for class 'factor' to_boolean(x, true = NULL, false = NULL, ...)
x |
A vector of values |
... |
Additional arguments passed to methods |
true |
A vector of values to convert to |
false |
A vector of values to convert to |
A logical
vector of equal length as x
Converts a column to row names
to_row_names(data, row_names = 1L)
to_row_names(data, row_names = 1L)
data |
A data.frame |
row_names |
The numeric position of the column. |
A data.frame
x <- data.frame( a = 1:4, b = letters[1:4] ) to_row_names(x) to_row_names(x, "b")
x <- data.frame( a = 1:4, b = letters[1:4] ) to_row_names(x) to_row_names(x, "b")
Search for #`` TODO
tags
todos( pattern = NULL, path = ".", force = getOption("mark.todos.force"), ext = getOption("mark.todos.ext"), ignore = NULL, ... ) fixmes( pattern = NULL, path = ".", force = getOption("mark.todos.force"), ext = getOption("mark.todos.ext"), ignore = NULL, ... )
todos( pattern = NULL, path = ".", force = getOption("mark.todos.force"), ext = getOption("mark.todos.ext"), ignore = NULL, ... ) fixmes( pattern = NULL, path = ".", force = getOption("mark.todos.force"), ext = getOption("mark.todos.ext"), ignore = NULL, ... )
pattern |
A character string containing a regular expression to filter
for comments after tags; default |
path |
Where to search for the todos. If this is a directory, paths
matching the |
force |
If |
ext |
A vector of file extensions to search for todos. Ignored when
|
ignore |
A regular expression for files to ignore. Ignored if |
... |
Additional parameters passed to |
Searches for TODO
comments in files. Extensions with md
, Rmd
,
and qmd
specifically search for a <-- TODO * -->
string, whereas
everything else is found with # TODO
.
NULL
if none are found, otherwise a data.frame
with the line
number, file name, and TODO comment.
## Not run: file <- tempfile() writeLines(c( "# TODO make x longer", "x <- 1:10", "length(x)", "# TODO add another example", "# FIXME This is a fixme" ), file) todos(path = file) todos("example", path = file) fixmes(path = file) file.remove(file) ## End(Not run)
## Not run: file <- tempfile() writeLines(c( "# TODO make x longer", "x <- 1:10", "length(x)", "# TODO add another example", "# FIXME This is a fixme" ), file) todos(path = file) todos("example", path = file) fixmes(path = file) file.remove(file) ## End(Not run)
Try an expression a set number of times
tryn(expr, n = 10, silent = TRUE)
tryn(expr, n = 10, silent = TRUE)
expr |
expression to evaluate |
n |
number of attempts until error |
silent |
whether to suppress warnings |
result of expr
foo <- function() stop("I added an error") try(tryn(n = 10, foo()))
foo <- function() stop("I added an error") try(tryn(n = 10, foo()))
Drops duplicated rows
unique_rows(data, cols = NULL, from_last = FALSE, invert = FALSE)
unique_rows(data, cols = NULL, from_last = FALSE, invert = FALSE)
data |
A |
cols |
Columns to compare against; when |
from_last |
When |
invert |
If |
data
will duplicates removes
df <- quick_dfl( i = 1:4, a = rep(1:2, 2L), b = rep("a", 4L), ) unique_rows(df, 2:3) unique_rows(df, c("a", "b"), from_last = TRUE, invert = TRUE)
df <- quick_dfl( i = 1:4, a = rep(1:2, 2L), b = rep("a", 4L), ) unique_rows(df, 2:3) unique_rows(df, c("a", "b"), from_last = TRUE, invert = TRUE)
Unlist without unique names; combine names for unique values
unlist0(x) squash_vec(x, sep = ".")
unlist0(x) squash_vec(x, sep = ".")
x |
A vector of values |
sep |
A separation for combining names |
unlist0()
is much like unlist()
expect that name are not made to be
unique.
squash_vec()
works differently
unlist0()
: a vector with the possibility of non-unique names
squash_vec()
: A vector of unique values and names
x <- list(a = 1:3, b = 2, c = 2:4) y <- c(a = 1, b = 1, c = 1, d = 2, e = 3, f = 3) # unlist0() doesn't force unique names unlist(x) # names: a1 a2 a3 b c1 c2 c3 unlist0(x) # names: a a a b c c c unlist0(y) # no change # squash_vec() is like the inverse of unlist0() because it works on values squash_vec(x) squash_vec(y)
x <- list(a = 1:3, b = 2, c = 2:4) y <- c(a = 1, b = 1, c = 1, d = 2, e = 3, f = 3) # unlist0() doesn't force unique names unlist(x) # names: a1 a2 a3 b c1 c2 c3 unlist0(x) # names: a a a b c c c unlist0(y) # no change # squash_vec() is like the inverse of unlist0() because it works on values squash_vec(x) squash_vec(y)
Adds author to description
use_author(author_info = find_author())
use_author(author_info = find_author())
author_info |
Author information as a named list |
Only valid for a single author.
None, called for side effects
Paste and combine
paste_c(x, y, collate = TRUE, sep = "") paste_combine(..., collate = TRUE, sep = "") collapse0(..., sep = "")
paste_c(x, y, collate = TRUE, sep = "") paste_combine(..., collate = TRUE, sep = "") collapse0(..., sep = "")
x , y , ...
|
Vectors to paste and/or combine |
collate |
Logical; |
sep |
A character string to separate terms |
A character
vector
x <- letters[1:5] y <- 1:3 z <- month.abb[c(1, 12)] paste_combine(x, y) paste_combine(x, y, z) paste_combine(x, y, z, sep = ".") paste_combine(x, y, sep = "_") paste_combine(x, y, collate = FALSE) collapse0(list(1:3, letters[1:3]), 5:7, letters[5:7]) collapse0(1:3, letters[5:7], sep = "_")
x <- letters[1:5] y <- 1:3 z <- month.abb[c(1, 12)] paste_combine(x, y) paste_combine(x, y, z) paste_combine(x, y, z, sep = ".") paste_combine(x, y, sep = "_") paste_combine(x, y, collate = FALSE) collapse0(list(1:3, letters[1:3]), 5:7, letters[5:7]) collapse0(1:3, letters[5:7], sep = "_")
Wrappers for vapply
vap_int(.x, .f, ..., .nm = FALSE) vap_dbl(.x, .f, ..., .nm = FALSE) vap_chr(.x, .f, ..., .nm = FALSE) vap_lgl(.x, .f, ..., .nm = FALSE) vap_cplx(.x, .f, ..., .nm = FALSE) vap_date(.x, .f, ..., .nm = FALSE)
vap_int(.x, .f, ..., .nm = FALSE) vap_dbl(.x, .f, ..., .nm = FALSE) vap_chr(.x, .f, ..., .nm = FALSE) vap_lgl(.x, .f, ..., .nm = FALSE) vap_cplx(.x, .f, ..., .nm = FALSE) vap_date(.x, .f, ..., .nm = FALSE)
.x |
A vector of values |
.f |
A function to apply to each element in vector |
... |
Additional arguments passed to |
.nm |
Logical, if |
These are simply wrappers for base::vapply()
to shorten lines.
Each function is designed to use specific vector types:
integer
double
character
logical
complex
Date
A vector of type matching the intended value in the function name.
Transforms a vector (named) to a data.frame
vector2df(x, name = "name", value = "value", show_NA)
vector2df(x, name = "name", value = "value", show_NA)
x |
A vector of values. |
name , value
|
Character strings for the name and value columns |
show_NA |
Ignored; will trigger a warning if set |
A data.frame
with name
(optional) and value
columns
Reset par() after running
with_par(..., ops = NULL)
with_par(..., ops = NULL)
... |
Code to be evaluated |
ops |
A named list to be passed to |
Invisibly, the result of ...
with_par( plot(lm(Sepal.Length ~ Sepal.Width, data = iris)), plot(lm(Petal.Length ~ Petal.Width, data = iris)), ops = list(mfrow = c(2, 4)) )
with_par( plot(lm(Sepal.Length ~ Sepal.Width, data = iris)), plot(lm(Petal.Length ~ Petal.Width, data = iris)), ops = list(mfrow = c(2, 4)) )
Compare a vector within (between) other values
between_more(x, left, right, type = c("gele", "gel", "gle", "gl")) within(x, left = NULL, right = NULL, bounds = c("[]", "[)", "(]", "()"))
between_more(x, left, right, type = c("gele", "gel", "gle", "gl")) within(x, left = NULL, right = NULL, bounds = c("[]", "[)", "(]", "()"))
x |
A numeric vector of values |
left , right
|
Boundary values. For |
type |
Abbreviation for the evaluation of |
bounds |
Boundaries for comparisons of |
type``,
bounds“ can be one of the below:
is greater than (>)
greater than or equal to (>=)
less than (<)
less than or equal to (<=)
Note: between_more()
may be deprecated in the future in favor of just
within()
A logical vector
between_more(2:10, 2, 10, "gl") within(2:10, 2, bounds = "()") between_more(10, 2, 10, "gle") within(2:10, bounds = "(]") within(1:5, c(3, 3, 2, 2, 1), 5)
between_more(2:10, 2, 10, "gl") within(2:10, 2, bounds = "()") between_more(10, 2, 10, "gle") within(2:10, bounds = "(]") within(1:5, c(3, 3, 2, 2, 1), 5)
Returns the function call you are within
within_call() within_fun() outer_call(n = 0) outer_fun(n = 0)
within_call() within_fun() outer_call(n = 0) outer_fun(n = 0)
n |
The number of calls to move out from |
The string of the call/function
Write file with md5 hash check
write_file_md5( x, path = NULL, method = mark_write_methods(), overwrite = NA, quiet = FALSE, encoding = "UTF-8", compression = getOption("mark.compress.method", mark_compress_methods()), ... ) mark_write_methods() mark_compress_methods()
write_file_md5( x, path = NULL, method = mark_write_methods(), overwrite = NA, quiet = FALSE, encoding = "UTF-8", compression = getOption("mark.compress.method", mark_compress_methods()), ... ) mark_write_methods() mark_compress_methods()
x |
An object to write to file |
path |
The file or connection to write to (dependent on part by method) |
method |
The method of saving the file. When |
overwrite |
When |
quiet |
When |
encoding |
The encoding to use when writing the file. |
compression |
The compression method to use when writing the file. |
... |
Additional arguments passed to the write function. |
write_file_md5()
: x
, invisibly. When path
is not the stdout()
, x
is returned with the attribute "path"
set to the result of
file_copy_md5()
.
mark_write_methods()
: A list of applicable methods and their aliases
mark_compress_methods()
: A character vector of applicable compression
methods
options()
mark.compress.method
: compression method to use when writing files
mark.list.hook
: when a data.frame
contains a list
column, this
function is applied to each element of the list. The default "auto"
uses toJSON()
if the package jsonlite
is available, otherwise
# just writes to stdout() df <- data.frame(a = 1, b = 2) write_file_md5(df) temp <- tempfile() write_file_md5(df, temp) # new write_file_md5(df, temp) # same df$c <- 3 write_file_md5(df, temp) # changes fs::file_delete(temp)
# just writes to stdout() df <- data.frame(a = 1, b = 2) write_file_md5(df) temp <- tempfile() write_file_md5(df, temp) # new write_file_md5(df, temp) # same df$c <- 3 write_file_md5(df, temp) # changes fs::file_delete(temp)