Skip to content

Commit

Permalink
feat: strip sourceref generic
Browse files Browse the repository at this point in the history
  • Loading branch information
sebffischer committed Feb 6, 2024
2 parents a2c3d76 + 2e0d5de commit c48fb32
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Config/testthat/parallel: true
Encoding: UTF-8
NeedsCompilation: yes
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3.9000
RoxygenNote: 7.3.1
Collate:
'Dictionary.R'
'named_list.R'
Expand Down Expand Up @@ -97,6 +97,7 @@ Collate:
'str_collapse.R'
'str_indent.R'
'str_trunc.R'
'strip_srcrefs.R'
'to_decimal.R'
'topo_sort.R'
'transpose.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ S3method(remove_named,data.frame)
S3method(remove_named,data.table)
S3method(remove_named,default)
S3method(remove_named,environment)
S3method(strip_srcrefs,"function")
S3method(strip_srcrefs,default)
export("%nin%")
export("get_private<-")
export(Callback)
Expand Down Expand Up @@ -156,6 +158,7 @@ export(stopf)
export(str_collapse)
export(str_indent)
export(str_trunc)
export(strip_srcrefs)
export(to_decimal)
export(topo_sort)
export(transpose_list)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# mlr3misc (development version)

* Feat: Added `strip_screfs` S3 generic, which removes source references from objects

# mlr3misc 0.14.0

* Added argument `.compile` to function `crate()` because R disables byte-code
Expand Down
29 changes: 29 additions & 0 deletions R/strip_srcrefs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' @title Strip source references from objects
#'
#' @description
#' Source references can make objects unexpectedly large and are undesireable in many situations.
#' As {renv} installs packages with the `--with-keep.source` option, we sometimes need to remove source references
#' from objects.
#' Methods should remove source references from the input, but should otherwise leave the input unchanged.
#'
#' @param x (any)\cr
#' The object to strip of source references.
#' @param ... (any)\cr
#' Additional arguments to the method.
#'
#' @keywords internal
#' @export
strip_srcrefs = function(x, ...) {
UseMethod("strip_srcrefs")
}

#' @export
strip_srcrefs.default = function(x, ...) {
x
}

#' @export
strip_srcrefs.function = function(x, ...) {
attr(x, "srcref") = NULL
x
}
22 changes: 22 additions & 0 deletions man/strip_srcrefs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test_strip_srcrefs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("remove srcref from function", {
f = function(x) NULL
attr(f, "srcref") = "mock_srcrefs"
f_strip = strip_srcrefs(f)
expect_true(is.null(attr(f_strip, "srcref")))
})

0 comments on commit c48fb32

Please sign in to comment.