Title: | List, String, and Meta Programming Utility Functions |
---|---|
Description: | Includes functions for mapping named lists to function arguments, random strings, pasting and combining rows together across columns, etc. |
Authors: | Timothy Conwell |
Maintainer: | Timothy Conwell <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.1.1 |
Built: | 2024-11-01 11:28:16 UTC |
Source: | https://github.com/tconwell/toolbox |
Get the names of the arguments to a function
argNames(x)
argNames(x)
x |
A function or string naming a function. |
A vector of the names of the arguments to a function.
argNames("readLines")
argNames("readLines")
Create a named list of length 1 using a name stored in a variable as the name.
argumentNamedList(name, x)
argumentNamedList(name, x)
name |
The name for the item in the list. |
x |
The item to put in the list. |
A named list.
argumentNamedList("test_name", 1)
argumentNamedList("test_name", 1)
Format a date string as " from a SQL database to a format compatible with a HTML date input value.
castDateString(x)
castDateString(x)
x |
A string. |
A string, formatted YYYY-MM-DD.
castDateString(Sys.time())
castDateString(Sys.time())
Convert strings to logical.
castLogical(x)
castLogical(x)
x |
A string. |
A string, converted to logical.
castLogical("1")
castLogical("1")
Convert strings to numeric if possible, otherwise remains as is.
castNumeric(x)
castNumeric(x)
x |
A string. |
A string, converted to numeric if possible.
castNumeric("100")
castNumeric("100")
Combine columns of a list/data frame into a list by row
combineCols(x, cols = NULL, by_name = FALSE, parallel = FALSE, cores = 1)
combineCols(x, cols = NULL, by_name = FALSE, parallel = FALSE, cores = 1)
x |
A list or data frame. |
cols |
An optional vector of column positions or names to combine together. If passing column names, set by_name to TRUE. The order of items in cols determines the order of the combined result. |
by_name |
Boolean, if TRUE, it quotes the items in cols to properly index the list by name (x[[1]] vs x[["col_a"]]). |
parallel |
Boolean, if TRUE, attempts to use mclapply. |
cores |
An integer, the number of cores to use if parallel is TRUE. |
A list of the values in each column combined together for each row.
combineCols(list("x" = c(1, 2, 3), "y" = c("a", "b", "c")))
combineCols(list("x" = c(1, 2, 3), "y" = c("a", "b", "c")))
Group items of a list by name
consolidateList(x)
consolidateList(x)
x |
A named list, likely with names repeating for different positions. |
A list with items consolidated by name.
consolidateList(list("col1" = "Test", "col2" = "Hello", "col1" = "Repeated Name"))
consolidateList(list("col1" = "Test", "col2" = "Hello", "col1" = "Repeated Name"))
Filters the argument list to match the arguments in what and then calls do.call.
do.call2(what, args, quote = FALSE, envir = parent.frame())
do.call2(what, args, quote = FALSE, envir = parent.frame())
what |
See do.call. |
args |
Argument list, gets filtered to match arguments of what. See do.call. |
quote |
See do.call. |
envir |
See do.call. |
See do.call.
do.call
do.call2(intersect, list(x = c(1, 2, 3), y = c(2)))
do.call2(intersect, list(x = c(1, 2, 3), y = c(2)))
Add double quotes to strings.
doubleQuoteText( x, char_only = TRUE, excluded_chars = c("NULL"), null_or_na_as_NULL = TRUE )
doubleQuoteText( x, char_only = TRUE, excluded_chars = c("NULL"), null_or_na_as_NULL = TRUE )
x |
A string. |
char_only |
TRUE/FALSE, if TRUE, adds quotes only if is.character(x) is TRUE. |
excluded_chars |
A character vector, will not add quotes if a value is in excluded_chars. |
null_or_na_as_NULL |
TRUE/FALSE, if TRUE, NULL and NA values are replaced with the string "NULL". |
A string, with double quotes added.
doubleQuoteText("Sample quotes.")
doubleQuoteText("Sample quotes.")
Checks if x is NULL or NA
isNULLorNA(x)
isNULLorNA(x)
x |
A object. |
TRUE/FALSE.
isNULLorNA(NULL)
isNULLorNA(NULL)
Format data as a JSON object (like this: "x": "120").
jsonStr(name, val)
jsonStr(name, val)
name |
A string, the name of the JSON entry |
val |
A string, the value to associate with the JSON entry. |
A string, data formatted as a JSON object.
jsonStr(name = "var1", val = "Blue")
jsonStr(name = "var1", val = "Blue")
Extract the values from each entry in a list of vectors at a specific index
listExtract(x, pos)
listExtract(x, pos)
x |
A list, each item of the list should have equal length. |
pos |
A integer, the position to extract from each entry in the list. |
A list.
listExtract(list(col1 = c(1, 2, 3, 4, 5), col2 = c("a", "b", "c", "d", "e")), 3)
listExtract(list(col1 = c(1, 2, 3, 4, 5), col2 = c("a", "b", "c", "d", "e")), 3)
Pastes the names of a object into a string, optionally quoting the names.
namesToString(x, collapse = ",", quote = FALSE)
namesToString(x, collapse = ",", quote = FALSE)
x |
A named object (vector, list, data.frame) |
collapse |
A string to separate the collapsed names. |
quote |
TRUE/FALSE, if TRUE, adds quotes to the names. |
A string.
namesToString(c("test" = 1, "this" = 2))
namesToString(c("test" = 1, "this" = 2))
Paste together columns of a list/data frame
pasteCols( x, sep = " ", collapse = NULL, use_paste0 = FALSE, cols = NULL, by_name = FALSE )
pasteCols( x, sep = " ", collapse = NULL, use_paste0 = FALSE, cols = NULL, by_name = FALSE )
x |
A list or data frame. |
sep |
A character sting to separate the terms. |
collapse |
An optional character string to separate the results. |
use_paste0 |
Boolean, if TRUE, will call paste0 instead of paste. |
cols |
An optional vector of column positions or names to paste together. If passing column names, set by_name to TRUE. The order of items in cols determines the order of the paste result. |
by_name |
Boolean, if TRUE, it quotes the items in cols to properly index the list by name (x[[1]] vs x[["col_a"]]). |
A string with the values in each column pasted together.
pasteCols(list("x" = c(1, 2, 3), "y" = c("a", "b", "c")))
pasteCols(list("x" = c(1, 2, 3), "y" = c("a", "b", "c")))
Paste parts of file paths/urls separated with single forward-slashes
pastePaths(...)
pastePaths(...)
... |
Text strings to combine into a file path |
A string.
pastePaths("/home/", "/files")
pastePaths("/home/", "/files")
Add single quotes to strings, useful for converting R strings into SQL formatted strings.
quoteText( x, char_only = TRUE, excluded_chars = c("NULL"), null_or_na_as_NULL = TRUE )
quoteText( x, char_only = TRUE, excluded_chars = c("NULL"), null_or_na_as_NULL = TRUE )
x |
A string. |
char_only |
TRUE/FALSE, if TRUE, adds quotes only if is.character(x) is TRUE. |
excluded_chars |
A character vector, will not add quotes if a value is in excluded_chars. |
null_or_na_as_NULL |
TRUE/FALSE, if TRUE, NULL and NA values are replaced with the string "NULL". |
A string, with single quotes added to match PostgreSQL string formatting.
quoteText("Sample quotes.")
quoteText("Sample quotes.")
Generates (pseudo)random strings of the specified char length
sampleStr(n_char, sample_chars = c(letters, LETTERS, 0:9))
sampleStr(n_char, sample_chars = c(letters, LETTERS, 0:9))
n_char |
A integer, the number of chars to include in the output string. |
sample_chars |
A vector of characters to sample from. Includes the lowercase and uppercase English alphabet and 0-9 by default. |
A string.
sampleStr(10)
sampleStr(10)