THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Browse Source

parse_env_subset() gained argument 'is_variable'

fixes #45
feature/c
Henrik Bengtsson 4 months ago
parent
commit
c3b12fec63
  1. 8
      NEWS
  2. 15
      R/parse_env_subset.R
  3. 10
      man/parse_env_subset.Rd
  4. 5
      tests/parse_env_subset.R

8
NEWS

@ -1,9 +1,13 @@
Package: listenv
================
Version: 0.8.0-9000 [2020-04-16]
Version: 0.8.0-9000 [2021-02-14]
* ...
NEW FEATURES:
* parse_env_subset() gained argument 'is_variable' to control whether or not
the inferred element named should be checked if it is a valid variable name
or not.
DEPRECATED AND DEFUNCT:

15
R/parse_env_subset.R

@ -7,6 +7,9 @@
#' @param substitute If `TRUE`, then the expression is [base::substitute()]:ed,
#' otherwise not.
#'
#' @param is_variable (logical) If TRUE and an element name is specified, then
#' the name is checked to be a valid variable name.
#'
#' @return A named list with elements:
#' \describe{
#' \item{`envir`}{An environment (defaults to argument `envir`)}
@ -22,7 +25,7 @@
#'
#' @export
#' @keywords internal
parse_env_subset <- function(expr, envir = parent.frame(), substitute = TRUE) {
parse_env_subset <- function(expr, envir = parent.frame(), substitute = TRUE, is_variable = TRUE) {
if (substitute) expr <- substitute(expr)
code <- paste(deparse(expr), collapse = "")
@ -112,10 +115,12 @@ parse_env_subset <- function(expr, envir = parent.frame(), substitute = TRUE) {
} # if (is.symbol(expr))
## Validate name, iff any
name <- res$name
if (nzchar(name) && !grepl("^[.a-zA-Z]+", name)) {
stop("Not a valid variable name: ", sQuote(name), call. = FALSE)
## Validate name, iff any?
if (is_variable) {
name <- res$name
if (nzchar(name) && !grepl("^[.a-zA-Z]+", name)) {
stop("Not a valid variable name: ", sQuote(name), call. = FALSE)
}
}

10
man/parse_env_subset.Rd

@ -4,7 +4,12 @@
\alias{parse_env_subset}
\title{Helper function to infer target from expression and environment}
\usage{
parse_env_subset(expr, envir = parent.frame(), substitute = TRUE)
parse_env_subset(
expr,
envir = parent.frame(),
substitute = TRUE,
is_variable = TRUE
)
}
\arguments{
\item{expr}{An expression.}
@ -13,6 +18,9 @@ parse_env_subset(expr, envir = parent.frame(), substitute = TRUE)
\item{substitute}{If \code{TRUE}, then the expression is \code{\link[base:substitute]{base::substitute()}}:ed,
otherwise not.}
\item{is_variable}{(logical) If TRUE and an element name is specified, then
the name is checked to be a valid variable name.}
}
\value{
A named list with elements:

5
tests/parse_env_subset.R

@ -295,9 +295,14 @@ stopifnot(inherits(res, "error"))
res <- try(target <- parse_env_subset(x[[0]], substitute = TRUE), silent = TRUE)
stopifnot(inherits(res, "try-error"))
## Invalid variable name
res <- try(target <- parse_env_subset("_a", substitute = TRUE), silent = TRUE)
stopifnot(inherits(res, "try-error"))
## Don't check variable name
target <- parse_env_subset("_a", substitute = TRUE, is_variable = FALSE)
stopifnot(is.list(target))
res <- try(target <- parse_env_subset(1:10, envir = x, substitute = FALSE),
silent = TRUE)
stopifnot(inherits(res, "try-error"))

Loading…
Cancel
Save