3

How could I pass what columns should adorn_totals consider without passing another arguments.

library(dplyr)
library(janitor)

mtcars %>% 
  count(vs,am) %>% 
  adorn_totals()
#>     vs am  n
#>      0  0 12
#>      0  1  6
#>      1  0  7
#>      1  1  7
#>  Total  2 32

I don't want the total in column "AM" to be calculated, only in column "n". I know I can do this by passing the column names at the end of the function, but to do this you have to pass through all the other arguments first adorn_totals(,,,, n), which is ugly.

Is there any way to do this directly, as in:

mtcars %>% 
  count(vs,am) %>%
  adorn_totals(... = n)
#> Error in `adorn_totals()`:
#> ! Names can't be of the form `...` or `..j`.
#> ✖ These names are invalid:
#>   * "..." at location 1.
#> Backtrace:
#>      ▆
#>   1. ├─mtcars %>% count(vs, am) %>% adorn_totals(... = n)
#>   2. └─janitor::adorn_totals(., ... = n)
#>   3.   └─tidyselect::eval_select(expr, data = dat)
#>   4.     └─tidyselect:::eval_select_impl(...)
#>   5.       ├─tidyselect:::with_subscript_errors(...)
#>   6.       │ └─rlang::try_fetch(...)
#>   7.       │   └─base::withCallingHandlers(...)
#>   8.       └─tidyselect:::vars_select_eval(...)
#>   9.         └─tidyselect:::ensure_named(...)
#>  10.           └─vctrs::vec_as_names(names(pos), repair = "check_unique", call = call)
#>  11.             └─vctrs (local) `<fn>`()
#>  12.               └─vctrs:::validate_unique(names = names, arg = arg, call = call)
#>  13.                 └─vctrs:::stop_names_cannot_be_dot_dot(names, call = call)
#>  14.                   └─vctrs:::stop_names(...)
#>  15.                     └─vctrs:::stop_vctrs(...)
#>  16.                       └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))
M Aurélio
  • 830
  • 5
  • 13
  • I don’t see an argument for that function that lets you exclude particular columns. You are also going to run into complication when there is a character value in a numeric column. – IRTFM May 16 '23 at 14:23
  • It is in the documentation, make sure you check the latest one. As for the need, I gave an example where the problem could be reproduced, if we imagine something like a column of averages that makes no sense to calculate the total but we still want to format as numeric. – M Aurélio May 23 '23 at 13:48

2 Answers2

5

As a hack you could give that argument a name that isn't otherwise in the named arguments for adorn_totals. That is:

mtcars %>% 
  count(vs,am) %>% 
  adorn_totals(cols=n)

    vs am  n
     0  0 12
     0  1  6
     1  0  7
     1  1  7
 Total  - 32
George Savva
  • 4,152
  • 1
  • 7
  • 21
  • Thank you. This way also works for the other functions in the `adorn_xxx` family! But why set the `... = n` directly doesn't work? – M Aurélio May 23 '23 at 14:17
  • I don't know to be honest. Maybe something about the way tidyverse functions deal with arguments. Looking at the `traceback()` for the error gives some clues. – George Savva May 24 '23 at 11:00
1

A quick hack would be to transfer non-target columns to character before piping to adorn_totals.

library(dplyr)
library(janitor)

mtcars %>% 
  count(vs,am) %>% 
  # across whatever columns you don't want to include in adorn_totals
  mutate(across(-n, as.character)) %>% 
  adorn_totals()

    vs am  n
     0  0 12
     0  1  6
     1  0  7
     1  1  7
 Total  - 32
benson23
  • 16,369
  • 9
  • 19
  • 38