| Type: | Package |
| Title: | Miscellaneous, Analytic R Kernels |
| Version: | 0.8.3 |
| Maintainer: | Jordan Mark Barbone <jmbarbone@gmail.com> |
| Description: | Miscellaneous functions and wrappers for development in other packages created, maintained by Jordan Mark Barbone. |
| License: | MIT + file LICENSE |
| URL: | https://CRAN.R-project.org/package=mark,https://github.com/jmbarbone/mark,https://jmbarbone.github.io/mark/ |
| BugReports: | https://github.com/jmbarbone/mark/issues |
| Encoding: | UTF-8 |
| Depends: | R (≥ 3.6) |
| Imports: | cli, fs (≥ 1.6.2), fuj (≥ 0.2.1), magrittr (≥ 2.0.1),rlang, stats (≥ 3.6), tools (≥ 3.6), utils (≥ 3.6) |
| Suggests: | bench (≥ 1.1.1), bib2df (≥ 1.1.1), crayon (≥ 1.3.4), covr(≥ 3.5.1), desc (≥ 1.3.0), dplyr (≥ 1.0.6), echo (≥ 0.1.0),graphics (≥ 3.6), haven, knitr (≥ 1.30), rcmdcheck (≥1.3.3), stringi (≥ 1.5.3), spelling (≥ 2.2), testthat (≥3.0.0), tibble (≥ 3.0.4), waldo (≥ 0.2.5), withr (≥ 2.3.0),xopen, yaml, jsonlite, arrow (≥ 16.1.0), readMDTable (≥0.2.0), clipr (≥ 0.8.0) |
| RoxygenNote: | 7.3.2 |
| Config/testthat/edition: | 3 |
| Config/testthat/parallel: | true |
| Language: | en-US |
| NeedsCompilation: | no |
| Packaged: | 2025-04-23 03:17:49 UTC; jordan |
| Author: | Jordan Mark Barbone |
| Repository: | CRAN |
| Date/Publication: | 2025-04-23 03:40:02 UTC |
mark
Description
Miscellaneous, Analytic R Kernels
Author(s)
Maintainer: Jordan Mark Barbonejmbarbone@gmail.com (ORCID) [copyright holder]
See Also
Useful links:
Report bugs athttps://github.com/jmbarbone/mark/issues
Add file timestamp
Description
Adds a timestamp to a file
Usage
add_file_timestamp( x, ts = Sys.time(), format = "%Y-%m-%d %H%M%S", sep = " ")Arguments
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 |
Value
The full name paths with the appended time stamp
Examples
file1 <- tempfile(fileext = ".txt")file2 <- tempfile()add_file_timestamp(file1)add_file_timestamp(file2)file.remove(file1, file2)Identical extensions
Description
Extensions for the use ofbase::identical()
Usage
are_identical(..., params = NULL)Arguments
... | Vectors of values to compare, element-wise of equal length |
params | Additional params (as a named list of arguments forbase::identical) |
Value
Alogical vector ofTRUE/FALSE of equal length of each...vector
Examples
x <- y <- z <- 1:5y[2] <- 3Lz[5] <- NA_integer_identical(x, y) # compare entire vectorare_identical(x, y) # element-wiseare_identical(x, y, z) # 3 or more vectorsArray extract
Description
Extract dimensions from an array
Usage
array_extract(.arr, ..., default = "1")Arguments
.arr | An array |
... | A named list by array dimension number and the value |
default | The default dimension index |
Value
A value from the arrayarr
Examples
x <- array(rep(NA, 27), dim = c(3, 3, 3))x[1, 2, 3] <- TRUEx[1, 2, 3]xarray_extract(x, `2` = 2, `3` = 3)Ordered
Description
As ordered
Usage
as_ordered(x)## Default S3 method:as_ordered(x)Arguments
x | A vector of values |
Details
Simple implementation ofordered. Ifx isordered it issimply returned. Ifx is afactor theordered class is added.Otherwise,x is made into afactor withfact() and then theordered class is added. Unlike justfact,ordered will replace theNAlevels withNA_integer_ to work appropriately with other functions.
Value
Anordered vector
See Also
Other factors:char2fact(),drop_levels(),fact(),fact2char(),fact_na()
Examples
x <- c("a", NA, "b")x <- fact(x)str(x) # NA is 3Ly <- xclass(y) <- c("ordered", class(y))max(y)max(y, na.rm = TRUE) # returns NA -- bad# as_ordered() removes the NA levelx <- as_ordered(x)str(x)max(x, na.rm = TRUE) # returns b -- correctAlpha base
Description
Base 26 conversion with letters
Usage
base_alpha(x, base = 26)Arguments
x | A string of letters. Non characters are removed. |
base | A numeric |
Value
A vector of integers
Examples
base_alpha("AB")base_alpha("XFD")base_alpha(c("JMB", "Jordan Mark", "XKCD"))sum(base_alpha(c("x", "k", "c", "d")))Base N conversion
Description
Convert between base numbers
Usage
base_n(x, from = 10, to = 10)Arguments
x | A vector of integers |
from,to | An integer base to convert to and from; |
Value
The A vector of integers converted from basefrom to baseto
Examples
base_n(c(24, 22, 16), from = 7)Blank values
Description
Detectblank values; select, remove columns that are entirelyblank
Usage
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)Arguments
x | An object, or |
na_blank | Logical, if |
ws | Logical, when |
names | Logical, if |
Details
Blank values are values that do not contain any text
Value
is_blank()alogicalvector indicatingblank elements inxselect_blank_cols()xwith only columns that are allblankremove_blank_cols()xwithout columns of onlyblankis_blank_cols()a logical vector:TRUEall rows of column areblank,otherwiseFALSE
Character to factor
Description
Converts characters to factors
Usage
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)Arguments
x | A vector of characters |
n | The limit to the number of unique values for the factor |
See Also
Other factors:as_ordered(),drop_levels(),fact(),fact2char(),fact_na()
Check options
Description
For each name inx checks the current option value and reports if thereis a difference in amessage. This does not change the options
Usage
checkOptions(x)Arguments
x | A named list of new options |
Details
Checks and reports on options
Value
Invisible, a list of the current options fromoptions()
Examples
op <- options()x <- list(width = -20, warning.length = 2, probably_not_a_real_option = 2)checkOptions(x)# pointless, but shows that no messages are givenidentical(options(), checkOptions(options()))options(op)Character split
Description
Split apart a string by each character
Usage
chr_split(x)Arguments
x | A vector of strings to split |
Value
Acharacter vector of lengthnchar(x)
Examples
chr_split("split this")Write to and read from the clipboard
Description
Wrappers for working with the clipboard
Usage
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()Arguments
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 rownames of |
method | Method switch for loading the clipboard |
Details
As these functions rely onclipr::read_clip() andutils::writeClipboard() they are only available for Windows 10. For copyingand pasting floats, there may be some rounding that can occur.
Value
write_clipboard() None, called for side effectsread_clipboard()Either a vector,data.frame, ortibble depending on themethod chosen.Unlikeutils::readClipboard(), an empty clipboard value returnsNA ratherthan""
Examples
# Will only run on windowsif (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}Complete cases
Description
Return completed cases of a data.frame
Usage
complete_cases(data, cols = NULL, invert = FALSE)Arguments
data | A data.frame |
cols | Colnames or numbers to remove |
invert | Logical, if |
Value
Adata.frame
Examples
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 rowscomplete_cases(x, "a")complete_cases(x, "b")complete_cases(x, "c")Count observations by unique values
Description
Variables will be return by the order in which they appear. Even factors areshown by their order of appearance in the vector.
There are 2 methods for counting vectors. Thedefault method usesbase::tabulate() (the workhorse forbase::table() with a call topseudo_id() to transform all inputs into integers. Thelogical methodcountsTRUE,FALSE andNA values, which is much quicker.
Usage
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")Arguments
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 |
Details
Get counts or proportions of unique observations in a vector or columns in adata.frame
Value
A named vector ofintegers ordoubles (forcounts, andprops,respectively) ordata.frame with columns for each column chosen and the.name chosen for the summary
Examples
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)Partial dates
Description
Derive a date vector from a partial date string
Usage
date_from_partial( x, format = "ymd", method = c("min", "max"), year_replacement = NA_integer_)Arguments
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 earliestpossible date ( |
year_replacement | (Default: |
Details
Takes a character as an argument and attempts to create a date object whenpart of the date string is missing.
Value
A vector ofDates
Examples
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))Depth
Description
Functions to extract the 'depth' of an object
Usage
depth(x, ...)## Default S3 method:depth(x, ...)## S3 method for class 'list'depth(x, ...)Arguments
x | An object |
... | Possible additional arguments passed to methods (not in use) |
Details
This function does not count an empty lists (list()) as a level orNULLobjects.
Value
A singleinteger
Examples
a <- c(1, 2, 3)depth(a) # Vectors are 1Lb <- list(a = 1, b = list(list(1)))depth(b)Details an object
Description
Provides details about an object
Usage
detail(x, ...)## Default S3 method:detail(x, factor_n = 5L, ...)## S3 method for class 'data.frame'detail(x, factor_n = 5L, ...)Arguments
x | An object |
... | Additional arguments passed to methods |
factor_n | An |
Examples
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)Diff time wrappers
Description
Wrappers for computing diff times
Usage
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, ...)Arguments
x,y | Vectors of times |
method | A method to report the difference in units of time (seeUnits section) |
tzx,tzy | time zones (seeTime zones section) |
... | Additional arguments passed to |
Details
A few significant differences exist with these functions * The classof the object returned is no longerdifftime (but does print) with thedifftime method. This makes the exporting process easier as the datawill not have to be converted back tonumeric *difftime() computes thedifference oftime1 -time2, but the inverse feels a bit more nature:time difference fromx toy * Additional units can be used (detailedbelow) * Differences can be sensitive to time zones if time zones arepassed to thetz parameter as a character vector
Value
Adiff_time vector, object
Units
Units can be used beyond those available inbase::difftime(). Some of these use assumptions in how units of timeshould be standardized and can be changed in the corresponding options. Anyof these can be calculated withbase::difftime() through usingunits = "days" but thedtime class will print out with these specifications intothe console for less potential confusion.
- months
Months by number of days
mark.days_in_month(defaults:30)- years
Years by number of days
mark.days_in_year(defaults:365)- dyears
Years by number of days
mark.days_in_year(defaults:365) (same asyears)- myears
Years by number of days in a month
mark.days_in_month(defaults:30)- wyears
Years by number of weeks in a year
mark.weeks_in_year(defaults:52)
Time zones
Time zones can be passed as either a numeric vector ofGMT/UTC offsets (the number of seconds from GMT) or as a character vector.If the letter, these need to conform with values frombase::OlsonNames().
A default timezone can be set withoptions(mark.default_tz = .). Thevalue can either be a numeric
Drop levels
Description
Drop unused levels of a factor
Usage
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, ...)Arguments
x | A |
... | Additional arguments passed to methods (not used) |
See Also
Other factors:as_ordered(),char2fact(),fact(),fact2char(),fact_na()
Parse and evaluate text
Description
A wrapper for eval(parse(text = .))
Usage
ept(x, envir = parent.frame())Arguments
x | A character string to parse |
envir | The environment in which to evaluate the code |
Value
The evaluation ofx after parsing
Evaluate a Named Chunk
Description
Evaluate a named chunk from an Rmd file.
Usage
eval_named_chunk(rmd_file, label_name)Arguments
rmd_file | Absolute path to rmd file |
label_name | Name of label |
Value
The value from the evaluated code chunk
Examples
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] TRUEfile.remove(temp_rmd)## End(Not run)Expands a vector
Description
Expands vector x by y
Usage
expand_by(x, y, expand = c("x", "y", "intersect", "both"), sort = FALSE)Arguments
x,y | Vectors |
expand | Character switch to expand or keep only the values thatintersect, all values in x or y, or retain all values found. |
sort | Logical, if |
Value
A vector with expanded
Examples
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")Factor
Description
Quickly create a factor
Usage
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)Arguments
x | A vector of values |
Details
fact() can be about 5 times quicker thanfactor() oras.factor() as it doesn't bother sorting the levels for non-numeric dataor have other checks or features. It simply converts a vector to a factorwith all unique values as levels withNAs included.
fact.factor() will perform several checks on a factor to includeNAlevels and to check if the levels should be reordered to conform with theother methods. Thefact.fact() method simple returnsx.
Value
A vector of equal length ofx with classfact andfactor. Ifx wasordered, that class is added in between.
level orders
The order of the levels may be adjusted to these rules depending on the classofx:
characterThe order of appearance
numeric/integer/Date/POSIXtBy the numeric order
logicalAs
TRUE,FALSE, thenNAif presentfactorNumeric if levels can be safely converted, otherwise asthey are
See Also
Other factors:as_ordered(),char2fact(),drop_levels(),fact2char(),fact_na()
Factor to character
Description
Convert factor columns to characters in adata.frame
Usage
fact2char(data, threshold = 10)Arguments
data | A |
threshold | A threshold for the number of levels to be met/exceeded fortransforming into a character |
Value
Thedata.framedata with factors converted by the rule above
See Also
Other factors:as_ordered(),char2fact(),drop_levels(),fact(),fact_na()
fact withNA
Description
IncludedNA values intofact()
Usage
fact_na(x, remove = FALSE)Arguments
x | A |
remove | If |
Details
This re-formats thex value so thatNAs are found immediately within theobject rather than accessed through its attributes.
Value
Afact vector
See Also
Other factors:as_ordered(),char2fact(),drop_levels(),fact(),fact2char()
Fact reverse levels
Description
Reverse the levels of afact
Usage
fact_reverse(x)Arguments
x | A |
Factor Expand by Sequence
Description
Expands an ordered factor from one level to another
Usage
fct_expand_seq( x, min_lvl = min(x, na.rm = TRUE), max_lvl = max(x, na.rm = TRUE), by = 1L)Arguments
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 |
Details
Defaults formin_lvl andmax_lvl are the minimum and maximumlevels in the ordered vectorx.
Value
Anordered vector
Examples
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 3fct_expand_seq(x, "g", "t", 3L) # same as above# from the first inherit level to the last observedfct_expand_seq(x, min(levels(x)))File copy with md5 hash check
Description
File copy with md5 hash check
Usage
file_copy_md5(path, new_path, overwrite = NA, quiet = FALSE)Arguments
path | A character vector of one or more paths. |
new_path | A character vector of paths to the new locations. |
overwrite | When |
quiet | When |
Value
The path(s) of the new file(s), invisibly. Whenoverwrite isNA, the paths will be returned with two addition attributes,"changed",a logical vector indicating whether the file was changed (NA for when thefile is new), and"md5sum", a list of the md5sums of the old and new md5sums.
File information utils
Description
Other utility functions for dealing with files
Usage
newest_file(x)newest_dir(x)oldest_file(x)oldest_dir(x)largest_file(x)smallest_file(x)Arguments
x | A vector of file paths |
Value
A full file path
File name
Description
Basename of file without extension
Usage
file_name(x, compression = FALSE)Arguments
x | character vector giving file paths. |
compression | logical: should compression extension ‘.gz’,‘.bz2’ or ‘.xz’ be removed first? |
Value
The file name of the path without the extension
Open a file using windows file associations
Description
Opens the given files(s)
Usage
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)Arguments
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 arereturned (following Unix-style visibility, that is files whose name doesnot start with a dot). If TRUE, all file names will be returned. |
negate | Logical, if |
basename | If |
Details
open_file is an alternative toshell.exec() that can take takemultiple files.list_files andlist_dirs are mostly wrappers forfs::dir_ls() with preferred defaults and pattern searching on the full filepath.
file_open is simply an alias.
Value
open_file(),shell_exec(): A logical vector whereTRUEsuccessfullyopened,FALSEdid not andNAdid not try to open (file not found)list_files(),list_dirs(): A vector of full paths
Fizz Buzz
Description
For when someone asked you to do something you've done before, you can arguethat the quickest way to do it is to just take the work someone else did andutilize that. No reason to reinvent the wheel.
Usage
fizzbuzz(n, show_numbers = TRUE)fizzbuzz_lazy(n).fizzbuzz_vectorArguments
n | The number of numbers |
show_numbers | If |
Format
An object of classcharacter of length 1000000.
Details
Multiples of3 are shown as"Fizz"; multiples of5 as"Buzz";multiple of both (i.e.,15) are"FizzBuzz".fizzbuzz_lazy() subsets the.fizzbuzz_vector object, which is a solutionwith default parameters up to1e6
Value
Acharacter vector of1, 2, Fizz, 3, Buzz, etc
Examples
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))}Get recent directory by number name
Description
Finds the directory where the number is the greatest. This can be useful forwhen folders are created as run IDs.
Usage
get_dir_max_number(x)Arguments
x | The directory to look in |
Value
A full path to a directory
Get recent directory by date
Description
Looks at the directories and assumes the date
Usage
get_dir_recent_date(x = ".", dt_pattern = NULL, dt_format = NULL, all = FALSE)Arguments
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 |
Value
A full path to a directory
Get recent directory
Description
Finds the recent subdirectory in a directory.
Usage
get_recent_dir(x = ".", ...)Arguments
x | The root directory |
... | Additional arguments passed to |
Value
The full path of the most recent directory
Get recent file
Description
A function where you can detect the most recent file from a directory.
Usage
get_recent_file(x, exclude_temp = TRUE, ...)Arguments
x | The directory in which to search the file |
exclude_temp | Logical, if |
... | Additional arguments passed to |
Value
The full name of the most recent file from the stated directory
Get and bump version
Description
Will read theDESCRIPTION file and to get and adjust theversion
bump_date_version() will not check if the version is actually a date. Whenthe current version is the same as today's date(equal by character strings)it will append a.1.
Usage
get_version()bump_version(version = NULL)bump_date_version(version = NULL)update_version(version = NULL, date = FALSE)Arguments
version | A new version to be added; default of |
date | If |
Details
Get and bump package version for dates
Value
get_version(): A package_versionbump_version(): None, called for its side-effectsbump_date_version(): None, called for its side-effectsupdate_version(): None, called for its side-effects
Wildcard globbing
Description
Helper function for globbing character vectors
Usage
glob(x, pattern = NULL, value = TRUE, ...)Arguments
x | A vector of characters |
pattern | Wildcard globbing pattern |
value,... | Additional parameters passed to |
Examples
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*")Handlers
Description
Catch and report handlers
Usage
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)Arguments
x | A vector |
FUN | A function |
... | Additional params passed to |
.null | Logical, if |
Details
These functions can be used to catch whether an evaluation willreturn an error or warning without raising.
Value
Thehas_*() functions will returnTRUE/FALSE for if the handleris found in the execution of the code. Theget_*() functions provide thetext of the message
References
Function forcatching has been adapted fromhttps://stackoverflow.com/a/4952908/12126576
Examples
has_warning(c(1, "no"), as.integer)# 1 no# FALSE TRUEget_warning(c(1, "no"), as.integer)# drop NULLsget_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 FALSEget_error(c(1, 0, 2), foo)# drop NULLsget_error(c(1, 0, 2), foo, .null = FALSE)Import
Description
Import a single function from a package
Usage
import(pkg, fun, overwrite = FALSE)Arguments
pkg | String, name of the package |
fun | String, fun name of the function |
overwrite | Logical, if TRUE and |
Value
None, called for side effects
Examples
# assigns `add` -- test with cautionimport("magrittr", "add")Insert
Description
Insert values at a position
Usage
insert(x, positions, values)Arguments
x | A vector of values |
positions | Integer of positions of |
values | A vector of values to insert into |
Value
A vector with the intended values inserted
Examples
insert(letters[1:5], c(2, 4), c("X", "Y"))Is File/Directory
Description
Is the path a file/directory?
Usage
is_dir(x)is_file(x)Arguments
x | A vector of file paths |
Details
These are essentially taken fromutils::file_test() forop = '-d' andop = -f but separated.
Value
Alogical vector
Dataframe labels
Description
Assign labels to a vector or data.frame.
Usage
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, ...)Arguments
x | A vector of data.frame |
... | One or more unquoted expressed separated by commas. If assigningto 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 thelabel attribute across all columns |
Details
When labels are assigned to a data.frame they can make viewing the object(withView() inside Rstudio). Theview_labels() has a call toView()inside and will retrieve the labels and show them in the viewer as adata.frame.
Value
A labelled vector ordata.frame
Examples
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 columnlabs0 <- remove_labels(labs, c("Sepal.Length", "Sepal.Width"))get_labels(labs0) # No labels for Sepal.Length and Sepal.WidthLimit
Description
Limit a numeric vector by lower and upper bounds
Usage
limit(x, lower = min(x), upper = max(x))Arguments
x | A numeric vector |
lower | A lower limit (as |
upper | An upper limit (as |
Value
The vectorx withlower andupper as the minimum, maximumvalues
Lines of R code
Description
Find the total number of lines of R code
Usage
lines_of_r_code(x = ".", skip_empty = TRUE)Arguments
x | Directory to search for files |
skip_empty | Logical, if TRUE will not count lines that are empty oronly contain a bracket or quotation mark. |
Details
Tries to read each file in the directory that ends in .R or .r and sumstogether. Files that fail to read are not counted.
Value
Aninteger for the number of lines in all applicable files
Examples
lines_of_r_code(system.file())lines_of_r_code(system.file(), skip_empty = FALSE)List to data.frame
Description
Converts a list object into a data.frame
Usage
list2df(x, name = "name", value = "value", show_NA, warn = TRUE)Arguments
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 |
Details
Unlikebase::list2DF(),list2df() tries to format the data.frame by usingthe names of the list as values rather than variables. This creates alonger form list that may be more tidy.
Value
adata.frame object with columns"name" and"value" for thenames of thelist and the values in each
Examples
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)}List all environments and objects
Description
Functions to list out all environments and objects
Usage
environments()ls_all(all.names = FALSE)objects_all(all.names = FALSE)Arguments
all.names | a logical value. If |
Details
environments() is basically a printing wrapper forbase::search()
ls_all() andobjects_all() can be used retrieved all objects from allenvironments in thesearch() path, which may print out a large resultinto the console.
Value
environments(): Invisibly, acharactervector of environment names
ls_all(),objects_all(): A named list for each of the environmentsthesearch()path with all the objects found in that environment
Logic - Extension'
Description
All functions take logical or logical-like (i.e., 1, 0, or NA asinteger or doubles) and return logical values.
Extensions to the base logical operations to account forNA values.
base::isTRUE() andbase::isFALSE() will only return single lengthTRUEorFALSE as it checks for valid lengths in the evaluation. When needing tocheck over a vector for the presence ofTRUE orFALSE and not being heldback byNA values,is_true andis_false will always provide aTRUEFALSE when the vector is logical or returnNA is the vectorx is notlogical.
%or% is just a wrapper forbase::xor()
Usage
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% yOR(..., na.rm = FALSE)AND(..., na.rm = FALSE)either(x, y)is_boolean(x)none(..., na.rm = FALSE)Arguments
x,y | A vector of logical values. If |
... | Vectors or a list of logical values |
na.rm | Logical, if |
Details
Logical operations, extended
Value
is_true(),is_false(),either(),%or%,AND(),OR(): Alogicalvector, equal length ofx(oryor of all...lengths)is_boolean():TRUEorFALSEnone():TRUE,FALSE, orNA
Examples
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% TRUETRUE %xor% TRUETRUE %xor% FALSENA %xor% FALSEOR(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
Description
List Objects - extensions
Usage
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())Arguments
pattern | an optionalregular expression. Only namesmatching |
all.names | a logical value. If |
envir | an alternative argument to |
Value
Acharacter vector of names
Make system file function
Description
Simple wrapper for package specific function for internal packages
Usage
make_sf(package)Arguments
package | The name of the package |
Match arguments
Description
This function is essentially a clear version ofbase::match.arg() whichproduces a cleaner warning message and does not restrict thetable paramtocharacter vectors only.
Usage
match_arg(x, table)Arguments
x | An argument |
table | A table of choices |
Details
Match arguments
Value
A single value fromx matched ontable
See Also
Examples
x <- c("apple", "banana", "orange")match_arg("b", x)# Produces errortry(match_arg("pear", x))foo <- function(x, op = c(1, 2, 3)) { op <- match_arg(op) x / op}foo(10, 3)# Errortry(foo(1, 0))Match params
Description
Much likebase::match.arg() with a few key differences:
Will not perform partial matching
Will not return error messages with ugly quotation marks
Usage
match_param( param, choices, null = TRUE, partial = getOption("mark.match_param.partial", FALSE), multiple = FALSE, simplify = TRUE)Arguments
param | The parameter |
choices | The available choices; named lists will return the name (acharacter) for when matched to the value within the list element. A listof formula objects (preferred) retains the LHS of the formula as the returnvalue when matched to the RHS of the formula. |
null | If |
partial | If |
multiple | If |
simplify | If |
Details
Param matching for an argument
Value
A single value fromparam matched onchoices
See Also
Examples
fruits <- function(x = c("apple", "banana", "orange")) { match_param(x)}fruits() # appletry(fruits("b")) # must be exact fruits("banana")pfruits <- function(x = c("apple", "apricot", "banana")) { match_param(x, partial = TRUE)}pfruits() # appletry(pfruits("ap")) # matchParamMatchErrorpfruits("app") # appleafruits <- function(x = c("apple", "banana", "orange")) { match_param(x, multiple = TRUE)}afruits() # apple, banana, orange# can have multiple responseshow_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 insteadls <- list(1L ~ 0:1, 2L, 3L ~ 3:5)sapply(0:5, match_param, choices = ls)Compute the MD5 hash of an object
Description
Wrapper for callingtools::md5sum() on objects rather than files.
Usage
md5(x)Arguments
x | An object |
Details
Allx objects areserialized to a temporaryfile beforetools::md5sum() is called.
Value
Amd5sum object
Examples
md5("hello")md5(1:10)md5(data.frame(a = 1:10, b = letters[1:10]))Median (Q 50)
Description
Median as the 50th quantile with an option to select quantile algorithm
Usage
median2(x, type = 7, na.rm = FALSE)q50(x, type = 7, na.rm = FALSE)Arguments
x | numeric vector whose sample quantiles are wanted, or anobject of a class for which a method has been defined (see also‘details’). |
type | an integer between 1 and 9 selecting one of thenine quantile algorithms detailed below to be used. |
na.rm | logical; if true, any |
Details
q50 is an alias formedian2
Value
Seestats::quantile()
See Also
Examples
set.seed(42)x <- rnorm(100)median(x) # 0.08979677median2(x, type = 7) # 0.08979677 - default type is 7median2(x, type = 3) # 0.08976065Merge lists
Description
Merge lists with different or intersecting names
Usage
merge_list(x, y, keep = c("x", "y"), null = c("ignore", "drop", "keep")[1:2])Arguments
x,y | Lists to merge |
keep | When matching names are found, from which object should thevalues be retained; |
null | Method for handling
|
Examples
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 searching
Description
Multiple search pattern searches
Usage
multi_grepl(x, patterns, ..., simplify = TRUE)multi_grep(x, patterns, ..., simplify = TRUE)Arguments
x | Passed to |
patterns | A list or vector of patterns to search across |
... | Additional arguments passed to |
simplify | if |
Value
The name or position of the pattern that is matched
Examples
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 eithermulti_grepl(x, c("a" = "^[ab]", "b" = "e")) # apple matches "a" before "b"multi_grepl(x, c("a" = "^[ab]", "b" = "e"), simplify = FALSE) # shows all matchesmulti_grepl(x, c("^[ab]", "e")) # returned as positionsmulti_grepl(x, c("^[ab]", "e"), simplify = FALSE)NA at positions
Description
Converts select elements of a vector intoNAs
This is how the end results are
NA_atandNA_ifrequire a suitable index value (x[y] <- NA)NA_atexpectsy(or the result of functiony) to beintegersNA_ifexpectsy(or the result of functiony) to belogical
NA_inandNA_outexpect some values to match onNA_inchecksx[x %in% y] <- NANA_outchecksx[x %out% y] <- NA(seefuj::match_ext)
Usage
NA_at(x, y, ...)NA_if(x, y, ...)NA_in(x, y, ...)NA_out(x, y, ...)Arguments
x | A vector of values |
y | Either a suitable value (see |
... | Additional values passed to |
Details
Convert specific values to NA
Value
x with assignedNA values
See Also
Inspired bydplyr::na_if()
Examples
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 eNA_in(let, c("a", "c")) # [1] <NA> b <NA> d eNA_out(let, c("a", "c")) # [1] a <NA> c <NA> <NA>Selecting NA columns
Description
Select or remove columns that are entirely NA
Usage
select_na_cols(x)remove_na_cols(x)is_na_cols(x, names = TRUE)Arguments
x | A |
names | Logical, if |
Value
select_na_cols()xwith only columns that are allNAremove_na_cols()xwithout columns of onlyNAis_na_cols()a logical vector:TRUEall rows of column areNA,otherwiseFALSE
Normalize paths
Description
Normalize and check a vector of paths
Usage
norm_path(x = ".", check = FALSE, remove = check)file_path(..., check = FALSE, remove = check)user_file(..., check = FALSE, remove = check)Arguments
x | A character vector of paths |
check | Logical, if TRUE will check if the path exists and output awarning if it does not. |
remove | Logical, if TRUE will remove paths that are not found |
... | Character vectors for creating a path |
Value
A vector of full file paths
Normalize values
Description
Normalizes values based on possible range and new bounds
Usage
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, ...)Arguments
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 |
Details
Parametersrange andbounds are modified withbase::range().The largest and smallest values are then used to determine theminimum/maximum values and lower/upper bounds. This allows for a vector ofmore than two values to be passed.
The current implementation ofnormalize.data.frame() allows forlist ofparameters passed for each column. However, it is probably best suited fordefault values.
Value
x with transformed values whererange values are transformed tobounds.
Examples
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 matrixmat <- 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))matnormalize(mat, bounds = -1:1)normalize(as.data.frame(mat), bounds = -1:1)Make not available
Description
Create NA vectors
Usage
not_available(type = "logical", length = 0L)set_not_available(type, value)NA_Date_NA_POSIXct_NA_POSIXlt_Arguments
type | Type of NA (see details) |
length | Length of the vector |
value | A value to return in |
Format
An object of classDate of length 1.
An object of classPOSIXct (inherits fromPOSIXt) of length 1.
An object of classPOSIXlt (inherits fromPOSIXt) of length 1.
Details
If length is a text it will search for an appropriate match.
Value
A vector ofNA values
Examples
x <- not_available("Date", 3)xclass(x)Append a note to an object
Description
An alternative to thebase::comment().
Usage
note(x) <- valueset_note(x, value)note(x)Arguments
x | An object |
value | The note to attach; if |
Details
When the note is assigned to an object a new class will be added,note, so that aprint function can call an S3 method. The print forthis can be adjusted for it's width by using the optionmark.note.widthwhich defaults to the optionwidth when not set.
The type of object assigned to the note is not restricted, so user bewareof odd prints or additional features added to the notes fun.
When assigning a note (withnote<-, and its aliasset_note()) thenoted class is added to the object. This allows theprint.noted classto be dispatched and for the note to be printed every time the object iscalled/printed and the next print method used. However, it will not becalled when notinteractive()
Value
note<-,set_note()will returnx(with the"note"attributeassigned)note()will retrieve the"note"attribute
Examples
x <- c("x", "k", "c", "d")comment(x) <- "This is just a comment"comment(x)# Comment is intentionally hiddenxnote(x) <- "Just some random letters"note(x)# Note is now present every timex# Assigning `NULL` will remove note (and class)note(x) <- NULLnote(x) # NULLx # No more noteOmit NA values
Description
Omit NA values
Usage
omit_na(x)Arguments
x | A vector of values |
Value
x whichNA values removes and two attributes ofintegers:nawhich is the position ofNA values, andvalid for the position ofnon-NA values; empty positions reported asinteger(0)
Examples
# Like stats::na.omit but always providesx <- letters[1:5]omit_na(x)x[c(3, 5)] <- NAomit_na(x)Check if package is available
Description
A wrapped requireNamespace
Usage
package_available(namespace)Arguments
namespace | One or more packages to to require. |
Value
require_namespace(): None, called for side effectspackage_available(): Visibly,TRUEorFALSE
Percentile rank
Description
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 thecurrent score.
PR = (c_L + 0.5 f_i) / N
Where
c_L is the frequency of scores less than the score of interest
f_i is the frequency of the score of interest
Usage
percentile_rank(x, weights = times, times)Arguments
x | A vector of values to rank |
weights,times | A vector of the number of times to repeat |
Details
Computes a percentile rank for each score in a set.
Value
The percentile rank ofx between 0 and 1 (see Boundaries)
Boundaries
While the percentile rank of a score in a set must be exclusively within theboundaries of0 and1, this function may produce a percentile rank thatis exactly0 or1. This may occur when the number of values are so largethat the value within the boundaries is too small to be differentiated.
Additionally, when using theweights parameter, if the lowest or highestnumber has a value of0, the number will then have a theoretical0 or1, as these values are not actually within the set.
Examples
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 timespercentile_rank(7:1, c(1, 0, 2, 2, 3, 1, 1))Print bib data frame
Description
Print bib dataframe, or as list
Usage
## S3 method for class 'mark_bib_df'print(x, list = FALSE, ...)Arguments
x | The |
list | If |
... | Additional arguments passed to methods |
Value
x, invisibly, called for its side effects
Printpseudo_id
Description
Printpseudo_id
Usage
## S3 method for class 'pseudo_id'print(x, ..., all = FALSE)Arguments
x | An object of classpseudo_id |
... | Not implemented |
all | if |
Value
x, invisibly. Called for its side effects.
See Also
Print as c
Description
Prints a vector to paste into an R script
Usage
print_c(x = read_clipboard(), sorted = TRUE, null = TRUE)Arguments
x | A vector (defaults to reading the clipboard) |
sorted | If |
null | If |
Details
This sorts (if set) and provides unique values for each element inx andprints then as a call toc. This can be useful for copying data that youwant to save as a vector in an R script.The result is both called incat() as well as copied to the clipboard.
Value
Invisibly, as acharacter vector, the object printed to the console
Examples
print_c(1:10)print_c(letters[1:3])print_c(month.abb)Process bib values
Description
Generates a data frame of values from bibs
Usage
process_bib_dataframe(categories, values, fields, keys)Arguments
categories | A list of categories |
values | A list of values |
fields | a Vector of fields |
keys | a Vector of keys |
Value
A widedata.frame with explicitNAs
Create an ID for a vector
Description
Transforms a vector into an integer of IDs.
Usage
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, ...)Arguments
x | A vector of values |
... | Additional arguments passed to methods |
na_last |
|
Value
Apseudo_id object where theinteger value of the vectorcorrespond to the position of the unique values in the attribute"uniques".
Examples
set.seed(42)(x <- sample(letters, 10, TRUE))(pid <- pseudo_id(x))attr(pid, "uniques")[pid]Quiet stop
Description
Quietly calls stop
Usage
quiet_stop()Value
None, called for side effects
Range 2
Description
Employsmin() andmax(). However,base::range(), there is no argumentfor removingInf values.
Usage
range2(x, na.rm = FALSE)Arguments
x | A numeric (or character) vector (see Note inbase::min) |
na.rm | Logical, if |
Value
Anumeric vector of length 2 of the minimum and maximum values,respectively
Examples
x <- rep(1:1e5, 100)system.time(rep(range(x), 100))system.time(rep(range2(x), 100))x[sample(x, 1e5)] <- NAsystem.time(rep(range(x, na.rm = TRUE), 100))system.time(rep(range2(x, na.rm = TRUE), 100))Read Bib file
Description
Read a bib file into a data.frame
Usage
read_bib(file, skip = 0L, max_lines = NULL, encoding = "UTF-8")Arguments
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 |
Details
Inspired and partially credited tobib2df::bib2df() although this has nodependencies outside of base functions and much quicker. This speed seemsto come from removingstringr functions and simplifying a few *applyfunctions.This will also include as many categories as possible from the entry.
Value
Adata.frame with each row as a bib entry and each column as afield
See Also
?bib2df::bib2df
Examples
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]}Recode by
Description
A simple implementation of recoding
Usage
recode_by(x, by, vals = NULL, mode = "any")recode_only(x, by, vals = NULL)Arguments
x | A vector to recode |
by | A names vector ( |
vals | An optional vector of values to use in lieu of a names in thevector; this takes priority over |
mode | passed to |
Details
This can be comparable todplyr::recode() expect that the values arearranged asnew = old rather thanold = new and allows for a separatevector to be passed fornew.
recode_only() will only recode the values matches inby/val.Themode is automatically set according tomode(x). This functionsmore likebase::replace() but with extra features
Value
A vector of values fromx
See Also
Examples
recode_by(1:3, c(a = 1, b = 2))recode_by(letters[1:3], c(`1` = "a", `2` = "b")) # will not guess moderecode_by(letters[1:3], c(`1` = "a", `2` = "b"), mode = "integer") # make as integerrecode_by(letters[1:3], c("a", "b"), vals = 1:2) # or pass to valsrecode_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 multiplesrecode_only(letters[1:10], list(abc = c("a", "b", "c"), ef = c("e", "f")))Objects exported from other packages
Description
These objects are imported from other packages. Follow the linksbelow to see their documentation.
- fuj
%:::%,%::%,%||%,%colons%,%len%,%names%,%out%,%wi%,%wo%,any_match,exattr,flip,is_linux,is_macos,is_named,is_windows,muffle,no_match,quick_df,quick_dfl,remove_names,require_namespace,set_names,struct,wuffle- magrittr
Reindex a data.frame
Description
Reindexes a data.frame with a reference
Usage
reindex( x, index = NULL, new_index, expand = c("intersect", "both"), sort = FALSE)Arguments
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 thatintersect (none), all values in x or index, or retain all values found. |
sort | Logical, if |
Value
Adata.frame with rows ofindex
Examples
iris1 <- head(iris, 5)iris1$index <- 1:5reindex(iris1, "index", seq(2, 8, 2))reindex(iris1, "index", seq(2, 8, 2), expand = "both")# Using letters will show changes in rownamesiris1$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 NA
Description
Remove NAs from a vector
Usage
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)Arguments
x | A vector of values |
Details
remove_na.factor will removeNA values as identified by thelevels()or by the integer value of the level.factors are recreated with allNA values and, if present, theNAlevel removed.
Value
x without values whereis.na(x) isTRUEFor factors, a new factor (ordered ifis.ordered(x))
Examples
remove_na(c(4, 1, 2, NA, 4, NA, 3, 2))# removes based on levelsremove_na(fact(c("b", NA, "a", "c")))# removes based on valuesx <- as_ordered(c("b", "d", "a", "c"))x[2:3] <- NAstr(remove_na(x))Remove NULL
Description
Remove NULL results from a list
Usage
remove_null(x)Arguments
x | A list |
Value
The listx withoutNULL
Examples
x <- list(a = letters[1:5], b = NULL, c = complex(3))xremove_null(x)Rounding by a specific interval.
Description
Rounds a number or vector of numbers by another
Usage
round_by(x, by = 1, method = c("round", "ceiling", "floor"), include0 = TRUE)Arguments
x | A number or vector to round. |
by | The number by which to round |
method | An option to explicitly specify automatic rounding, ceiling, orfloor |
include0 | If |
Value
A vector ofdoubles of the same length ofx
Examples
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))Row bind
Description
Bind a list ofdata.frames
Usage
row_bind(...)Arguments
... | A list of |
Value
Adata.frame combining all the rows fromdata.frames in...and all the columns, as they appear. An emptydata.frame with0 columnsand0 rows is returned if... has no length
See Also
dplyr::bind_rows()base::rbind()
Rscript
Description
ImplementsRscript withsystem2
Usage
rscript(x, ops = NULL, args = NULL, ...)Arguments
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 |
Value
Acharacter vector of the result from callingRscript viasystem2()
See Also
Save source
Description
Source a file and save as file
Usage
save_source(env = parent.frame(), file = mark_temp("Rds"), name = NULL)Arguments
env | The parent environment |
file | The file to save the environment to |
name | An optional name for the environment (mostly cosmetic) |
Value
Asource_env/environment object, created fromenv
Set names
Description
Sets or removes names
Usage
set_names0(x, nm = x)names_switch(x)Arguments
x | A vector of values |
nm | A vector of names |
Value
set_names0():xwithnmvalues assigned to names (ifxisNULL,NULLis returned)remove_names():xwithout namesnames_switch():charactervector of equal lengthxwhere names andvalues are switched
Time reports
Description
This function can be used to evaluate an expression line-by-line to captureoutputs, errors, messages, and evaluation time.
Usage
simpleTimeReport(title = NULL, expr, envir = parent.frame())Arguments
title | The title to be printed |
expr | The expression to run |
envir | The environment from which to evaluate the |
Details
Evaluate code and report on the time difference
Value
Areported_results/list object containing results, outputs,messages, warnings, and errors
Examples
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 by
Description
Sort an object by another object
Usage
sort_by(x, by, ...)Arguments
x | A vector |
by | Another vector |
... | Additional arguments passed to |
Value
The values ofx, resorted
Examples
l3 <- letters[1:3]sort_by(l3, c(3, 2, 1))# make a factor object with the reversed orderf <- factor(l3, levels = rev(l3))sort_by(f, l3)sort_by(1:3, rev(l3))Sort by names
Description
Sort a vector by it's name
Usage
sort_names(x, numeric = FALSE)Arguments
x | A named vector of values |
numeric | If |
Value
x sorted by itsnames()
Source file from directory
Description
Walk through files in a directory and output them.Files are sources in order of names
Usage
source_r_dir(dir, echo = FALSE, quiet = FALSE, ...)source_r_file(path, echo = FALSE, quiet = FALSE, ...)Arguments
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. |
Value
None, called for side effects
Source to environment
Description
Source an R script to an environment
Usage
source_to_env(x, ops = NULL)Arguments
x | An R script |
ops | Options to be passed torscript |
Value
Invisibly, and environment variable of the objects/results createdfromx
Sourcing extensions
Description
Functions for extending sourcing features
Usage
ksource(file, ..., quiet = TRUE, cd = FALSE, env = parent.frame())try_source(file, cd = FALSE, ...)try_ksource(file, ...)Arguments
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 temporarilychanged to the directory containing file for evaluating |
env | An environment determining where the parsed expressions areevaluated |
Details
try_source() will output an error message rather than completely preventingthe execution.This can be useful for when a script calls on multiple, independent files tobe sourced and a single failure shouldn't prevent the entire run to fail aswell.
Value
ksource(): Invisibly, the result of callingsource()on the.Rfile conversion offiletry_source(),try_ksource(): attempts ofsource()andksource()but converts errors to warnings
Extract date from string
Description
Extract date from string
Usage
str_extract_date(x, format = "%Y-%m-%d")str_extract_datetime(x, format = "%Y-%m-%d %H%M%S")Arguments
x | A character vector |
format | A date format to find |
Value
ADate (if found) orNA
Examples
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")String Slice
Description
Slice/split a string into multiple lines by the desired length of the line.
Usage
str_slice(x, n = 80L)str_slice_by_word(x, n = 80L)Arguments
x | A character vector |
n | Integer, the length of the line split |
Value
Acharacter vector
Examples
if (requireNamespace("stringi")) { x <- stringi::stri_rand_lipsum(1) str_slice(x) str_slice_by_word(x, n = 50L)}Switch with a list of parameters
Description
switch_params() is a vectorized version ofswitchswitch_case() uses a formula syntax to return the value to the right of thetilde (~) whenx isTRUEswitch_in_case() is a special case ofswitch_case() formatch()-ingxin the values on the left to return the value on the right.
Usage
switch_params(x, ...)switch_in_case(x, ..., .default = NULL, .envir = parent.frame())switch_case(..., .default = NULL, .envir = parent.frame())Arguments
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 |
Details
Switch with a list of params
Value
A named vector of values of same lengthx; or forswitch_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
Examples
# by singleswitch_params(c("j", "m", "b"), j = 10, b = 2, m = 13)# match with TRUEswitch_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 vectorswitch_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# errortry(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 functionsswitch_in_case( 1:6, c(1, 3, 5) ~ exp, c(2, 4) ~ log)Data frame transpose
Description
This transposes a data.frame witht() but transforms back into a data.framewith column and row names cleaned up. Because the data types may be mixedand reduced to characters, this may only be useful for a visual viewing ofthe data.frame.
Usage
t_df(x, id = NULL)Arguments
x | A data.frame |
id | No longer used |
Details
Transposes a data.frame as a data.frame
Value
A transposeddata.frame with columns ("colname","row_1", ...,for each row inx.
Examples
x <- data.frame(col_a = Sys.Date() + 1:5, col_b = letters[1:5], col_c = 1:5)t_df(x)Table NA values
Description
Tables out whether data are NAs are not
Usage
tableNA(..., .list = FALSE)Arguments
... | one or more objects which can be interpreted as factors(including numbers or character strings), or a |
.list | Logical, if |
Details
All data are checked withis.na() and the resultingTRUE orFALSE isare tabulated.
Value
table() returns acontingency table, an object ofclass"table", an array of integer values.Note that unlike S the result is always anarray, a 1Darray if one factor is given.
as.table andis.table coerce to and test for contingencytable, respectively.
Theas.data.frame method for objects inheriting from class"table" can be used to convert the array-based representationof a contingency table to a data frame containing the classifyingfactors and the corresponding entries (the latter as componentnamed byresponseName). This is the inverse ofxtabs.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
See Also
tabulate is the underlying function and allows finercontrol.
Useftable for printing (and more) ofmultidimensional tables.margin.table,prop.table,addmargins.
addNA for constructing factors withNA asa level.
xtabs for cross tabulation of data frames with aformula interface.
Examples
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 listtableNA(x, .list = TRUE) # counts for eachtableNA(x[1], x[2])tableNA(x[1], x[2], x[3]) # equivalent ot tableNA(x, .list = TRUE)That
Description
Grammatical correctness
Usage
that(x, arr.ind = FALSE, useNames = TRUE)Arguments
x | a |
arr.ind | logical; shouldarrayindices be returnedwhen |
useNames | logical indicating if the value of |
Details
Seefortunes::fortune(175).
Value
See Also
To Boolean
Description
Convert a vector to boolean/logical
Usage
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, ...)Arguments
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 |
Value
Alogical vector of equal length asx
To row names
Description
Converts a column to row names
Usage
to_row_names(data, row_names = 1L)Arguments
data | A data.frame |
row_names | The numeric position of the column. |
Value
Adata.frame
Examples
x <- data.frame( a = 1:4, b = letters[1:4])to_row_names(x)to_row_names(x, "b")Get TODOs
Description
Search for#`` TODO tags
Usage
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, ...)Arguments
pattern | A character string containing a regular expression to filterfor comments after tags; default |
path | Where to search for the todos. If this is a directory, pathsmatching 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 |
Details
Searches forTODO comments in files. Extensions withmd,Rmd,andqmd specifically search for a<-- TODO * --> string, whereaseverything else is found with# TODO.
Value
NULL if none are found, otherwise adata.frame with the linenumber, file name, and TODO comment.
Examples
## 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
Description
Try an expression a set number of times
Usage
tryn(expr, n = 10, silent = TRUE)Arguments
expr | expression to evaluate |
n | number of attempts until error |
silent | whether to suppress warnings |
Value
result ofexpr
Examples
foo <- function() stop("I added an error")try(tryn(n = 10, foo()))Unique rows
Description
Drops duplicated rows
Usage
unique_rows(data, cols = NULL, from_last = FALSE, invert = FALSE)Arguments
data | A |
cols | Columns to compare against; when |
from_last | When |
invert | If |
Value
data will duplicates removes
Examples
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 and squash
Description
Unlist without unique names; combine names for unique values
Usage
unlist0(x)squash_vec(x, sep = ".")Arguments
x | A vector of values |
sep | A separation for combining names |
Details
unlist0()is much likeunlist()expect that name are not made to beunique.squash_vec()works differently
Value
unlist0(): a vector with the possibility of non-unique namessquash_vec(): A vector of unique values and names
Examples
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 namesunlist(x) # names: a1 a2 a3 b c1 c2 c3unlist0(x) # names: a a a b c c cunlist0(y) # no change# squash_vec() is like the inverse of unlist0() because it works on valuessquash_vec(x)squash_vec(y)Add author to DESCRIPTION
Description
Adds author to description
Usage
use_author(author_info = find_author())Arguments
author_info | Author information as a named list |
Details
Only valid for a single author.
Value
None, called for side effects
Paste combine
Description
Paste and combine
Usage
paste_c(x, y, collate = TRUE, sep = "")paste_combine(..., collate = TRUE, sep = "")collapse0(..., sep = "")Arguments
x,y,... | Vectors to paste and/or combine |
collate | Logical; |
sep | A character string to separate terms |
Value
Acharacter vector
Examples
x <- letters[1:5]y <- 1:3z <- 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 = "_")Vaps!
Description
Wrappers for vapply
Usage
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)Arguments
.x | A vector of values |
.f | A function to apply to each element in vector |
... | Additional arguments passed to |
.nm | Logical, if |
Details
These are simply wrappers forbase::vapply() to shorten lines.
Each function is designed to use specific vector types:
- vap_int
integer
- vap_dbl
double
- vap_chr
character
- vap_lgl
logical
- vap_cplx
complex
- vap_date
Date
Value
A vector of type matching the intended value in the function name.
See Also
Vector to data.frame
Description
Transforms a vector (named) to a data.frame
Usage
vector2df(x, name = "name", value = "value", show_NA)Arguments
x | A vector of values. |
name,value | Character strings for the name and value columns |
show_NA | Ignored; will trigger a warning if set |
Value
Adata.frame withname (optional) andvalue columns
Temporary plotting
Description
Reset par() after running
Usage
with_par(..., ops = NULL)Arguments
... | Code to be evaluated |
ops | A named list to be passed to |
Value
Invisibly, the result of...
Examples
with_par( plot(lm(Sepal.Length ~ Sepal.Width, data = iris)), plot(lm(Petal.Length ~ Petal.Width, data = iris)), ops = list(mfrow = c(2, 4)))within boundaries
Description
Compare a vector within (between) other values
Usage
between_more(x, left, right, type = c("gele", "gel", "gle", "gl"))within(x, left = NULL, right = NULL, bounds = c("[]", "[)", "(]", "()"))Arguments
x | A numeric vector of values |
left,right | Boundary values. For |
type | Abbreviation for the evaluation of |
bounds | Boundaries for comparisons of |
Details
type``, bounds“ can be one of the below:
- g,(
is greater than (>)
- ge,[
greater than or equal to (>=)
- l,))
less than (<)
- le,[]
less than or equal to (<=)
Note:between_more() may be deprecated in the future in favor of justwithin()
Value
A logical vector
Examples
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)Function within
Description
Returns the function call you are within
Usage
within_call()within_fun()outer_call(n = 0)outer_fun(n = 0)Arguments
n | The number of calls to move out from |
Value
The string of the call/function
Write file with md5 hash check
Description
Write file with md5 hash check
Usage
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()Arguments
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. |
Value
write_file_md5():x, invisibly. Whenpathis not thestdout(),xis returned with the attribute"path"set to the result offile_copy_md5().mark_write_methods(): A list of applicable methods and their aliasesmark_compress_methods(): A character vector of applicable compressionmethods
options()
mark.compress.method: compression method to use when writing filesmark.list.hook: when adata.framecontains alistcolumn, thisfunction is applied to each element of the list. The default"auto"usestoJSON()if the packagejsonliteis available, otherwise
Examples
# just writes to stdout()df <- data.frame(a = 1, b = 2)write_file_md5(df)temp <- tempfile()write_file_md5(df, temp) # newwrite_file_md5(df, temp) # samedf$c <- 3write_file_md5(df, temp) # changesfs::file_delete(temp)