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

Feature request: adorn_n_formatting() #444

Closed
Thyrisis opened this issue Apr 16, 2021 · 10 comments
Closed

Feature request: adorn_n_formatting() #444

Thyrisis opened this issue Apr 16, 2021 · 10 comments
Assignees
Milestone

Comments

@Thyrisis
Copy link

I need to present numbers as well as percentages in many tables I create with tabyl() and since those numbers can be large, they need to be formattable - with either commas or dots for legibility.

I know this is possible by pre-formatting and use of the ns argument in adorn_ns() but I suggest making this simpler and more consistent with the existing adorn_pct_formatting() by adding a new function - adorn_n_formatting(). This would also avoid the need to create multiple output objects in multiple steps.

I suggest the arguments for this could include big.mark to be consistent with other packages existing syntax.

Proposed code example

set.seed(1)
raw_data <- data.frame(sex = rep(c("m", "f"), 3000),
											 age = round(runif(3000, 1, 102), 0))
raw_data$agegroup = cut(raw_data$age, quantile(raw_data$age, c(0, 1/3, 2/3, 1)))

comparison <- raw_data %>%
	tabyl(agegroup, sex, show_missing_levels = F) %>%
	adorn_totals(c("row", "col")) %>%
	adorn_percentages("col") %>%
	adorn_pct_formatting(digits = 1) %>% 
	adorn_ns(position = "rear") %>% 
	adorn_n_formatting(big.mark = ",")

Comparable current code example

set.seed(1)
raw_data <- data.frame(sex = rep(c("m", "f"), 3000),
											 age = round(runif(3000, 1, 102), 0))
raw_data$agegroup = cut(raw_data$age, quantile(raw_data$age, c(0, 1/3, 2/3, 1)))

comparison <- raw_data %>%
	tabyl(agegroup, sex, show_missing_levels = F) %>%
	adorn_totals(c("row", "col")) %>%
	adorn_percentages("col") %>%
	adorn_pct_formatting(digits = 1)

formatted_ns <- attr(comparison, "core") %>% 
	adorn_totals(c("row", "col")) %>% 
	mutate_if(is.numeric, format, big.mark = ",")

comparison %>% 
	adorn_ns(position = "rear", ns = formatted_ns)
@sfirke
Copy link
Owner

sfirke commented Apr 16, 2021

Thanks for this suggestion. I was on the fence about #378 and it sounds like this is a vote for implementing that.

I think the formatting would have to be part of adorn_ns() rather than a subsequent function, as once the Ns are merged into the values it would be much more difficult to manipulate them.

What do you think about a new argument to adorn_ns that would be a function that is applied to all numeric columns of the Ns? The most basic would be format_ns <- function(x) { format(x, big.mark = ",") }. You'd declare that function on a single line, once, and then could have calls like adorn_n_formatting(format_function = format_ns) EDIT: adorn_ns(format_function = format_ns).

@sfirke sfirke added the seeking comments Users and any interested parties should please weigh in - this is in a discussion phase! label Apr 16, 2021
@Thyrisis
Copy link
Author

Apologies, I did try to avoid a duplicate request but didn't find #378 - yes that's very much the same requirement.

I understand your point about amending the formats after the adorn so yes, another approach would be to support the format() function as a ... pass through of adorn_ns(). This is how pandoc.table in the pander package does this and it works well.

I think I'm arguing for a way to do this all inline and without repetition rather than requiring another step - also for consistency with standard elements of the tidyverse as a consistent design philosophy. My problem with defining a function would be that it's another step - passing through the format() elements seems more elegant and consistent.

@Chanelle280
Copy link

Has this been implemented yet?

@sfirke
Copy link
Owner

sfirke commented Dec 9, 2021

Hi. Sorry my engagement is sporadic. I edited my comment above to show what it would look like in-line. I think we agree it should be part of adorn_ns, I don't know why I described it as a new function. I think this should not be too hard and I'll put it on the milestone list for v2.2, since there's clearly demand for it.

@sfirke
Copy link
Owner

sfirke commented Dec 9, 2021

Consolidating into one issue, the use case stated in #459 was:

I'm trying to add a comma to large number but I'm very confused as to how to do this.

adorn_totals(where = "row") %>%
adorn_percentages("col") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns(position = "front", big.format = ",")

@davidbudzynski
Copy link

Just here to say adding the big.format argument would make my life much easier! Thanks for your work on this package.

@sfirke sfirke self-assigned this Jan 12, 2023
@sfirke
Copy link
Owner

sfirke commented Jan 12, 2023

Alright I'm gonna try to sneak this in to 2.2 I think ...

sfirke added a commit that referenced this issue Jan 24, 2023
sfirke added a commit that referenced this issue Jan 24, 2023
* implement format_func from #444

* specify namespace for dplyr::desc
@sfirke
Copy link
Owner

sfirke commented Jan 24, 2023

@Thyrisis @Chanelle280 @davidbudzynski @rasmusrhl this is now merged into the main branch! If you're interested in taking it for a spin, there's a short window in which I could act on any feedback before submitting the next version to CRAN.

Otherwise of course I'd still like your feedback on how I implemented it, at any time.

@sfirke sfirke closed this as completed Jan 24, 2023
@davidbudzynski
Copy link

Looks good to me, thanks for your work on this!

@Chanelle280
Copy link

Looks great! Thanks for working on this. We use tabyl all the time and this will make our work a lot easier when we also include percentages.

@sfirke sfirke removed the seeking comments Users and any interested parties should please weigh in - this is in a discussion phase! label Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants