Skip to content

Commit f9d3176

Browse files
committed
Merge branch 'master' into 4.12
2 parents 7c17be4 + bf500c0 commit f9d3176

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ S3method(update,Welford)
4949
export("EVL<-")
5050
export("NVL<-")
5151
export("lrowweights<-")
52+
export("replace<-")
5253
export("rowweights<-")
5354
export("ult<-")
5455
export(.Deprecate_method)

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ New utilities
2626
- New function, `replace()`, a more pipe-friendly drop-in replacement
2727
for `base::replace()` for which replacement indices and values can
2828
be specified as functions evaluated on the input vector.
29+
`replace()<-` is also implemented, allowing the list to be modified
30+
in place.
2931

3032
Enhancements to existing utilities
3133
----------------------------------

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
* New functions, `ERRVL2()` and `ERRVL3()` that, unlike `ERRVL()`, do not require the expressions to be wrapped in `try()`.
1414

15-
* New function, `replace()`, a more pipe-friendly drop-in replacement for `base::replace()` for which replacement indices and values can be specified as functions evaluated on the input vector.
15+
* New function, `replace()`, a more pipe-friendly drop-in replacement for `base::replace()` for which replacement indices and values can be specified as functions evaluated on the input vector. `replace()<-` is also implemented, allowing the list to be modified in place.
1616

1717
## Enhancements to existing utilities
1818

R/misc.utilities.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,12 +1204,13 @@ modify_in_place <- function(x, value = x){
12041204
#'
12051205
#' This is a thin wrapper around [base::replace()] that allows `list`
12061206
#' and/or `values` to be functions that are evaluated on `x` to obtain
1207-
#' the replacement indices and values.
1207+
#' the replacement indices and values. The assignment version replaces
1208+
#' `x`.
12081209
#'
12091210
#' `list` function is passed the whole vector `x` at once (not
12101211
#' elementwise) and any additional arguments to `replace()`, and must
12111212
#' return an indexing vector (numeric, logical, character,
1212-
#' etc.). `values` function is passed `x` after subsetting it by the
1213+
#' etc.). `values`/`value` function is passed `x` after subsetting it by the
12131214
#' result of calling `list()`.
12141215
#'
12151216
#' If passing named arguments, `x`, `list`, and `values` may cause a
@@ -1218,7 +1219,7 @@ modify_in_place <- function(x, value = x){
12181219
#' @param x a vector.
12191220
#' @param list either an index vector or a function (*not* a function
12201221
#' name).
1221-
#' @param values either a vector of replacement values or a function
1222+
#' @param values,value either a vector of replacement values or a function
12221223
#' (*not* a function name).
12231224
#' @param ... additional arguments to `list` if it is a function;
12241225
#' otherwise ignored.
@@ -1247,9 +1248,20 @@ modify_in_place <- function(x, value = x){
12471248
#' stopifnot(identical(replace(x, \(.x) .x < 0, \(.x) .x * 1i),
12481249
#' base::replace(x, x < 0, x[x < 0] * 1i)))
12491250
#'
1251+
#' ### Modify the list in place.
1252+
#'
1253+
#' y <- x
1254+
#' replace(x, `<`, 1/4) <- 0
1255+
#' x
1256+
#' stopifnot(identical(x, replace(y, `<`, 0, 1/4)))
1257+
#'
12501258
#' @export
12511259
replace <- function(x, list, values, ...) {
12521260
if (is.function(list)) list <- list(x, ...)
12531261
if (is.function(values)) values <- values(x[list], ...)
12541262
base::replace(x, list, values)
12551263
}
1264+
1265+
#' @rdname replace
1266+
#' @export
1267+
`replace<-` <- function(x, list, ..., value) replace(x, list, value, ...)

man/replace.Rd

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

0 commit comments

Comments
 (0)