-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add multiple functions for dealing with packages
- Loading branch information
Showing
17 changed files
with
749 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: Vmisc | ||
Title: Various functions for personal use | ||
Version: 0.1.0 | ||
Version: 0.1.5 | ||
Authors@R: | ||
person("Vencislav", "Popov", , "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0002-8073-4199")) | ||
|
@@ -11,7 +11,11 @@ BugReports: https://github.com/venpopov/Vmisc/issues | |
Imports: | ||
stringr, | ||
tools, | ||
utils | ||
utils, | ||
xfun, | ||
rlang, | ||
devtools, | ||
remotes | ||
Suggests: | ||
testthat (>= 3.0.0) | ||
Config/testthat/edition: 3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#' Create Named List from Arguments | ||
#' | ||
#' This function creates a named list from its arguments. If the arguments are named, | ||
#' those names are used in the resulting list. If some arguments are unnamed, the variable | ||
#' names themselves are used as names in the list. This can be useful for creating lists | ||
#' where the names are important for later indexing or manipulation, and ensures all | ||
#' elements in the list have names. | ||
#' | ||
#' @param ... Arbitrary arguments to be included in the list. These can be named or unnamed. | ||
#' Unnamed arguments will be named based on their variable names. | ||
#' | ||
#' @return A list where each element corresponds to an argument passed to the function. | ||
#' Elements of the list are named based on either their original names or the names of | ||
#' the variables passed as arguments. | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' var1 <- 1 | ||
#' var2 <- 1:10 | ||
#' # This will return a list with names: c("a", "b", "var1", "var2") | ||
#' nlist(a = 1, b = 2, var1, var2) | ||
|
||
nlist <- function(...) { | ||
# adapted from brms | ||
m <- match.call() | ||
dots <- list(...) | ||
no_names <- is.null(names(dots)) | ||
has_name <- if (no_names) FALSE else nzchar(names(dots)) | ||
if (all(has_name)) return(dots) | ||
nms <- as.character(m)[-1] | ||
if (no_names) { | ||
names(dots) <- nms | ||
} else { | ||
names(dots)[!has_name] <- nms[!has_name] | ||
} | ||
dots | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#' Check if Directories are Empty | ||
#' | ||
#' This function checks whether one or more directories are empty. | ||
#' An empty directory is one that contains no files or subdirectories. | ||
#' If the directory does not exist, it is considered as empty. | ||
#' | ||
#' @param paths A character vector containing one or more file paths. | ||
#' Each path is checked to determine if the corresponding directory is empty. | ||
#' | ||
#' @return A logical vector where each element corresponds to a directory | ||
#' specified in `paths`. `TRUE` indicates that the directory is empty, | ||
#' and `FALSE` indicates that it is not. | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # Create two temporary directories one of which is empty | ||
#' library(fs) | ||
#' dir1 <- tempfile() | ||
#' dir2 <- tempfile() | ||
#' dir_create(c(dir1, dir2)) | ||
#' dir_create(file.path(dir1, "subdir")) | ||
#' | ||
#' # Check if the directories are empty (should return FALSE, TRUE) | ||
#' is_dir_empty(c(dir1, dir2)) | ||
#' | ||
#' # Clean up | ||
#' dir_delete(c(dir1, dir2)) | ||
#' } | ||
is_dir_empty <- function(paths) { | ||
.is_dir_empty <- function(path) { | ||
if (dir.exists(path)) { | ||
files <- list.files(path) | ||
return(length(files) == 0) | ||
} | ||
return(TRUE) | ||
} | ||
sapply(paths, .is_dir_empty) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#' Convert Function Arguments to Strings | ||
#' | ||
#' This function takes any number of R language objects and converts their names into strings. | ||
#' This is particularly useful for programming where variable names or symbols need to be used | ||
#' as strings without evaluating them. It leverages `rlang`'s tidy evaluation framework. | ||
#' | ||
#' @param ... Arbitrary arguments representing R language objects or symbols. | ||
#' | ||
#' @return A character vector where each element is the string representation of the corresponding | ||
#' argument passed to the function. The order of the strings in the output matches the order of | ||
#' the arguments. | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' # returns the arguments as strings even though functions bmm() and brms() are not defined | ||
#' arg2string(bmm('0.4.0'), brms('2.20.4')) | ||
arg2string <- function(...) { | ||
args <- rlang::enquos(...) | ||
labels <- as.character(sapply(args, rlang::as_label)) | ||
# if the argument was already a string, it will be returned as is | ||
labels <- sapply(labels, function(x) | ||
tryCatch({ | ||
eval(parse(text = x)) | ||
}, | ||
error = function(e) { | ||
x | ||
} | ||
) | ||
) | ||
names(labels) <- NULL | ||
labels | ||
} |
Oops, something went wrong.