Skip to content

Commit

Permalink
Fix docs (especially for coxph) and UI messaging (#261)
Browse files Browse the repository at this point in the history
* Add disclaimer for coxph (plus `markdown = TRUE`)

* Adjust logic for UI messaging

* Add test for UI logic

* Redocument

* Redact real object size for snapshots, just in case

* More readable redaction

* Fix abs value

* No more quotes, which seem weird for numbers in a sentence

* Render README
  • Loading branch information
juliasilge authored Apr 26, 2023
1 parent 71b1ae9 commit 7e162c9
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 31 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ Config/Needs/check:
bioc::mixOmics
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

* Updated methods for `mgcv::gam()` to also remove the `hat` and `offset`
components (@rdavis120, #255).

* Clarified the messaging for butchering results, as well as when butchering
may not work for `survival::coxph()` (#261).

# butcher 0.3.2

Expand Down
31 changes: 31 additions & 0 deletions R/coxph.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@
#'
#' @return Axed coxph object.
#'
#' @details
#' The [survival::coxph()] model is unique in how it uses environments in
#' its components, and butchering such an object can behave in surprising ways
#' in any environment other than the
#' [global environment](https://adv-r.hadley.nz/environments.html#important-environments)
#' (such as when wrapped in a function). We do not recommend that you use
#' `butcher()` with a `coxph` object anywhere other than the global environment.
#'
#' Do this:
#'
#' ```r
#' my_coxph_func <- function(df) {
#' coxph(Surv(time, status) ~ x + strata(covar), df)
#' }
#' ## in global environment only:
#' butcher(my_coxph_func(df))
#' ```
#'
#' Do *not* do this:
#'
#' ```r
#' my_coxph_func <- function(df) {
#' res <- coxph(Surv(time, status) ~ x + strata(covar), df)
#' ## no:
#' butcher(res)
#' }
#'
#' ## will not work correctly:
#' my_coxph_func(df)
#' ```
#'
#' @examplesIf rlang::is_installed("survival")
#' library(survival)
#'
Expand Down
25 changes: 14 additions & 11 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ memory_released <- function(og, butchered) {
old <- lobstr::obj_size(og)
new <- lobstr::obj_size(butchered)
rel <- old - new
rel <- format(rel, big.mark = ",", scientific = FALSE)
if (length(rel) == 1) {
if (rel <= 0) {
if (isTRUE(all.equal(old, new))) {
return(NULL)
} else {
return(rel)
}
return(rel)
} else {
return(NULL)
}
Expand All @@ -35,13 +33,18 @@ assess_object <- function(og, butchered) {
if (is.null(mem)) {
cli::cli_alert_danger("No memory released. Do not butcher.")
} else {
cli::cli_alert_success("Memory released: {.val {mem}}")
if (!is.null(disabled)) {
cli::cli_alert_danger("Disabled: {.code {disabled}}")
}
if (length(class_added) == 0) {
class_name <- "butchered"
cli::cli_alert_danger("Could not add {.cls {class_name}} class")
abs_mem <- format(abs(mem), big.mark = ",", scientific = FALSE)
if (mem < 0) {
cli::cli_alert_danger("The butchered object is {.field {abs_mem}} larger than the original. Do not butcher.")
} else {
cli::cli_alert_success("Memory released: {.field {abs_mem}}")
if (!is.null(disabled)) {
cli::cli_alert_danger("Disabled: {.code {disabled}}")
}
if (length(class_added) == 0) {
class_name <- "butchered"
cli::cli_alert_danger("Could not add {.cls {class_name}} class")
}
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ To remove this (mostly) extraneous component, we can use `axe_env()`:

``` r
cleaned_lm <- butcher::axe_env(big_lm, verbose = TRUE)
#> ✔ Memory released: "8.03 MB"
#> ✔ Memory released: 8.03 MB
```

Comparing it against our `small_lm`, we’ll find:
Expand Down
29 changes: 29 additions & 0 deletions man/axe-coxph.Rd

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

4 changes: 2 additions & 2 deletions man/axe-ipred.Rd

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

2 changes: 1 addition & 1 deletion man/axe-nested_model_fit.Rd

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

8 changes: 4 additions & 4 deletions man/axe-pls.Rd

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

10 changes: 5 additions & 5 deletions man/butcher-package.Rd

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

4 changes: 2 additions & 2 deletions man/butcher_example.Rd

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

10 changes: 5 additions & 5 deletions man/new_model_butcher.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/_snaps/ui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generate expected UI messages

Code
assess_object(big, small)
Message
v Memory released: <redacted>
Code
assess_object(small, small)
Message
x No memory released. Do not butcher.
Code
assess_object(small, big)
Message
x The butchered object is <redacted> larger than the original. Do not butcher.

16 changes: 16 additions & 0 deletions tests/testthat/test-ui.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that("Generate expected UI messages", {
big <- runif(1e4)
big <- add_butcher_class(big)
small <- runif(1e3)
small <- add_butcher_class(small)
obj_size_diff <- lobstr::obj_size(big) - lobstr::obj_size(small)
obj_size_diff <- format(obj_size_diff, big.mark = ",", scientific = FALSE)

expect_snapshot({
assess_object(big, small)
assess_object(small, small)
assess_object(small, big)
},
transform = function(x) gsub(obj_size_diff, "<redacted>", x, fixed = TRUE)
)
})

0 comments on commit 7e162c9

Please sign in to comment.