Skip to content

Commit

Permalink
new functions update_variable_labels_with() (#158)
Browse files Browse the repository at this point in the history
* new functions `update_variable_labels_with()` and `update_value_labels_with()`

allowing to update variable/value labels with a custom function

fix #153

* pkgdown

* typo

* typo

* update github actions

* additional tests

* lint

* wordlist
  • Loading branch information
larmarange authored Mar 28, 2024
1 parent c725f53 commit c50c245
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -47,3 +47,4 @@ jobs:
- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ jobs:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -39,7 +41,7 @@ jobs:

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@4.1.4
uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
Expand Down
79 changes: 79 additions & 0 deletions .github/workflows/pr-commands.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
issue_comment:
types: [created]

name: Commands

jobs:
document:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/document') }}
name: document
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::roxygen2
needs: pr-document

- name: Document
run: roxygen2::roxygenise()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add man/\* NAMESPACE
git commit -m 'Document'
- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

style:
if: ${{ github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'OWNER') && startsWith(github.event.comment.body, '/style') }}
name: style
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/pr-fetch@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- uses: r-lib/actions/setup-r@v2

- name: Install dependencies
run: install.packages("styler")
shell: Rscript {0}

- name: Style
run: styler::style_pkg()
shell: Rscript {0}

- name: commit
run: |
git config --local user.name "$GITHUB_ACTOR"
git config --local user.email "[email protected]"
git add \*.R
git commit -m 'Style'
- uses: r-lib/actions/pr-push@v2
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
23 changes: 21 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
Expand All @@ -27,5 +27,24 @@ jobs:
needs: coverage

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

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Imports:
rlang,
vctrs,
stringr,
tidyr
tidyr,
tidyselect
Suggests:
testthat,
knitr,
Expand All @@ -40,7 +41,7 @@ URL: https://larmarange.github.io/labelled/
BugReports: https://github.com/larmarange/labelled/issues
VignetteBuilder: knitr
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
RdMacros: lifecycle
Language: en-US
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ S3method(update_labelled,default)
S3method(update_labelled,haven_labelled)
S3method(update_labelled,haven_labelled_spss)
S3method(update_labelled,labelled)
S3method(update_value_labels_with,data.frame)
S3method(update_variable_labels_with,data.frame)
S3method(user_na_to_na,data.frame)
S3method(user_na_to_na,default)
S3method(user_na_to_na,haven_labelled_spss)
Expand Down Expand Up @@ -162,6 +164,8 @@ export(to_labelled)
export(unique_tagged_na)
export(unlabelled)
export(update_labelled)
export(update_value_labels_with)
export(update_variable_labels_with)
export(user_na_to_na)
export(user_na_to_regular_na)
export(user_na_to_tagged_na)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
* add a `null_action` argument to `val_labels()`, `val_label()` and a
`.null_action` argument to `set_value_labels()`, `add_value_labels()` and
`remove_value_labels()` (#145)
* new functions `update_variable_labels_with()` and `update_value_labels_with()`
allowing to update variable/value labels with a custom function (#153)

**Bug fix**

* avoid an error with `print.look_for()` when console pane is physically shrunk
too small (#148)
* fix in `recode.haven_labelled()` when `.x` contains `NA` and
`.combine_value_labels = TRUE` (#151)
* producing an error when trying to assign value labels to a Date vector (#156)
* produce an error when trying to assign value labels to a Date vector (#156)

# labelled 2.12.0

Expand Down
82 changes: 82 additions & 0 deletions R/update_with.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#' Update variable/value labels with a function
#' @param .data A data frame, or data frame extension (e.g. a tibble)
#' @param .fn A function used to transform the variable/value labels of the
#' selected `.cols`.
#' @param .cols Columns to update; defaults to all columns. Use tidy selection.
#' @param ... additional arguments passed onto `.fn`.
#' @examples
#' df <- iris %>%
#' set_variable_labels(
#' Sepal.Length = "Length of sepal",
#' Sepal.Width = "Width of sepal",
#' Petal.Length = "Length of petal",
#' Petal.Width = "Width of petal",
#' Species = "Species"
#' )
#' df$Species <- to_labelled(df$Species)
#' df %>% look_for()
#' df %>%
#' update_variable_labels_with(toupper) %>%
#' look_for()
#' df %>%
#' update_variable_labels_with(toupper, .cols = dplyr::starts_with("S")) %>%
#' look_for()
#' @export
update_variable_labels_with <- function(.data,
.fn,
.cols = dplyr::everything(),
...) {
UseMethod("update_variable_labels_with")
}

#' @export
update_variable_labels_with.data.frame <- function(.data,
.fn,
.cols = dplyr::everything(),
...) {
.fn <- rlang::as_function(.fn)
cols <- tidyselect::eval_select(
rlang::enquo(.cols),
.data,
allow_rename = FALSE
)
vl <- var_label(.data)
vl <- vl[names(cols)]
vl <- lapply(vl, .fn, ...)
var_label(.data) <- vl
.data
}

#' @export
#' @rdname update_variable_labels_with
#' @examples
#' df %>%
#' update_value_labels_with(toupper) %>%
#' look_for()
update_value_labels_with <- function(.data,
.fn,
.cols = dplyr::everything(),
...) {
UseMethod("update_value_labels_with")
}

#' @export
update_value_labels_with.data.frame <- function(.data,
.fn,
.cols = dplyr::everything(),
...) {
.fn <- rlang::as_function(.fn)
cols <- tidyselect::eval_select(
rlang::enquo(.cols),
.data,
allow_rename = FALSE
)
for (i in cols) {
vl <- val_labels(.data[[i]])
if (!is.null(vl)) {
names(vl) <- .fn(names(vl), ...)
val_labels(.data[[i]]) <- vl
}
}
.data
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ reference:
- var_label
- remove_var_label
- copy_labels
- update_variable_labels_with
- title: Manipulating value labels
desc: Functions to set, manipulate and remove value labels
contents:
Expand All @@ -22,6 +23,7 @@ reference:
- nolabel_to_na
- drop_unused_value_labels
- copy_labels
- update_variable_labels_with
- title: Data dictionnary
desc: Functions to look for keywords variable names / labels and create a data dictionary
contents:
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ tbl
tibble
tibbles
tidyr
unclass
unclassed
untagged
45 changes: 45 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.

Loading

0 comments on commit c50c245

Please sign in to comment.