Skip to content

Commit ec1d314

Browse files
committed
Merge remote-tracking branch 'upstream/main' into add-numeric-checks
2 parents 0ea84e1 + 150d522 commit ec1d314

14 files changed

+101
-18
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Suggests:
4646
Enhances:
4747
winch
4848
Encoding: UTF-8
49-
RoxygenNote: 7.3.2
49+
RoxygenNote: 7.3.3
5050
Roxygen: list(markdown = TRUE)
5151
URL: https://rlang.r-lib.org, https://github.com/r-lib/rlang
5252
BugReports: https://github.com/r-lib/rlang/issues

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ S3method(summary,rlang_trace)
9797
S3method(summary,rlang_warning)
9898
export("!!!")
9999
export("!!")
100+
export("%&&%")
100101
export("%<~%")
101102
export("%@%")
102103
export("%@%<-")

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# rlang (development version)
22

3+
* New `%&&%` operator that returns RHS when LHS is non-NULL (#1774, @snystrom).
4+
35
* C code no longer calls `memcpy()` and `memset()` on 0-length R object memory
46
(#1797).
7+
* `is_syntactic_literal()` returns `FALSE` for objects with attributes, such as `array(1)` or `factor("x")` (#1817, @jonthegeek).
8+
* `is_syntactic_literal()` returns `FALSE` for negative numbers and complex numbers with negative imaginary components (#1799, @jonthegeek).
59

610

711
# rlang 1.1.6

R/expr.R

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,26 @@ is_expression <- function(x) {
119119
#' @export
120120
#' @rdname is_expression
121121
is_syntactic_literal <- function(x) {
122+
# Don't combine the tests, because we want to make sure type is one of these
123+
# *before* dispatching on `length()` or `is.na()` or even `attributes()`.
122124
switch(
123125
typeof(x),
124-
NULL = {
125-
TRUE
126-
},
127-
126+
NULL = TRUE,
128127
logical = ,
129-
integer = ,
130-
double = ,
131128
character = {
132-
length(x) == 1
129+
length(x) == 1 && is.null(attributes(x))
130+
},
131+
integer = ,
132+
double = {
133+
length(x) == 1 && is.null(attributes(x)) && (is.na(x) || x >= 0)
133134
},
134-
135135
complex = {
136-
if (length(x) != 1) {
137-
return(FALSE)
138-
}
139-
is_na(x) || Re(x) == 0
136+
length(x) == 1 && is.null(attributes(x)) && (is.na(x) || (Re(x) == 0 && Im(x) >= 0))
140137
},
141-
142138
FALSE
143139
)
144140
}
141+
145142
#' @export
146143
#' @rdname is_expression
147144
is_symbolic <- function(x) {

R/formula.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ is_bare_formula <- function(x, scoped = TRUE, lhs = NULL) {
101101
#' Get or set formula components
102102
#'
103103
#' `f_rhs` extracts the right-hand side, `f_lhs` extracts the left-hand
104-
#' side, and `f_env` extracts the environment. All functions throw an
105-
#' error if `f` is not a formula.
104+
#' side, and `f_env` extracts the environment in which the formula was defined.
105+
#' All functions throw an error if `f` is not a formula.
106106
#'
107107
#' @param f,x A formula
108108
#' @param value The value to replace with.
@@ -120,6 +120,8 @@ is_bare_formula <- function(x, scoped = TRUE, lhs = NULL) {
120120
#' f_lhs(x ~ y)
121121
#'
122122
#' f_env(~ x)
123+
#' f <- as.formula("y ~ x", env = new.env())
124+
#' f_env(f)
123125
f_rhs <- function(f) {
124126
if (is_quosure(f)) {
125127
signal_formula_access()

R/operators.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ if (exists("%||%", envir = baseenv())) {
1919
`%||%` <- get("%||%", envir = baseenv())
2020
}
2121

22+
#' Default value for non-`NULL`
23+
#'
24+
#' This infix operator is the conceptual opposite of `%||%`, providing a fallback
25+
#' only if `x` is defined.
26+
#'
27+
#' @param x,y If `x` is NULL, will return `x`; otherwise returns `y`.
28+
#' @export
29+
#' @name op-null-continuation
30+
#' @seealso [op-null-default]
31+
#' @examples
32+
#' 1 %&&% 2
33+
#' NULL %&&% 2
34+
`%&&%` <- function(x, y) {
35+
if (!is_null(x)) y else x
36+
}
37+
2238
`%|0|%` <- function(x, y) {
2339
if (!length(x)) y else x
2440
}

R/standalone-types-check.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# - Rename `check_number_decimal()` to `check_number()` (@khusmann, #1714)
1414
# - Add `check_numeric()` and `check_numeric_whole()`, vectorized versions
1515
# of `check_number()` and `check_number_whole()` (@khusmann, #1714)
16+
# - `check_logical()` gains an `allow_na` argument (@jonthegeek, #1724)
1617
#
1718
# 2024-08-15:
1819
# - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724)
@@ -615,12 +616,20 @@ check_character <- function(
615616
check_logical <- function(
616617
x,
617618
...,
619+
allow_na = TRUE,
618620
allow_null = FALSE,
619621
arg = caller_arg(x),
620622
call = caller_env()
621623
) {
622624
if (!missing(x)) {
623625
if (is_logical(x)) {
626+
if (!allow_na && any(is.na(x))) {
627+
abort(
628+
sprintf("`%s` can't contain NA values.", arg),
629+
arg = arg,
630+
call = call
631+
)
632+
}
624633
return(invisible(NULL))
625634
}
626635
if (allow_null && is_null(x)) {

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,6 @@ reference:
336336
- subtitle: Operators
337337
contents:
338338
- "`%||%`"
339+
- "`%&&%`"
339340
- "`%|%`"
340341
- "`%@%`"

man/f_rhs.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/op-null-continuation.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)