Skip to content

Commit

Permalink
Deprecate na.rm
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 9, 2024
1 parent 3e1fd3b commit 9015c58
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 0.20.4.2
Version: 0.20.4.3
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

## General

* `get_datagrid()` can now be used to extract the "grid" information from
* `get_datagrid()` can now be used to extract the "grid" information from
`{emmeans}` and `{marginaleffects}` outputs.

* Argument `na.rm` is deprecated throughout the package's functions. Instead,
use `remove_na`.

# insight 0.20.4

## New supported models
Expand Down
10 changes: 5 additions & 5 deletions R/get_parameters_bayesian.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ get_parameters.MCMCglmm <- function(x,
nF <- x$Fixed$nfl
fixed <- as.data.frame(x$Sol[, 1:nF, drop = FALSE])
random <- as.data.frame(x$VCV[, find_random(x, split_nested = TRUE, flatten = TRUE), drop = FALSE])
all <- cbind(fixed, random)
all_params <- cbind(fixed, random)

out <- if (effects == "fixed") {
fixed
} else if (effects == "random") {
random
} else {
all
all_params
}

if (isTRUE(summary)) {
Expand Down Expand Up @@ -152,9 +152,9 @@ get_parameters.BFBayesFactor <- function(x,
colnames(posteriors) <- "p"
out <- posteriors
} else if (bf_type == "xtable") {
data <- get_data(x, verbose = verbose)
N <- sum(data)
cells <- prod(dim(data))
model_data <- get_data(x, verbose = verbose)
N <- sum(model_data)
cells <- prod(dim(model_data))
posts <- as.data.frame(as.matrix(suppressMessages(
BayesFactor::posterior(x, iterations = iterations, progress = progress)
)))
Expand Down
39 changes: 30 additions & 9 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
#' valid object name.
#'
#' @param x A (character) vector, or for some functions may also be a data frame.
#' @param na.rm Logical, if missing values should be removed from the input.
#' @param remove_na Logical, if missing values should be removed from the input.
#' @param character_only Logical, if `TRUE` and `x` is a data frame or list,
#' only processes character vectors.
#' @param ... Currently not used.
#' @param na.rm Deprecated. Use `remove_na` instead.
#'
#' @return
#' - `n_unique()`: For a vector, `n_unique` always returns an integer value,
Expand Down Expand Up @@ -88,22 +89,37 @@ n_unique <- function(x, ...) {

#' @rdname trim_ws
#' @export
n_unique.default <- function(x, na.rm = TRUE, ...) {
n_unique.default <- function(x, remove_na = TRUE, na.rm = remove_na, ...) {
if (is.null(x)) {
return(0)
}
if (isTRUE(na.rm)) x <- x[!is.na(x)]
## TODO:: remove deprecation warning later
if (!missin(na.rm)) {
format_warning("The `na.rm` argument is deprecated. Use `remove_na` instead.")
remove_na <- na.rm
}
if (isTRUE(remove_na)) x <- x[!is.na(x)]
length(unique(x))
}

#' @export
n_unique.data.frame <- function(x, na.rm = TRUE, ...) {
vapply(x, n_unique, na.rm = na.rm, FUN.VALUE = numeric(1L))
n_unique.data.frame <- function(x, remove_na = TRUE, na.rm = remove_na, ...) {
## TODO:: remove deprecation warning later
if (!missin(na.rm)) {
format_warning("The `na.rm` argument is deprecated. Use `remove_na` instead.")
remove_na <- na.rm
}
vapply(x, n_unique, na.rm = remove_na, FUN.VALUE = numeric(1L))
}

#' @export
n_unique.list <- function(x, na.rm = TRUE, ...) {
lapply(x, n_unique, na.rm = na.rm)
n_unique.list <- function(x, remove_na = TRUE, na.rm = remove_na, ...) {
## TODO:: remove deprecation warning later
if (!missin(na.rm)) {
format_warning("The `na.rm` argument is deprecated. Use `remove_na` instead.")
remove_na <- na.rm
}
lapply(x, n_unique, na.rm = remove_na)
}


Expand Down Expand Up @@ -139,7 +155,12 @@ safe_deparse_symbol <- function(x) {

#' @rdname trim_ws
#' @export
has_single_value <- function(x, na.rm = FALSE) {
if (na.rm) x <- x[!is.na(x)]
has_single_value <- function(x, remove_na = FALSE, na.rm = remove_na, ...) {
## TODO:: remove deprecation warning later
if (!missin(na.rm)) {
format_warning("The `na.rm` argument is deprecated. Use `remove_na` instead.")
remove_na <- na.rm
}
if (remove_na) x <- x[!is.na(x)]
!is.null(x) && length(x) > 0L && isTRUE(all(x == x[1]))
}
8 changes: 5 additions & 3 deletions man/trim_ws.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test_that("has_single_value() works", {

x <- c(NA, 1)
expect_false(has_single_value(x))
expect_true(has_single_value(x, na.rm = TRUE))
expect_true(has_single_value(x, remove_na = TRUE))

x <- c(2, 1)
expect_false(has_single_value(x))
Expand All @@ -65,7 +65,7 @@ test_that("has_single_value() works", {

x <- c(NA, NA)
expect_false(has_single_value(x))
expect_false(has_single_value(x, na.rm = TRUE))
expect_false(has_single_value(x, remove_na = TRUE))
})

test_that("safe_deparse_symbol() works", {
Expand Down

0 comments on commit 9015c58

Please sign in to comment.