Skip to content

Update CI + add example #591

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 2 additions & 5 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

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

permissions: read-all

Expand Down Expand Up @@ -43,9 +42,7 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::sf
any::rcmdcheck
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
release:
types: [published]
workflow_dispatch:

name: pkgdown
name: pkgdown.yaml

permissions: read-all

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
paths: ["**.[rR]", "**.[qrR]md", "**.[rR]markdown", "**.[rR]nw", "**.[rR]profile"]

name: Style
name: style.yaml

permissions: read-all

Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

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

permissions: read-all

Expand All @@ -25,10 +24,7 @@ jobs:

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

- name: Test coverage
Expand All @@ -43,7 +39,8 @@ jobs:

- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }}
# Fail if error if not on PR, or if on PR and token is given
fail_ci_if_error: ${{ github.event_name != 'pull_request' || secrets.CODECOV_TOKEN }}
file: ./cobertura.xml
plugin: noop
disable_search: true
Expand Down
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ Suggests:
rmarkdown,
RSQLite,
sf,
testthat (>= 3.0.0),
testthat (>= 3.2.0),
tibble,
tidygraph
VignetteBuilder:
knitr
Config/testthat/edition: 3
Config/Needs/check: sf
Config/Needs/coverage: sf
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
Expand Down
15 changes: 7 additions & 8 deletions R/clean_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' support using `clean_names()` on `sf` and `tbl_graph` (from
#' `tidygraph`) objects as well as on database connections through
#' `dbplyr`. For cleaning other named objects like named lists
#' and vectors, use `make_clean_names()`. When `set_labels` is set to `TRUE`, the old names,
#' and vectors, use `make_clean_names()`. When `set_labels` is set to `TRUE`, the old names,
#' stored as column labels, can be restored using `sjlabelled::label_to_colnames()`.
#'
#' @export
Expand Down Expand Up @@ -83,14 +83,13 @@ clean_names.default <- function(dat, ..., set_labels = FALSE) {
if (is.null(names(dat))) {
dimnames(dat) <- lapply(dimnames(dat), make_clean_names, ...)
} else {
if (set_labels){
if (set_labels) {
old_names <- names(dat)
for (i in seq_along(old_names)){
for (i in seq_along(old_names)) {
attr(dat[[i]], "label") <- old_names[[i]]
}
}
names(dat) <- make_clean_names(names(dat), ...)

}
dat
}
Expand All @@ -112,9 +111,9 @@ clean_names.sf <- function(dat, ..., set_labels = FALSE) {
sf_cleaned <- make_clean_names(sf_names[cols_to_rename], ...)
# rename original df
names(dat)[cols_to_rename] <- sf_cleaned
if(set_labels){
for (i in seq_along(sf_names[cols_to_rename])){

if (set_labels) {
for (i in seq_along(sf_names[cols_to_rename])) {
attr(dat[[i]], "label") <- sf_names[[i]]
}
}
Expand All @@ -131,7 +130,7 @@ clean_names.tbl_graph <- function(dat, ...) {
call. = FALSE
)
} # nocov end

dplyr::rename_all(dat, .funs = make_clean_names, ...)
}

Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ options(width = 110)

<!-- badges: start -->
[![R-CMD-check](https://github.com/sfirke/janitor/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/sfirke/janitor/actions/workflows/R-CMD-check.yaml)
[![Coverage Status](https://img.shields.io/codecov/c/github/sfirke/janitor/main.svg)](https://app.codecov.io/github/sfirke/janitor?branch=main)
[![Codecov test coverage](https://codecov.io/gh/sfirke/janitor/graph/badge.svg)](https://app.codecov.io/gh/sfirke/janitor)
[![lifecycle](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version-ago/janitor)](https://cran.r-project.org/package=janitor)
![!Monthly Downloads](https://cranlogs.r-pkg.org/badges/janitor)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<!-- badges: start -->

[![R-CMD-check](https://github.com/sfirke/janitor/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/sfirke/janitor/actions/workflows/R-CMD-check.yaml)
[![Coverage
Status](https://img.shields.io/codecov/c/github/sfirke/janitor/main.svg)](https://app.codecov.io/github/sfirke/janitor?branch=main)
[![Codecov test
coverage](https://codecov.io/gh/sfirke/janitor/graph/badge.svg)](https://app.codecov.io/gh/sfirke/janitor)
[![lifecycle](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version-ago/janitor)](https://cran.r-project.org/package=janitor)
![!Monthly Downloads](https://cranlogs.r-pkg.org/badges/janitor)
Expand Down
3 changes: 3 additions & 0 deletions janitor.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
Expand Down
4 changes: 2 additions & 2 deletions man/remove_empty.Rd

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

27 changes: 13 additions & 14 deletions tests/testthat/test-clean-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ test_that("labels are created in default method (feature request #563)", {
dat_df <- dplyr::tibble(`a a` = c(11, 22), `b b` = c(2, 3))
dat_df_clean_labels <- clean_names(dat_df, set_labels = TRUE)
dat_df_clean <- clean_names(dat_df)
for (i in seq_along(names(dat_df))){

for (i in seq_along(names(dat_df))) {
# check that old names are saved as labels when set_labels is TRUE
expect_equal(attr(dat_df_clean_labels[[i]], "label"), names(dat_df)[[i]])
# check that old names are not stored if set_labels is not TRUE
expect_null(attr(dat_df_clean[[i]], "label"))
}
}

# expect names are always cleaned
expect_equal(names(dat_df_clean), c("a_a", "b_b"))
expect_equal(names(dat_df_clean_labels), c("a_a", "b_b"))
Expand Down Expand Up @@ -482,9 +482,8 @@ test_that("Names are cleaned appropriately without attaching sf", {
# last column name
issue_578_sf <- readRDS("testdata/issue-578-sf.rds")
issue_578_sf_clean <- clean_names(issue_578_sf)
expect_error(
print(issue_578_sf_clean),
NA
expect_no_error(
print(issue_578_sf_clean)
)
})

Expand Down Expand Up @@ -605,19 +604,19 @@ test_that("Tests for cases beyond default snake for sf objects", {

test_that("labels are created in sf method (feature request #563)", {
skip_if_not_installed("sf")

dat_df <- dplyr::tibble(`a a` = c(11, 22), `b b` = c(2, 3))
dat_sf <- dat_df
dat_sf$x <- c(1,2)
dat_sf$y <- c(1,2)
dat_sf$x <- c(1, 2)
dat_sf$y <- c(1, 2)
dat_sf <- sf::st_as_sf(dat_sf, coords = c("x", "y"))
dat_sf_clean_labels <- clean_names(dat_sf, set_labels = TRUE)
dat_sf_clean <- clean_names(dat_sf)
for (i in seq_along(names(dat_df))){

for (i in seq_along(names(dat_df))) {
# check that old names are saved as labels when set_labels is TRUE
expect_equal(attr(dat_sf_clean_labels[[i]], "label"), names(dat_sf)[[i]])

# check that old names are not stored if set_labels is not TRUE
expect_null(attr(dat_sf_clean[[i]], "label"))
}
Expand Down Expand Up @@ -724,5 +723,5 @@ test_that("groupings are preserved, #260", {
df_grouped <- iris %>% dplyr::group_by(Sepal.Length, Sepal.Width) # nonsense for analysis but doesn't matter
df_grouped_renamed <- df_grouped %>% clean_names(case = "lower_camel")
expect_equal(dplyr::group_vars(df_grouped_renamed), c("sepalLength", "sepalWidth")) # group got renamed
expect_equal(names(df_grouped_renamed), c("sepalLength", "sepalWidth", "petalLength", "petalWidth", "species"))
expect_named(df_grouped_renamed, c("sepalLength", "sepalWidth", "petalLength", "petalWidth", "species"))
})
15 changes: 13 additions & 2 deletions vignettes/janitor.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ output:
toc: true
toc_depth: 3
vignette: >
%\VignetteIndexEntry{janitor}
%\VignetteIndexEntry{Overview of janitor functions}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Expand All @@ -15,7 +15,7 @@ vignette: >
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(janitor)
```
The janitor functions expedite the initial data exploration and cleaning that comes with any new data set. This catalog describes the usage for each function.
The janitor functions expedite the initial data exploration and cleaning that comes with any new data set. This catalog describes the usage for each function.

# Major functions
Functions for everyday use.
Expand Down Expand Up @@ -212,6 +212,17 @@ For instance, here a vector with a date and an Excel datetime sees both values s
convert_to_date(c("2020-02-29", "40000.1"))
```

### Coerce different strings together.

`paste_skip_na()` can be used to skip `NA` values when coercing multiple columns together.
This is useful since `paste()` just pastes `"NA"` instead of ignoring it.

```{r}
paste("x", NA, "y")

paste_skip_na("x", NA, "y")
```

### Elevate column names stored in a data.frame row

If a data.frame has the intended variable names stored in one of its rows, `row_to_names()` will elevate the specified row to become the names of the data.frame and optionally (by default) remove the row in which names were stored and/or the rows above it.
Expand Down
Loading