Skip to content

Commit

Permalink
add multiple functions for dealing with packages
Browse files Browse the repository at this point in the history
  • Loading branch information
venpopov committed Mar 3, 2024
1 parent 444ad97 commit a71f60c
Show file tree
Hide file tree
Showing 17 changed files with 749 additions and 7 deletions.
8 changes: 6 additions & 2 deletions DESCRIPTION
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"))
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ export("%A%")
export("%A%<-")
export("%a%")
export("%a%<-")
export(arg2string)
export(available_packages)
export(collapse)
export(extract_pkg_fun_calls)
export(is_dir_empty)
export(nlist)
export(packageOptions)
export(parse_pkg_version)
export(pkg_vavailable)
export(pkg_vload)
export(require_pkg)
export(stop2)
export(str_extract_nested_balanced)
export(strip_attributes)
Expand Down
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Vmisc 0.1.5

### New features
* add nlist() function to create a named list
* add is_dir_empty() function to check if a directory is empty
* add arg2string() to defuse a function's arguments into strings
* add pkg_vload() function which can load and/or install a specific version of multiple packages. This function takes calls to packages of the form pkg(version), e.g. dplyr('1.0.0').
* add parse_pkg_version() function that parses calls such as dplyr('1.0.0') into a list with package names and versions
* add require_pkg() function which checks if one or more packages are installed and if their versions are at least the specified one. If not, it gives an error message and stops the execution.
* add pkg_vavailable() function which is is an alternative to [xfun::pkg_available()] that checks for a specific version of the package rather than a minimal version.
* add available_packages() function which returns a simple character vector of all installed packages, including specific multiple versions created by pkg_vload()


# Vmisc 0.1.0

### New features
Expand Down
38 changes: 38 additions & 0 deletions R/data_objects.R
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
}
40 changes: 40 additions & 0 deletions R/file_system.R
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)
}
33 changes: 33 additions & 0 deletions R/functions.R
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
}
Loading

0 comments on commit a71f60c

Please sign in to comment.