Skip to content

Commit

Permalink
Merge pull request #128 from selkamand/minor_fixes
Browse files Browse the repository at this point in the history
Minor fixes
  • Loading branch information
selkamand authored Dec 18, 2024
2 parents 97e0fd7 + d4a115a commit 250698a
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 22 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
Config/Needs/website: uwot, datarium, palmerpenguins
Config/testthat/edition: 3
Depends:
R (>= 2.10)
Expand Down
38 changes: 21 additions & 17 deletions R/gg1d.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ utils::globalVariables(".data")
#' @param max_plottable_cols maximum number of columns that can be plotted (default: 15) (number)
#' @param cols_to_plot names of columns in \strong{data} that should be plotted. By default plots all valid columns (character)
#' @param tooltip_column_suffix the suffix added to a column name that indicates column should be used as a tooltip (string)
#' @param ignore_column_regex a regex string that, if matches a column name, will cause that column to be exclude from plotting (string) (default: "_ignore$")
#' @param ignore_column_regex a regex string that, if matches a column name, will cause that column to be excluded from plotting (string). If NULL no regex check will be performed. (default: "_ignore$")
#' @param convert_binary_numeric_to_factor If a numeric column conatins only values 0, 1, & NA, then automatically convert to a factor.
#' @param options a list of additional visual parameters created by calling [gg1d_options()]. See \code{\link{gg1d_options}} for details.
#'
Expand Down Expand Up @@ -93,7 +93,7 @@ gg1d <- function(
assertions::assert_flag(limit_plots)
assertions::assert_flag(desc)
assertions::assert_string(tooltip_column_suffix)
assertions::assert_string(ignore_column_regex)
if(!is.null(ignore_column_regex)) assertions::assert_string(ignore_column_regex)
assertions::assert_class(options, "gg1d_options", msg = "The options argument must be created using {.code gg1d_options()}")
assertions::assert_number(max_plottable_cols)
assertions::assert_greater_than(max_plottable_cols, 0)
Expand Down Expand Up @@ -480,11 +480,14 @@ column_info_table <- function(data, maxlevels = 6, col_id = NULL, cols_to_plot,
# 3) The`cols_to_plot` variable is suppplied and column names are NOT in the list of cols_to_plot
# 4) Their colnames match the _ignore suffix
lgl_too_many_levels <- df_column_info$coltype == "categorical" & df_column_info$ndistinct > maxlevels

df_column_info[["plottable"]] <-
!lgl_too_many_levels & !df_column_info$coltype %in% c("invalid", "id", "tooltip") &
(is.null(cols_to_plot) | df_column_info$colnames %in% c(cols_to_plot)) &
(!grepl(x = df_column_info$colnames, pattern = ignore_column_regex))
(is.null(cols_to_plot) | df_column_info$colnames %in% c(cols_to_plot))

# Only check colnames_match ignore_column_regex suffix if not NULL
if(!is.null(ignore_column_regex))
df_column_info[["plottable"]] <- df_column_info[["plottable"]] & (!grepl(x = df_column_info$colnames, pattern = ignore_column_regex))

if (sum(lgl_too_many_levels) > 0) {
char_cols_with_too_many_levels <- df_column_info$colnames[lgl_too_many_levels]
Expand Down Expand Up @@ -592,6 +595,7 @@ theme_categorical <- function(fontsize_y_title = 12, show_legend = TRUE, show_le
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(size = fontsize_y_title, angle = 0),
axis.title.y.right = element_text(size = fontsize_y_title, angle = 0),
legend.title = if (show_legend_titles) element_text(size = legend_title_size, face = "bold", hjust = 0) else element_blank(),
legend.justification = c(0, 0.5),
legend.margin = ggplot2::margin(0, 0, 0, 0),
Expand Down Expand Up @@ -725,19 +729,19 @@ beautify <- function(string, autodetect_units = TRUE) {

# Autodetect units (and move to brackets)
if (autodetect_units) {
string <- sub("\\bm\\b", "(m)", string)
string <- sub("\\bmm\\b", "(mm)", string)
string <- sub("\\cm\\b", "(cm)", string)
string <- sub("\\km\\b", "(km)", string)
string <- sub("\\bg\\b", "(g)", string)
string <- sub("\\bkg\\b", "(kg)", string)
string <- sub("\\bmg\\b", "(mm)", string)
string <- sub("\\boz\\b", "(oz)", string)
string <- sub("\\blb\\b", "(lb)", string)
string <- sub("\\bin\\b", "(in)", string)
string <- sub("\\bft\\b", "(ft)", string)
string <- sub("\\byd\\b", "(yd)", string)
string <- sub("\\bmi\\b", "(mi)", string)
string <- sub("\\sm(\\s|$)", " (m)", string)
string <- sub("\\smm(\\s|$)", " (mm)", string)
string <- sub("\\sm(\\s|$)", " (cm)", string)
string <- sub("\\sm(\\s|$)", " (km)", string)
string <- sub("\\sg(\\s|$)", " (g)", string)
string <- sub("\\skg(\\s|$)", " (kg)", string)
string <- sub("\\smg(\\s|$)", " (mm)", string)
string <- sub("\\soz(\\s|$)", " (oz)", string)
string <- sub("\\slb(\\s|$)", " (lb)", string)
string <- sub("\\sin(\\s|$)", " (in)", string)
string <- sub("\\sft(\\s|$)", " (ft)", string)
string <- sub("\\syd(\\s|$)", " (yd)", string)
string <- sub("\\smi(\\s|$)", " (mi)", string)
}


Expand Down
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ knitr::opts_chunk$set(
)
```

# gg1d <a href="https://selkamand.github.io/gg1d/"><img src="man/figures/logo.png" align="right" height="120" alt="gg1d website" /></a>
# gg1d <a href="https://selkamand.github.io/gg1d/"><img src="man/figures/logo.png" align="right" height="138" alt="gg1d website" /></a>

<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/gg1d)](https://CRAN.R-project.org/package=gg1d)
[![R-CMD-check](https://github.com/selkamand/gg1d/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/selkamand/gg1d/actions/workflows/R-CMD-check.yaml)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![Codecov test coverage](https://codecov.io/gh/selkamand/gg1d/branch/main/graph/badge.svg)](https://app.codecov.io/gh/selkamand/gg1d?branch=main)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# gg1d <a href="https://selkamand.github.io/gg1d/"><img src="man/figures/logo.png" align="right" height="120" alt="gg1d website" /></a>
# gg1d <a href="https://selkamand.github.io/gg1d/"><img src="man/figures/logo.png" align="right" height="138" alt="gg1d website" /></a>

<!-- badges: start -->

[![CRAN
status](https://www.r-pkg.org/badges/version/gg1d)](https://CRAN.R-project.org/package=gg1d)
[![R-CMD-check](https://github.com/selkamand/gg1d/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/selkamand/gg1d/actions/workflows/R-CMD-check.yaml)
[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
Expand Down
2 changes: 1 addition & 1 deletion man/column_info_table.Rd

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

Binary file modified man/figures/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/gg1d.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-beautify.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ test_that("beautify works as expected", {
"flipper_length_mm" = "Flipper Length (mm)",
"body_mass_g" = "Body Mass (g)",
"kgtest_kg" = "Kgtest (kg)",
"sex" = "Sex"
"sex" = "Sex",
"Length (mm)" = "Length (mm)"
)

expect_equal(examples, beautify(names(examples)), ignore_attr = TRUE)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-gg1d.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ test_that("gg1d applies ignore_column_regex", {
expect_false("IgnoreMe_ignore" %in% result$colnames[result$plottable])
})

test_that("gg1d does not check any regex when ignore_column_regex = NULL", {
data <- mock_data
colnames(data)[2] <- "Do_NOT_IgnoreMe_ignore"

# Runs without error
expect_no_error(gg1d(data = data, ignore_column_regex = NULL, return = "column_info", verbose = FALSE))

# Gets the expected result
result <- gg1d(data = data, ignore_column_regex = NULL, return = "column_info", verbose = FALSE)
expect_true("Do_NOT_IgnoreMe_ignore" %in% result$colnames[result$plottable])
})

test_that("gg1d limits the number of plottable columns", {
data <- mock_data
for (i in 1:20) {
Expand Down

0 comments on commit 250698a

Please sign in to comment.