From 7c1aeab357b7f099a44f78036ec37058b5ff145d Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 12:33:21 +0200 Subject: [PATCH 01/12] feature: allow id suffixes when retrieving dictionary element --- NAMESPACE | 1 + R/mlr_pipeops.R | 1 + R/po.R | 2 +- R/utils.R | 30 ++++++++++++++++++++++++++++++ man/dictionary_sugar_inc_get.Rd | 22 ++++++++++++++++++++++ tests/testthat/test_dictionary.R | 5 +++++ tests/testthat/test_po.R | 10 ++++++++++ 7 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 man/dictionary_sugar_inc_get.Rd diff --git a/NAMESPACE b/NAMESPACE index a40fd7b44..7322b5065 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -110,6 +110,7 @@ export(assert_graph) export(assert_pipeop) export(chain_graphs) export(concat_graphs) +export(dictionary_sugar_inc_get) export(filter_noop) export(greplicate) export(gunion) diff --git a/R/mlr_pipeops.R b/R/mlr_pipeops.R index 4b7d8910b..e498ffd19 100644 --- a/R/mlr_pipeops.R +++ b/R/mlr_pipeops.R @@ -48,6 +48,7 @@ mlr_pipeops = R6Class("DictionaryPipeOp", inherit = mlr3misc::Dictionary, public = list( metainf = new.env(parent = emptyenv()), add = function(key, value, metainf = NULL) { + assert_false(grepl("_\\d+$", key)) ret = super$add(key, value) if (!is.null(metainf)) { # we save the *expression*, not the value, because we could otherwise get version conflicts from objects. diff --git a/R/po.R b/R/po.R index 0589331f7..56d930245 100644 --- a/R/po.R +++ b/R/po.R @@ -82,7 +82,7 @@ po.PipeOp = function(.obj, ...) { #' @export po.character = function(.obj, ...) { - dictionary_sugar_get(dict = mlr_pipeops, .key = .obj, ...) + dictionary_sugar_inc_get(dict = mlr_pipeops, .key = key, ...) } #' @export diff --git a/R/utils.R b/R/utils.R index 7c1f89f7b..6d9ca5144 100644 --- a/R/utils.R +++ b/R/utils.R @@ -103,3 +103,33 @@ multiplicity_recurse = function(.multip, .fun, ...) { .fun(.multip, ...) } } + +#' @title A Quick Way to Initialize Objects from Dictionaries with Incremented ID +#' @description +#' Covenience function to obtain dictionary elements with incremented ids for easier avoidance +#' of ID clashes. This should only be used on dictionaries that ensure that the ids don't end with +#' the pattern `_`. Where `n` is any number. +#' +#' @return An element from the dictionary. +#' +#' @examples +#' dictionary_sugar_get("pca", id = "pca_1") +#' # is the same as +#' dictionary_sugar_get_inc("pca_1") +#' +#' @export +dictionary_sugar_inc_get = function(dict, .key, ...) { + newkey = gsub("_\\d+$", "", .key) + add_suffix = .key != newkey + if (add_suffix) { + assert_true(!hasArg("id")) + suffix = gsub(newkey, "", .key) + } + obj = mlr3misc::dictionary_sugar_get(dict = dict, .key = newkey, ...) + + if (add_suffix) { + obj$id = paste0(obj$id, suffix) + } + obj + +} diff --git a/man/dictionary_sugar_inc_get.Rd b/man/dictionary_sugar_inc_get.Rd new file mode 100644 index 000000000..7f8be51b6 --- /dev/null +++ b/man/dictionary_sugar_inc_get.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{dictionary_sugar_inc_get} +\alias{dictionary_sugar_inc_get} +\title{A Quick Way to Initialize Objects from Dictionaries with Incremented ID} +\usage{ +dictionary_sugar_inc_get(dict, .key, ...) +} +\value{ +An element from the dictionary. +} +\description{ +Covenience function to obtain dictionary elements with incremented ids for easier avoidance +of ID clashes. This should only be used on dictionaries that ensure that the ids don't end with +the pattern \verb{_}. Where \code{n} is any number. +} +\examples{ +dictionary_sugar_get("pca", id = "pca_1") +# is the same as +dictionary_sugar_get_inc("pca_1") + +} diff --git a/tests/testthat/test_dictionary.R b/tests/testthat/test_dictionary.R index c8bc860bf..980509804 100644 --- a/tests/testthat/test_dictionary.R +++ b/tests/testthat/test_dictionary.R @@ -211,3 +211,8 @@ test_that("mlr_graphs dictionary", { expect_data_table(dt, col.names = "unique") expect_true("key" %in% colnames(dt)) }) + +test_that("Cannot add pipeops with keys that invalidates the convenience for id incrementation", { + copy = mlr_pipeops$clone(deep = TRUE) + expect_error(copy$add("name_1", PipeOp), regexp = "grepl") +}) diff --git a/tests/testthat/test_po.R b/tests/testthat/test_po.R index e17d0d4fe..4b793b60a 100644 --- a/tests/testthat/test_po.R +++ b/tests/testthat/test_po.R @@ -202,3 +202,13 @@ test_that("mlr_pipeops multi-access works", { ) }) + +test_that("Incrementing ids works", { + x = po("pca_123") + expect_true(x$id == "pca_123") + expect_r6(x, "PipeOpPCA") + + x = po("learner_1", lrn("regr.rpart")) + expect_true(x$id == "regr.rpart_1") + expect_r6(x, "PipeOpLearner") +}) From c0f012f2fb1815fe4c3667947131939993184932 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 12:48:36 +0200 Subject: [PATCH 02/12] fix typo and add increment method for mget --- R/po.R | 4 ++-- R/utils.R | 21 +++++++++++++++++++-- tests/testthat/test_po.R | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/R/po.R b/R/po.R index 56d930245..e071ffe7b 100644 --- a/R/po.R +++ b/R/po.R @@ -82,7 +82,7 @@ po.PipeOp = function(.obj, ...) { #' @export po.character = function(.obj, ...) { - dictionary_sugar_inc_get(dict = mlr_pipeops, .key = key, ...) + dictionary_sugar_inc_get(dict = mlr_pipeops, .key = .obj, ...) } #' @export @@ -110,7 +110,7 @@ pos.NULL = function(.objs, ...) { #' @export pos.character = function(.objs, ...) { - dictionary_sugar_mget(dict = mlr_pipeops, .keys = .objs, ...) + dictionary_sugar_inc_mget(dict = mlr_pipeops, .keys = .objs, ...) } #' @export diff --git a/R/utils.R b/R/utils.R index 6d9ca5144..16ce4433b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -113,10 +113,13 @@ multiplicity_recurse = function(.multip, .fun, ...) { #' @return An element from the dictionary. #' #' @examples -#' dictionary_sugar_get("pca", id = "pca_1") +#' dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") #' # is the same as -#' dictionary_sugar_get_inc("pca_1") +#' dictionary_sugar_inc_get(mlr_pipeops, "pca_1") +#' # multiple pipeops +#' dictionary_sugar_inc_mget(mlr_pipeops, c("pca_1", "pca_2")) #' +#' @rdname inc_get #' @export dictionary_sugar_inc_get = function(dict, .key, ...) { newkey = gsub("_\\d+$", "", .key) @@ -133,3 +136,17 @@ dictionary_sugar_inc_get = function(dict, .key, ...) { obj } + +#' @name inc_get +dictionary_sugar_inc_mget = function(dict, .keys, ...) { + objs = lapply(.keys, dictionary_sugar_inc_get, dict = dict, ...) + if (!is.null(names(.keys))) { + nn = names2(.keys) + ii = which(!is.na(nn)) + for (i in ii) { + objs[[i]]$id = nn[i] + } + } + names(objs) = map_chr(objs, "id") + objs +} diff --git a/tests/testthat/test_po.R b/tests/testthat/test_po.R index 4b793b60a..fb6fc0559 100644 --- a/tests/testthat/test_po.R +++ b/tests/testthat/test_po.R @@ -211,4 +211,7 @@ test_that("Incrementing ids works", { x = po("learner_1", lrn("regr.rpart")) expect_true(x$id == "regr.rpart_1") expect_r6(x, "PipeOpLearner") + + xs = pos(c("pca_1", "pca_2")) + assert_true(all(names(xs) == c("pca_1", "pca_2"))) }) From 491260bad25821798f5bee09a394821fcb1e31da Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 12:50:53 +0200 Subject: [PATCH 03/12] docs: document --- man/{dictionary_sugar_inc_get.Rd => inc_get.Rd} | 10 ++++++++-- man/mlr_pipeops_nmf.Rd | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) rename man/{dictionary_sugar_inc_get.Rd => inc_get.Rd} (69%) diff --git a/man/dictionary_sugar_inc_get.Rd b/man/inc_get.Rd similarity index 69% rename from man/dictionary_sugar_inc_get.Rd rename to man/inc_get.Rd index 7f8be51b6..a325bafd9 100644 --- a/man/dictionary_sugar_inc_get.Rd +++ b/man/inc_get.Rd @@ -2,9 +2,13 @@ % Please edit documentation in R/utils.R \name{dictionary_sugar_inc_get} \alias{dictionary_sugar_inc_get} +\alias{inc_get} +\alias{dictionary_sugar_inc_mget} \title{A Quick Way to Initialize Objects from Dictionaries with Incremented ID} \usage{ dictionary_sugar_inc_get(dict, .key, ...) + +dictionary_sugar_inc_mget(dict, .keys, ...) } \value{ An element from the dictionary. @@ -15,8 +19,10 @@ of ID clashes. This should only be used on dictionaries that ensure that the ids the pattern \verb{_}. Where \code{n} is any number. } \examples{ -dictionary_sugar_get("pca", id = "pca_1") +dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") # is the same as -dictionary_sugar_get_inc("pca_1") +dictionary_sugar_inc_get(mlr_pipeops, "pca_1") +# multiple pipeops +dictionary_sugar_inc_mget(mlr_pipeops, c("pca_1", "pca_2")) } diff --git a/man/mlr_pipeops_nmf.Rd b/man/mlr_pipeops_nmf.Rd index 5e967fab2..3c8a75c9a 100644 --- a/man/mlr_pipeops_nmf.Rd +++ b/man/mlr_pipeops_nmf.Rd @@ -96,7 +96,7 @@ See \code{\link[NMF:nmf]{nmf()}}. \section{Internals}{ -Uses the \code{\link[NMF:nmf]{nmf()}} function as well as \code{\link[NMF:basis-coef-methods]{basis()}}, \code{\link[NMF:basis-coef-methods]{coef()}} and +Uses the \code{\link[NMF:nmf]{nmf()}} function as well as \code{\link[NMF:basis]{basis()}}, \code{\link[NMF:coef]{coef()}} and \code{\link[MASS:ginv]{ginv()}}. } From 09bd0c876b5a76267f5b1b8a263a864d5c956260 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 12:54:37 +0200 Subject: [PATCH 04/12] export increment version of mget --- NAMESPACE | 1 + R/utils.R | 1 + 2 files changed, 2 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 7322b5065..1d8dd4882 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -111,6 +111,7 @@ export(assert_pipeop) export(chain_graphs) export(concat_graphs) export(dictionary_sugar_inc_get) +export(dictionary_sugar_inc_mget) export(filter_noop) export(greplicate) export(gunion) diff --git a/R/utils.R b/R/utils.R index 16ce4433b..a9a8be103 100644 --- a/R/utils.R +++ b/R/utils.R @@ -138,6 +138,7 @@ dictionary_sugar_inc_get = function(dict, .key, ...) { } #' @name inc_get +#' @export dictionary_sugar_inc_mget = function(dict, .keys, ...) { objs = lapply(.keys, dictionary_sugar_inc_get, dict = dict, ...) if (!is.null(names(.keys))) { From 5b6ad238daa89a41ad132218b91de9a5294f5a89 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 13:33:13 +0200 Subject: [PATCH 05/12] fix: better docu and fix example --- NAMESPACE | 1 + R/utils.R | 20 ++++++++++++++++---- R/zzz.R | 1 + man/inc_get.Rd | 24 ++++++++++++++++++++---- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 1d8dd4882..985e47800 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -151,6 +151,7 @@ import(mlr3misc) import(paradox) importFrom(R6,R6Class) importFrom(digest,digest) +importFrom(methods,hasArg) importFrom(stats,setNames) importFrom(utils,bibentry) importFrom(utils,tail) diff --git a/R/utils.R b/R/utils.R index a9a8be103..51039c8e1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -106,14 +106,26 @@ multiplicity_recurse = function(.multip, .fun, ...) { #' @title A Quick Way to Initialize Objects from Dictionaries with Incremented ID #' @description -#' Covenience function to obtain dictionary elements with incremented ids for easier avoidance -#' of ID clashes. This should only be used on dictionaries that ensure that the ids don't end with -#' the pattern `_`. Where `n` is any number. +#' Covenience wrapper around [mlr3misc::dictionary_sugar_get] and [mlr3misc::dictionary_sugar_inc_mget] +#' to allow easier avoidance of of ID clashes which is for example useful, +#' when multiple instances of the [PipeOp] are in one [Graph]. +#' Let `` be the key of the objet to retrieve. When passing the `_` to this +#' function, where `` is any natural numer, the object with key is retrieved and the +#' suffix `_` is appended to the id after the object is constructed. +#' +#' @param dict ([Dictionary])\cr +#' Dictionary from which to retrieve an element. +#' @param .key (`character(1)`)\cr +#' Key of the object to construct - possibly with a suffix like `_n` to modify its id. +#' @param .keys (`character()`)\cr +#' Keys of the objects to construct - possibly with a suffix like `_n` to modify its id. +#' @param ... (any)\cr +#' See description of [mlr3misc::dictionary_sugar]. #' #' @return An element from the dictionary. #' #' @examples -#' dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") +#' mlr3misc::dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") #' # is the same as #' dictionary_sugar_inc_get(mlr_pipeops, "pca_1") #' # multiple pipeops diff --git a/R/zzz.R b/R/zzz.R index 0573770ba..9604f5c35 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -8,6 +8,7 @@ #' @importFrom digest digest #' @importFrom withr with_options #' @importFrom stats setNames +#' @importFrom methods hasArg "_PACKAGE" register_mlr3 = function() { diff --git a/man/inc_get.Rd b/man/inc_get.Rd index a325bafd9..7a37d7d50 100644 --- a/man/inc_get.Rd +++ b/man/inc_get.Rd @@ -10,16 +10,32 @@ dictionary_sugar_inc_get(dict, .key, ...) dictionary_sugar_inc_mget(dict, .keys, ...) } +\arguments{ +\item{dict}{(\link{Dictionary})\cr +Dictionary from which to retrieve an element.} + +\item{.key}{(\code{character(1)})\cr +Key of the object to construct - possibly with a suffix like \verb{_n} to modify its id.} + +\item{...}{(any)\cr +See description of \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar}.} + +\item{.keys}{(\code{character()})\cr +Keys of the objects to construct - possibly with a suffix like \verb{_n} to modify its id.} +} \value{ An element from the dictionary. } \description{ -Covenience function to obtain dictionary elements with incremented ids for easier avoidance -of ID clashes. This should only be used on dictionaries that ensure that the ids don't end with -the pattern \verb{_}. Where \code{n} is any number. +Covenience wrapper around \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar_get} and \link[mlr3misc:dictionary_sugar_inc_mget]{mlr3misc::dictionary_sugar_inc_mget} +to allow easier avoidance of of ID clashes which is for example useful, +when multiple instances of the \link{PipeOp} are in one \link{Graph}. +Let \verb{} be the key of the objet to retrieve. When passing the \verb{_} to this +function, where \verb{} is any natural numer, the object with key is retrieved and the +suffix \verb{_} is appended to the id after the object is constructed. } \examples{ -dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") +mlr3misc::dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") # is the same as dictionary_sugar_inc_get(mlr_pipeops, "pca_1") # multiple pipeops From 48c2a023ade8d649d4ce3bdb001bfe957b27cd21 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 13:47:22 +0200 Subject: [PATCH 06/12] docs: add methods to DESCRIPTION --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index c36abb942..e49d195d9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -48,6 +48,7 @@ Imports: digest, lgr, mlr3 (>= 0.6.0), + methods, mlr3misc (>= 0.9.0), paradox, R6, From ef04d2c44e891f40b407a87a2292a91787bb3df9 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 14:41:57 +0200 Subject: [PATCH 07/12] fix: dumb mistake --- DESCRIPTION | 1 - R/utils.R | 4 ++-- man/inc_get.Rd | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e49d195d9..c36abb942 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -48,7 +48,6 @@ Imports: digest, lgr, mlr3 (>= 0.6.0), - methods, mlr3misc (>= 0.9.0), paradox, R6, diff --git a/R/utils.R b/R/utils.R index 51039c8e1..8d06196cf 100644 --- a/R/utils.R +++ b/R/utils.R @@ -106,7 +106,7 @@ multiplicity_recurse = function(.multip, .fun, ...) { #' @title A Quick Way to Initialize Objects from Dictionaries with Incremented ID #' @description -#' Covenience wrapper around [mlr3misc::dictionary_sugar_get] and [mlr3misc::dictionary_sugar_inc_mget] +#' Covenience wrapper around [mlr3misc::dictionary_sugar_get] and [mlr3misc::dictionary_sugar_mget] #' to allow easier avoidance of of ID clashes which is for example useful, #' when multiple instances of the [PipeOp] are in one [Graph]. #' Let `` be the key of the objet to retrieve. When passing the `_` to this @@ -137,7 +137,7 @@ dictionary_sugar_inc_get = function(dict, .key, ...) { newkey = gsub("_\\d+$", "", .key) add_suffix = .key != newkey if (add_suffix) { - assert_true(!hasArg("id")) + assert_true(!methods::hasArg("id")) suffix = gsub(newkey, "", .key) } obj = mlr3misc::dictionary_sugar_get(dict = dict, .key = newkey, ...) diff --git a/man/inc_get.Rd b/man/inc_get.Rd index 7a37d7d50..7e489ac5a 100644 --- a/man/inc_get.Rd +++ b/man/inc_get.Rd @@ -27,7 +27,7 @@ Keys of the objects to construct - possibly with a suffix like \verb{_n} to modi An element from the dictionary. } \description{ -Covenience wrapper around \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar_get} and \link[mlr3misc:dictionary_sugar_inc_mget]{mlr3misc::dictionary_sugar_inc_mget} +Covenience wrapper around \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar_get} and \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar_mget} to allow easier avoidance of of ID clashes which is for example useful, when multiple instances of the \link{PipeOp} are in one \link{Graph}. Let \verb{} be the key of the objet to retrieve. When passing the \verb{_} to this From 7da636da3e607e2ba31a67177def5639724cba91 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 15:00:51 +0200 Subject: [PATCH 08/12] fix: remove hasArg import --- NAMESPACE | 1 - R/zzz.R | 1 - 2 files changed, 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 985e47800..1d8dd4882 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -151,7 +151,6 @@ import(mlr3misc) import(paradox) importFrom(R6,R6Class) importFrom(digest,digest) -importFrom(methods,hasArg) importFrom(stats,setNames) importFrom(utils,bibentry) importFrom(utils,tail) diff --git a/R/zzz.R b/R/zzz.R index 9604f5c35..0573770ba 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -8,7 +8,6 @@ #' @importFrom digest digest #' @importFrom withr with_options #' @importFrom stats setNames -#' @importFrom methods hasArg "_PACKAGE" register_mlr3 = function() { From 71d3cfcf48bd47734c56a5646f69b1efd4a11655 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Fri, 30 Sep 2022 15:28:48 +0200 Subject: [PATCH 09/12] fix pkgdown error --- pkgdown/_pkgdown.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index a4650d54a..a5c1b3520 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -83,6 +83,8 @@ reference: - filter_noop - starts_with("as.") - starts_with("is.") + - inc_get + - starts_with("is.") - title: Abstract PipeOps contents: - PipeOpEnsemble From 85f8e134cebe44f2b39e3aa79ddb65a8c4837671 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Mon, 3 Oct 2022 14:03:21 +0200 Subject: [PATCH 10/12] don't expoort sugar for id incrementation --- NAMESPACE | 2 -- R/utils.R | 35 +++-------------------------------- man/inc_get.Rd | 44 -------------------------------------------- 3 files changed, 3 insertions(+), 78 deletions(-) delete mode 100644 man/inc_get.Rd diff --git a/NAMESPACE b/NAMESPACE index 1d8dd4882..a40fd7b44 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -110,8 +110,6 @@ export(assert_graph) export(assert_pipeop) export(chain_graphs) export(concat_graphs) -export(dictionary_sugar_inc_get) -export(dictionary_sugar_inc_mget) export(filter_noop) export(greplicate) export(gunion) diff --git a/R/utils.R b/R/utils.R index 8d06196cf..696f3e24c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -104,41 +104,13 @@ multiplicity_recurse = function(.multip, .fun, ...) { } } -#' @title A Quick Way to Initialize Objects from Dictionaries with Incremented ID -#' @description -#' Covenience wrapper around [mlr3misc::dictionary_sugar_get] and [mlr3misc::dictionary_sugar_mget] -#' to allow easier avoidance of of ID clashes which is for example useful, -#' when multiple instances of the [PipeOp] are in one [Graph]. -#' Let `` be the key of the objet to retrieve. When passing the `_` to this -#' function, where `` is any natural numer, the object with key is retrieved and the -#' suffix `_` is appended to the id after the object is constructed. -#' -#' @param dict ([Dictionary])\cr -#' Dictionary from which to retrieve an element. -#' @param .key (`character(1)`)\cr -#' Key of the object to construct - possibly with a suffix like `_n` to modify its id. -#' @param .keys (`character()`)\cr -#' Keys of the objects to construct - possibly with a suffix like `_n` to modify its id. -#' @param ... (any)\cr -#' See description of [mlr3misc::dictionary_sugar]. -#' -#' @return An element from the dictionary. -#' -#' @examples -#' mlr3misc::dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") -#' # is the same as -#' dictionary_sugar_inc_get(mlr_pipeops, "pca_1") -#' # multiple pipeops -#' dictionary_sugar_inc_mget(mlr_pipeops, c("pca_1", "pca_2")) -#' -#' @rdname inc_get -#' @export +# replace when new mlr3misc version is released https://github.com/mlr-org/mlr3misc/pull/80 dictionary_sugar_inc_get = function(dict, .key, ...) { newkey = gsub("_\\d+$", "", .key) add_suffix = .key != newkey if (add_suffix) { assert_true(!methods::hasArg("id")) - suffix = gsub(newkey, "", .key) + suffix = regmatches(.key, regexpr("_\\d+$", .key)) } obj = mlr3misc::dictionary_sugar_get(dict = dict, .key = newkey, ...) @@ -149,8 +121,7 @@ dictionary_sugar_inc_get = function(dict, .key, ...) { } -#' @name inc_get -#' @export +# replace when new mlr3misc version is released https://github.com/mlr-org/mlr3misc/pull/80 dictionary_sugar_inc_mget = function(dict, .keys, ...) { objs = lapply(.keys, dictionary_sugar_inc_get, dict = dict, ...) if (!is.null(names(.keys))) { diff --git a/man/inc_get.Rd b/man/inc_get.Rd deleted file mode 100644 index 7e489ac5a..000000000 --- a/man/inc_get.Rd +++ /dev/null @@ -1,44 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R -\name{dictionary_sugar_inc_get} -\alias{dictionary_sugar_inc_get} -\alias{inc_get} -\alias{dictionary_sugar_inc_mget} -\title{A Quick Way to Initialize Objects from Dictionaries with Incremented ID} -\usage{ -dictionary_sugar_inc_get(dict, .key, ...) - -dictionary_sugar_inc_mget(dict, .keys, ...) -} -\arguments{ -\item{dict}{(\link{Dictionary})\cr -Dictionary from which to retrieve an element.} - -\item{.key}{(\code{character(1)})\cr -Key of the object to construct - possibly with a suffix like \verb{_n} to modify its id.} - -\item{...}{(any)\cr -See description of \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar}.} - -\item{.keys}{(\code{character()})\cr -Keys of the objects to construct - possibly with a suffix like \verb{_n} to modify its id.} -} -\value{ -An element from the dictionary. -} -\description{ -Covenience wrapper around \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar_get} and \link[mlr3misc:dictionary_sugar_get]{mlr3misc::dictionary_sugar_mget} -to allow easier avoidance of of ID clashes which is for example useful, -when multiple instances of the \link{PipeOp} are in one \link{Graph}. -Let \verb{} be the key of the objet to retrieve. When passing the \verb{_} to this -function, where \verb{} is any natural numer, the object with key is retrieved and the -suffix \verb{_} is appended to the id after the object is constructed. -} -\examples{ -mlr3misc::dictionary_sugar_get(mlr_pipeops, "pca", id = "pca_1") -# is the same as -dictionary_sugar_inc_get(mlr_pipeops, "pca_1") -# multiple pipeops -dictionary_sugar_inc_mget(mlr_pipeops, c("pca_1", "pca_2")) - -} From 0c01bd4f837164dedb3a0e110593dcd60e7a8916 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Mon, 3 Oct 2022 14:27:11 +0200 Subject: [PATCH 11/12] fix pkgdown error --- pkgdown/_pkgdown.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index a5c1b3520..9e556cf79 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -83,7 +83,6 @@ reference: - filter_noop - starts_with("as.") - starts_with("is.") - - inc_get - starts_with("is.") - title: Abstract PipeOps contents: From d916b5c0af31f600d24c82ba71c8dbd9358f4c5c Mon Sep 17 00:00:00 2001 From: mb706 Date: Mon, 3 Oct 2022 19:13:38 +0200 Subject: [PATCH 12/12] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 120984592..8c1074c83 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,5 @@ # mlr3pipelines 0.4.2-9000 +* `po()`, `pos()` can now construct `PipeOp`s with ID postfix `_` to avoid ID clashes. # mlr3pipelines 0.4.2