Skip to content

Commit

Permalink
feat: unpack assign
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldelarosa committed Oct 27, 2023
1 parent c2820d3 commit 802e51e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: Dmisc
Title: Daniel miscellaneous functions
Version: 0.3.1
Version: 0.3.2
Authors@R:
person(given = "Daniel E.",
family = "de la Rosa",
Expand Down
2 changes: 1 addition & 1 deletion R/describe.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#'
#' # Using describe
#' describe(cars)
#' describe(cars, flextable = TRUE, ft_args = list(cwidth = 1))
#' describe(cars, flextable = TRUE)
describe <- function(data, digits = 4, t = TRUE, flextable = FALSE, ft_args = list(), ...) {
data <- as.data.frame(data)
res <- list()
Expand Down
63 changes: 33 additions & 30 deletions R/unpack_assign.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@

# Unpack and assign values to specified names in an environment
# `r lifecycle::badge("experimental")`
#
# @param names A character vector specifying the names of variables to be assigned.
# @param values A list or vector containing the values to be assigned to the names.
# @param ... Additional arguments, including
# \itemize{
# \item{.envir}{The environment where the variables will be assigned. Defaults to the global environment.}
# \item{.warn}{Logical flag indicating whether to show a warning message. Defaults to TRUE.}
# }
#
# @return NULL. The function performs assignments in the specified environment.
#
#
# @rdname unpkg-assign
#
# @examples
# \dontrun{
# c("x", "y") %<...% list(1, 2)
# }
`%<...%` <- function(names, values, ...) {
#' Unpack and assign values to specified names in an environment
#' `r lifecycle::badge("experimental")`
#'
#' @param names A list or character vector specifying the names of variables to be assigned.
#' @seealso Additional arguments that can be passed to names include
#' \itemize{
#' \item{.envir}{The environment where the variables will be assigned. Defaults to the global environment.}
#' \item{.warn}{Logical flag indicating whether to show a warning message. Defaults to TRUE.}
#' }
#' @param values A list or vector containing the values to be assigned to the names.
#'
#' @return NULL. The function performs assignments in the specified environment.
#'
#'
#' @rdname unpkg-assign
#'
#' @examples
#' \dontrun{
#' c("x", "y") %<...% list(1, 2)
#' }
`%<...%` <- function(names, values) {
# Collect additional arguments
args <- list(...)

# Set default values if not provided
if (is.null(args[['.envir']])){
tryCatch({
.envir <- names[['.envir']]
names[['.envir']] <- NULL
}, error = function(x){
.envir <- .GlobalEnv
}
})

if (is.null(args[['.warn']])){
tryCatch({
.warn <- names[['.warn']]
.warn <- NULL
}, error = function(x){
.warn <- TRUE
}
})

# Issue a warning if '.warn' is TRUE
if (.warn) {
Expand Down Expand Up @@ -68,6 +71,6 @@

# @rdname unpkg-assign
# @order 2
`%...>%` <- function(values, names, ...) {
`%<...%`(names, values, ...)
`%...>%` <- function(values, names) {
`%<...%`(names, values)
}
2 changes: 1 addition & 1 deletion man/describe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions man/unpkg-assign.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 802e51e

Please sign in to comment.