Title: | Miscellaneous Functions Used at 'Numeract LLC' |
---|---|
Description: | Contains functions useful for debugging, set operations on vectors, and 'UTC' date and time functionality. It adds a few vector manipulation verbs to 'purrr' and 'dplyr' packages. It can also generate an R file to install and update packages to simplify deployment into production. The functions were developed at the data science firm 'Numeract LLC' and are used in several packages and projects. |
Authors: | Mike Badescu [aut, cre], Ana-Maria Niculescu [aut], Teodor Ciuraru [ctb], Numeract LLC [cph] |
Maintainer: | Mike Badescu <[email protected]> |
License: | MIT + file LICENCE |
Version: | 0.3.7 |
Built: | 2025-02-10 05:29:51 UTC |
Source: | https://github.com/numeract/nmisc |
Wrapper around cat which appends new line to output.
catn(...)
catn(...)
... |
Arguments to be passed to |
None
Clear warnings for production code.
clear_warnings()
clear_warnings()
Converts Date and POSIXct objects to the format given as input.
format_utc(x, format = NULL, usetz = TRUE)
format_utc(x, format = NULL, usetz = TRUE)
x |
A Date or POSIXct object to be converted. |
format |
A character string. The default format is "%Y-%m-%d" for Date and "%Y-%m-%d %H:%M:%S" for POSIXct. |
usetz |
Logical. If TRUE, the time zone abbreviation is appended to the output. Applicable only if an POSIXct object. |
A character string representing the formatted date.
format_utc(Sys.time(), format = "%Y-%m-%d", usetz = FALSE)
format_utc(Sys.time(), format = "%Y-%m-%d", usetz = FALSE)
The function takes the output of get_packages
and
writes in a file the commands needed to install and update
package used throughout the project.
generate_install_file( file, package_df = get_packages(), include_core_package = FALSE )
generate_install_file( file, package_df = get_packages(), include_core_package = FALSE )
file |
The name of the file to be created. |
package_df |
A data frame obtained with |
include_core_package |
Logical, whether to include in the generated install file package which come with R by default |
Nothing
## Not run: package_df <- get_packages(package_options = c("library")) generate_install_file("install_packages.R", package_df) ## End(Not run)
## Not run: package_df <- get_packages(package_options = c("library")) generate_install_file("install_packages.R", package_df) ## End(Not run)
A simple wrapper around rappdirs:::get_os
,
allowing it to be exported.
get_os()
get_os()
One of "win"
, "mac"
, "unix"
,
"Unknown OS"
.
The function returns a data frame containing information about
packages that are loaded with library()
, require()
, used
with ::
operator, listed in the DESCRIPTION file,
and/or already loaded.
get_packages( project_path = ".", include_pattern = "\\.R(md)?$", exclude_pattern = "tests/", package_options = c("referenced", "library", "description") )
get_packages( project_path = ".", include_pattern = "\\.R(md)?$", exclude_pattern = "tests/", package_options = c("referenced", "library", "description") )
project_path |
A string representing the path of the project root
in which the function will look recursively in order to find files that
fit |
include_pattern |
A string representing a regex that matches
project files in which to look for packages. By default,
|
exclude_pattern |
A string representing a regex that matches
project files to exclude. By default,
|
package_options |
A character vector that represents the method through
which packages are loaded or referenced. The options are:
|
A data frame containing package information:
package_name |
The name of the package |
requested_by |
The context in which the package was used |
is_base |
Whether package is part of the core R packages |
source |
The source from which the package was installed |
version |
The version of the package, if installed locally |
is_installed |
Whether the package is installed locally |
## Not run: package_df <- get_packages( project_path = '.', include_pattern = '\\.R$', exclude_pattern = '', package_options = c('referenced')) ## End(Not run)
## Not run: package_df <- get_packages( project_path = '.', include_pattern = '\\.R$', exclude_pattern = '', package_options = c('referenced')) ## End(Not run)
Is it a POSIXct object?
is.POSIXct(x)
is.POSIXct(x)
x |
An R object. |
keep_at()
keeps only the elements from specific positions
while discard_at()
does the opposite.
The functions are wrappers around purrr::keep
and
purrr::discard
, respectively.
keep_at(.x, .at) discard_at(.x, .at)
keep_at(.x, .at) discard_at(.x, .at)
.x |
A list or a vector. |
.at |
A character vector (names), a numeric vector (positions),
a symbol or or a list generated by
|
A list or a vector.
x <- c("First" = 1, "Second" = 2, "Last" = 3) keep_at(x, "Second") keep_at(x, Second) keep_at(x, 2) keep_at(x, starts_with("Sec")) #> Second #> 2 keep_at(x, ends_with("t")) #> First Last #> 1 3 x <- c(1, 2, 3) discard_at(x, 1) #> Second Last #> 2 3
x <- c("First" = 1, "Second" = 2, "Last" = 3) keep_at(x, "Second") keep_at(x, Second) keep_at(x, 2) keep_at(x, starts_with("Sec")) #> Second #> 2 keep_at(x, ends_with("t")) #> First Last #> 1 3 x <- c(1, 2, 3) discard_at(x, 1) #> Second Last #> 2 3
Unlike intersect
, it does not remove duplicates in
x and keeps its order.
keep_if_in(x, y) x %if_in% y
keep_if_in(x, y) x %if_in% y
x |
Source vector. |
y |
Destination vector (of the same mode as x). |
A filtered version of x.
keep_if_in(1:5, 3:6) # returns [3, 4, 5] keep_if_in(c(4, 3, 4, 3, 1), 3:6) # returns [4 3 4 3]
keep_if_in(1:5, 3:6) # returns [3, 4, 5] keep_if_in(c(4, 3, 4, 3, 1), 3:6) # returns [4 3 4 3]
Unlike setdiff
, it does not remove duplicates in x
and keeps its order.
keep_if_not_in(x, y) x %if_not_in% y
keep_if_not_in(x, y) x %if_not_in% y
x |
Source vector. |
y |
Destination vector (of the same mode as x). |
A filtered version of x.
keep_if_not_in(1:5, 3:6) # returns [1 2] keep_if_not_in(c(4, 3, 4, 3, 1), 3:6) # returns [1]
keep_if_not_in(1:5, 3:6) # returns [1 2] keep_if_not_in(c(4, 3, 4, 3, 1), 3:6) # returns [1]
Returns a vector with the current date and time in the UTC time zone.
now_utc(length = 1L)
now_utc(length = 1L)
length |
Positive integer (scalar) indicating the length of the returned vector. If length is a vector of multiple elements, only the first element is taken into account. |
A POSIXct vector of size length
with the tzone
attribute set to "UTC".
now_utc(0) # returns "POSIXct of length 0"
now_utc(0) # returns "POSIXct of length 0"
Pull out a single column by using its name or its position and name the obtained vector using values from another column.
pull_with_names(.data, var = -1, name_col)
pull_with_names(.data, var = -1, name_col)
.data |
A data frame |
var |
The name of the column of interest, or a positive integer, giving the position counting from the left, or a negative integer, giving the position counting from the right. This argument supports tidyeval. |
name_col |
The column whose values will be used to name the pulled column. This argument supports tidyeval. |
A named vector.
head(pull_with_names(iris, 4, "Species"))
head(pull_with_names(iris, 4, "Species"))
Creates a sequence from 1 to the number of row or columns, respectively.
seq_nrow(x) seq_ncol(x)
seq_nrow(x) seq_ncol(x)
x |
a data frame or a matrix |
a vector of integers
Wrapper around setequal
that adds extra parameter
na.rm
.
setequal_na(x, y, na.rm = FALSE)
setequal_na(x, y, na.rm = FALSE)
x , y
|
Vectors (of the same mode) containing a sequence of items. |
na.rm |
Boolean value indicating whether
|
A logical scalar that states the result.
setequal_na(c(2, 1, 3), c(1, 2, 3)) # returns TRUE setequal_na(c(1, NA, 3), c(3, NA, 1), na.rm = TRUE) # returns TRUE setequal_na(c(NA, NA), c(NA), na.rm = TRUE) # returns TRUE setequal_na(c(NA, NA), c(NA)) # returns TRUE setequal_na(c(1, 2, 3), c(1, 2, 3, NA)) # returns FALSE
setequal_na(c(2, 1, 3), c(1, 2, 3)) # returns TRUE setequal_na(c(1, NA, 3), c(3, NA, 1), na.rm = TRUE) # returns TRUE setequal_na(c(NA, NA), c(NA), na.rm = TRUE) # returns TRUE setequal_na(c(NA, NA), c(NA)) # returns TRUE setequal_na(c(1, 2, 3), c(1, 2, 3, NA)) # returns FALSE
str1()
is a wrapper around str
which sets maximal level
of nesting to 1, while str2()
sets maximal level of nesting to 2.
str1(x) str2(x)
str1(x) str2(x)
x |
An R object |
Does not return anything.