Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repr #282

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

repr #282

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ Suggests:
fastICA,
kernlab,
smotefamily
Remotes:
mlr-org/mlr3misc@repr,
mlr-org/mlr3@repr
VignetteBuilder:
knitr
ByteCompile: true
Expand Down
2 changes: 2 additions & 0 deletions R/PipeOp.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
#' @family PipeOps
#' @export
PipeOp = R6Class("PipeOp",
inherits = "Representable_In_Dict",
public = list(
packages = NULL,
state = NULL,
Expand All @@ -206,6 +207,7 @@ PipeOp = R6Class("PipeOp",
self$input = assert_connection_table(input)
self$output = assert_connection_table(output)
self$packages = assert_character(packages, any.missing = FALSE, unique = TRUE)
super$initialize()
},

print = function(...) {
Expand Down
31 changes: 3 additions & 28 deletions R/Selector.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,9 @@ make_selector = function(fun, description, ...) {
)
}

# Representation of character vector
# letters[1] --> '"a"'
# letters[1:2] --> 'c("a", "b")'
char_repr = function(x) {
output = str_collapse(x, sep = ", ", quote = '"')
if (length(x) == 0) {
"character(0)"
} else if (length(x) == 1) {
output
} else {
sprintf("c(%s)", output)
}
}

# Representation for a function that may or may not be a `Selector`.
# If it is not, we just use deparse(), otherwise we use the repr as
# reported by that selector.
selector_repr = function(selector) {
if (test_string(attr(selector, "repr"))) {
attr(selector, "repr")
} else {
str_collapse(deparse(selector), sep = "\n")
}
}

#' @export
print.Selector = function(x, ...) {
cat(paste0(attr(x, "repr"), "\n"))
cat(paste0(repr(x), "\n"))
}

#' @describeIn Selector `selector_all` selects all features.
Expand All @@ -128,7 +103,7 @@ selector_type = function(types) {
assert_subset(types, mlr_reflections$task_feature_types)
make_selector(function(task) {
task$feature_types[get("type") %in% types, get("id")]
}, "selector_type(%s)", char_repr(types))
}, "selector_type(%s)", repr(types))
}

#' @describeIn Selector `selector_grep` selects features with names matching the `grep()` pattern.
Expand Down Expand Up @@ -169,7 +144,7 @@ selector_name = function(feature_names, assert_present = FALSE) {
assert_subset(feature_names, task$feature_names)
}
intersect(task$feature_names, feature_names)
}, "selector_name(%s%s)", char_repr(feature_names), str_assert_present)
}, "selector_name(%s%s)", repr(feature_names), str_assert_present)
}

#' @describeIn Selector `selector_invert` inverts a given [`Selector`]: It always selects the features
Expand Down