Skip to content

Commit

Permalink
Merge branch 'main' into n_in_data_modify
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Sep 9, 2024
2 parents 96d4a71 + 0d30d24 commit 6f83f33
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 17 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: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.12.2.2
Version: 0.12.3.4
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
16 changes: 15 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# datawizard (development)

CHANGES

* The `pattern` argument in `data_rename()` can also be a named vector. In this
case, names are used as values for the `replacement` argument (i.e. `pattern`
can be a character vector using `<new name> = "<old name>"`).

# datawizard 0.12.3

CHANGES

* `demean()` (and `degroup()`) now also work for nested designs, if argument
`nested = TRUE` and `by` specifies more than one variable.
`nested = TRUE` and `by` specifies more than one variable (#533).

* Vignettes are no longer provided in the package, they are now only available
on the website. There is only one "Overview" vignette available in the package,
it contains links to the other vignettes on the website. This is because there
are CRAN errors occurring when building vignettes on macOS and we couldn't
determine the cause after multiple patch releases (#534).

* `data_modify()` now recognizes `n()`, for example to create an index for data groups
with `1:n()` (#535).
Expand Down
13 changes: 12 additions & 1 deletion R/data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
#' @param pattern Character vector. For `data_rename()`, indicates columns that
#' should be selected for renaming. Can be `NULL` (in which case all columns
#' are selected). For `data_addprefix()` or `data_addsuffix()`, a character
#' string, which will be added as prefix or suffix to the column names.
#' string, which will be added as prefix or suffix to the column names. For
#' `data_rename()`, `pattern` can also be a named vector. In this case, names
#' are used as values for the `replacement` argument (i.e. `pattern` can be a
#' character vector using `<new name> = "<old name>"`).
#' @param replacement Character vector. Indicates the new name of the columns
#' selected in `pattern`. Can be `NULL` (in which case column are numbered
#' in sequential order). If not `NULL`, `pattern` and `replacement` must be
Expand All @@ -33,6 +36,9 @@
#' head(data_rename(iris, "FakeCol", "length")) # This doesn't
#' head(data_rename(iris, c("Sepal.Length", "Sepal.Width"), c("length", "width")))
#'
#' # use named vector to rename
#' head(data_rename(iris, c(length = "Sepal.Length", width = "Sepal.Width")))
#'
#' # Reset names
#' head(data_rename(iris, NULL))
#'
Expand Down Expand Up @@ -66,6 +72,11 @@ data_rename <- function(data,
insight::format_error("Argument `pattern` must be of type character.")
}

# check if `pattern` has names, and if so, use as "replacement"
if (!is.null(names(pattern))) {
replacement <- names(pattern)
}

# name columns 1, 2, 3 etc. if no replacement
if (is.null(replacement)) {
replacement <- paste0(seq_along(pattern))
Expand Down
7 changes: 6 additions & 1 deletion R/reshape_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,20 @@ reshape_ci <- function(x, ci_type = "CI") {
# Reshape
if (length(unique(x$CI)) > 1) {
if ("Parameter" %in% names(x)) {
idvar <- "Parameter"
remove_parameter <- FALSE
} else if (!is.null(attr(x, "idvars"))) {

Check warning on line 48 in R/reshape_ci.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/reshape_ci.R,line=48,col=18,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
idvar <- attr(x, "idvars")
remove_parameter <- FALSE
} else {
idvar <- "Parameter"
x$Parameter <- NA
remove_parameter <- TRUE
}

x <- stats::reshape(
x,
idvar = "Parameter",
idvar = idvar,
timevar = "CI",
direction = "wide",
v.names = c(ci_low, ci_high),
Expand Down
9 changes: 5 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ We checked 18 reverse dependencies, comparing R CMD check results across CRAN an

## Other comments

This is another patch release that should fix a failure when building vignettes.
This only happens on macOS with R 4.3. We tried to reproduce this locally and in
CI with the same setup, but we couldn't. Hence, we removed an optional dependency
that might have been the problem.
This is a patch release that should (hopefully) fix a failure occurring on macOS
when building vignettes. This only happens on macOS with R 4.3. We tried to
reproduce this locally and in CI with the same setup, but we couldn't. Hence, we
removed all vignettes (except for one "Overview"), they are now only available
on the website.
8 changes: 7 additions & 1 deletion man/data_rename.Rd

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

5 changes: 4 additions & 1 deletion man/text_format.Rd

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

12 changes: 10 additions & 2 deletions tests/testthat/test-data_rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ test_that("data_rename works with one or several replacements", {
),
c("length", "width", "Petal.Length", "Petal.Width", "Species")
)
expect_named(
data_rename(test, c(length = "Sepal.Length", width = "Sepal.Width")),
c("length", "width", "Petal.Length", "Petal.Width", "Species")
)
})

test_that("data_rename returns a data frame", {
Expand Down Expand Up @@ -42,7 +46,9 @@ test_that("data_rename uses indices when no replacement", {

test_that("data_rename works when too many names in 'replacement'", {
expect_message(
x <- data_rename(test, replacement = paste0("foo", 1:6)),
{
x <- data_rename(test, replacement = paste0("foo", 1:6))
},
"There are more names in"
)
expect_identical(dim(test), dim(x))
Expand All @@ -51,7 +57,9 @@ test_that("data_rename works when too many names in 'replacement'", {

test_that("data_rename works when not enough names in 'replacement'", {
expect_message(
x <- data_rename(test, replacement = paste0("foo", 1:2)),
{
x <- data_rename(test, replacement = paste0("foo", 1:2))
},
"There are more names in"
)
expect_identical(dim(test), dim(x))
Expand Down
7 changes: 2 additions & 5 deletions vignettes/overview_of_vignettes.Rmd
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
---
title: "Overview of Vignettes"
output:
rmarkdown::html_vignette:
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Overview of Vignettes}
\usepackage[utf8]{inputenc}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
%\VignetteEncoding{UTF-8}
---

```{r message=FALSE, warning=FALSE, include=FALSE}
Expand Down

0 comments on commit 6f83f33

Please sign in to comment.