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

adorn_totals() attempting to add additional "Total" factor level #529

Closed
egozoglu opened this issue Feb 8, 2023 · 5 comments · Fixed by #531
Closed

adorn_totals() attempting to add additional "Total" factor level #529

egozoglu opened this issue Feb 8, 2023 · 5 comments · Fixed by #531
Assignees
Labels
next-release include in next release (bugfix, minor, or major)

Comments

@egozoglu
Copy link

egozoglu commented Feb 8, 2023

Bug reports

adorn_totals() attempting to add additional "Total" factor level, leading to error.

adorn_totals() is attempting to add factor layer "Total" if a table was subject to adorn_totals() previously. This leads to error as "Total" layer already exist for the factor.

library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

test <-iris |>
  mutate(Species = factor(Species, levels = c("setosa", "versicolor", "virginica")))

summary_1 <- test |>
  group_by(Species) |>
  select(Species, everything()) |>
  adorn_totals()

summary_2 <- summary_1 |>
  filter(Species != "Total")

summary_3 <- summary_2 |>
  group_by(Species) |>
  select(Species, everything()) |>
  adorn_totals()
#> Error in `levels<-`(`*tmp*`, value = as.character(levels)): factor level [5] is duplicated
@sfirke
Copy link
Owner

sfirke commented Feb 8, 2023

Makes sense, I can see how the change I introduced just before 2.2.0 in #494 would have caused this. And I think it's an easy fix.

I can patch it but I'm curious about a workflow that calls adorn_totals() twice.

@sfirke sfirke self-assigned this Feb 8, 2023
@sfirke sfirke added the next-release include in next release (bugfix, minor, or major) label Feb 8, 2023
@egozoglu
Copy link
Author

egozoglu commented Feb 8, 2023

I think a check of whether "name" already exists in levels prior to re-leveling at adorn_totals.R Line 137 could potentially solve the issue.

Re: workflow example calling adorn_totals() twice - real life case is actually coming from joining two tables where one of them was previously subject to adorn_totals(). When adorn_totals() is applied to this new joined table, error was thrown.

@sfirke
Copy link
Owner

sfirke commented Feb 8, 2023

I think this will do it: #531 Indeed, I'm targeting the line you suggest. Thanks for looking through the codebase!

@sfirke
Copy link
Owner

sfirke commented Feb 8, 2023

@egozoglu when you get a chance can you install the latest janitor from Github and confirm that this addresses your issue?

@egozoglu
Copy link
Author

egozoglu commented Feb 9, 2023

Thanks for the quick fix! I think this solves the issue.

While testing different use cases - I saw that when adorn_totals() is called twice on the table, it also results in I think in an handled error (Line 67). While I understand this, I think there are cases where someone might want to "refresh" the totals. There might be a better way to do this, however, instead of an "error", would that be possible to convert this to a verbose "reminder" and still proceed with updating the "total" row?

Example below:

library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
test <-iris |>
  mutate(Species = factor(Species, levels = c("setosa", "versicolor", "virginica")))

summary_1 <- test |>
  group_by(Species) |>
  select(Species, everything()) |>
  adorn_totals()

summary_2 <- summary_1 |>
  filter(Species != "Total")

summary_3 <- summary_2 |>
  group_by(Species) |>
  select(Species, everything()) |>
  adorn_totals()

summary_4 <- summary_3 |>
  bind_rows(summary_1) |>
  adorn_totals()
#> Error in adorn_totals(bind_rows(summary_3, summary_1)): trying to re-add a totals dimension that is already been added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next-release include in next release (bugfix, minor, or major)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants