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

in update_variable_labels_with(), it is now possible to access the variable name #164

Merged
merged 3 commits into from
Oct 3, 2024
Merged
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
4 changes: 3 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ on:
pull_request:
branches: [main, master]

name: R-CMD-check
name: R-CMD-check.yaml

permissions: read-all

jobs:
R-CMD-check:
Expand Down
46 changes: 0 additions & 46 deletions .github/workflows/R-CMD-historic-R-check.yaml

This file was deleted.

4 changes: 3 additions & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ on:
pull_request:
branches: [main, master]

name: lint
name: lint.yaml

permissions: read-all

jobs:
lint:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ on:
types: [published]
workflow_dispatch:

name: pkgdown
name: pkgdown.yaml

permissions: read-all

jobs:
pkgdown:
Expand Down
79 changes: 0 additions & 79 deletions .github/workflows/pr-commands.yaml

This file was deleted.

18 changes: 18 additions & 0 deletions .github/workflows/recheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
workflow_dispatch:
inputs:
which:
type: choice
description: Which dependents to check
options:
- strong
- most

name: Reverse dependency check

jobs:
revdep_check:
name: Reverse check ${{ inputs.which }} dependents
uses: r-devel/recheck/.github/workflows/recheck.yml@v1
with:
which: ${{ inputs.which }}
17 changes: 14 additions & 3 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ on:
pull_request:
branches: [main, master]

name: test-coverage
name: test-coverage.yaml

permissions: read-all

jobs:
test-coverage:
Expand All @@ -23,18 +25,27 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
covr::codecov(
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }}
file: ./cobertura.xml
plugin: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ URL: https://larmarange.github.io/labelled/, https://github.com/larmarange/label
BugReports: https://github.com/larmarange/labelled/issues
VignetteBuilder: knitr
LazyData: true
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
RdMacros: lifecycle
Language: en-US
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# labelled (development version)

**New features**

* in `update_variable_labels_with()`, it is now possible to access the variable
name inside `.fn` by using `names()` (#163)
* `var_label()` gets new options `"na"` and `"empty"` for `null_action`

# labelled 2.13.0

**New features**
Expand Down
23 changes: 22 additions & 1 deletion R/update_with.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#' selected `.cols`.
#' @param .cols Columns to update; defaults to all columns. Use tidy selection.
#' @param ... additional arguments passed onto `.fn`.
#' @details
#' For `update_variable_labels_with()`, it is possible to access the name of
#' the variable inside `.fn` by using `names()`, i.e. `.fn` receive a named
#' character vector (see example). `.fn` can return `as.character(NA)` to
#' remove a variable label.
#' @examples
#' df <- iris %>%
#' set_variable_labels(
Expand All @@ -18,6 +23,12 @@
#' df %>%
#' update_variable_labels_with(toupper) %>%
#' look_for()
#'
#' # accessing variable names with names()
#' df %>%
#' update_variable_labels_with(function(x){tolower(names(x))}) %>%
#' look_for()
#'
#' df %>%
#' update_variable_labels_with(toupper, .cols = dplyr::starts_with("S")) %>%
#' look_for()
Expand All @@ -40,9 +51,19 @@ update_variable_labels_with.data.frame <- function(.data,
.data,
allow_rename = FALSE
)
vl <- var_label(.data)
vl <- var_label(.data, null_action = "na")
vl <- vl[names(cols)]

vl <- mapply(
function(variable, label) {
setNames(label, variable)
},
names(vl),
vl,
SIMPLIFY = FALSE
)
vl <- lapply(vl, .fn, ...)
vl <- lapply(vl, unname)
var_label(.data) <- vl
.data
}
Expand Down
24 changes: 22 additions & 2 deletions R/var_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#' @param unlist for data frames, return a named vector instead of a list
#' @param null_action for data frames, by default `NULL` will be returned for
#' columns with no variable label. Use `"fill"` to populate with the column name
#' instead, or `"skip"` to remove such values from the returned list.
#' instead, `"skip"` to remove such values from the returned list, `"na"` to
#' populate with `NA` or `"empty"` to populate with an empty string (`""`).
#' @param recurse if `TRUE`, will apply `var_label()` on packed columns
#' (see [tidyr::pack()]) to return the variable labels of each sub-column;
#' otherwise, the label of the group of columns will be returned.
Expand Down Expand Up @@ -70,7 +71,8 @@ var_label.default <- function(x, ...) {
#' @export
var_label.data.frame <- function(x,
unlist = FALSE,
null_action = c("keep", "fill", "skip"),
null_action =
c("keep", "fill", "skip", "na", "empty"),
recurse = FALSE,
...) {
if (recurse) {
Expand Down Expand Up @@ -98,6 +100,24 @@ var_label.data.frame <- function(x,
)
}

if (null_action == "empty") {
r <- lapply(
r,
function(x) {
if (is.null(x)) "" else x
}
)
}

if (null_action == "na") {
r <- lapply(
r,
function(x) {
if (is.null(x)) as.character(NA) else x
}
)
}

if (null_action == "skip") {
r <- r[!sapply(r, is.null)]
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://
[![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/labelled)](https://cran.r-project.org/package=labelled)
[![Downloads](https://cranlogs.r-pkg.org/badges/labelled)](https://cran.r-project.org/package=labelled)
[![DOI](https://www.zenodo.org/badge/38772078.svg)](https://zenodo.org/badge/latestdoi/38772078)
[![Codecov test coverage](https://codecov.io/gh/larmarange/labelled/branch/main/graph/badge.svg)](https://app.codecov.io/gh/larmarange/labelled?branch=main)
[![Codecov test coverage](https://codecov.io/gh/larmarange/labelled/graph/badge.svg)](https://app.codecov.io/gh/larmarange/labelled)
<!-- [![Rdoc](https://www.rdocumentation.org/badges/version/labelled)](https://www.rdocumentation.org/packages/labelled) -->
<!-- badges: end -->

Expand Down
12 changes: 12 additions & 0 deletions man/update_variable_labels_with.Rd

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

5 changes: 3 additions & 2 deletions man/var_label.Rd

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

Loading
Loading