Skip to content

Commit 328ca5c

Browse files
committed
change name of pivot_wider_name_first
1 parent 7d34529 commit 328ca5c

File tree

6 files changed

+32
-45
lines changed

6 files changed

+32
-45
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export(is_numeric_like)
3131
export(italic_md)
3232
export(num_pad)
3333
export(p2stars)
34-
export(pivot_wider_name_value)
34+
export(pivot_wider_name_first)
3535
export(pretty_widths)
3636
export(separate_star_column)
3737
export(star_balance)

R/tables.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ pretty_widths <- function(
16281628
#' @inheritParams tidyr::pivot_wider
16291629
#' @return data.frame
16301630
#' @export
1631-
pivot_wider_name_value <- function(
1631+
pivot_wider_name_first <- function(
16321632
data,
16331633
...,
16341634
id_cols = NULL,
Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/apa7.qmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ d_star
441441
apa_flextable(d_star)
442442
```
443443

444-
## pivot_wider_name_value
444+
## pivot_wider_name_first
445445

446446

447447

@@ -460,7 +460,7 @@ d |>
460460
# deselect t, df, and p
461461
select(-c(t,df, p)) |>
462462
# restructure data
463-
pivot_wider_name_value(names_from = Model,
463+
pivot_wider_name_first(names_from = Model,
464464
values_from = c(b, beta)) |>
465465
# add break column between 2 models
466466
add_break_columns(`Model 1_beta`) |>
@@ -577,10 +577,10 @@ ggplot2::diamonds |>
577577
aes(label = paste0(p, " (", n, ")")),
578578
position = position_fill(vjust = .5),
579579
size.unit = "pt",
580-
size = 15 * .8,
580+
size = 14 * .8,
581581
color = "gray10"
582582
) +
583-
theme_minimal(base_family = "Roboto Condensed", base_size = 15) +
583+
theme_minimal(base_family = "Roboto Condensed", base_size = 14) +
584584
scale_y_continuous(
585585
"Cumulative Proportion",
586586
expand = expansion(c(0, .025)),

vignettes/articles/_APAManualTables.qmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ d <- d |>
394394
.by = c(Variable, name)) |>
395395
mutate(`Full sample_n` = sum(n),
396396
.by = c(Variable, Group)) |>
397-
pivot_wider_name_value(values_from = c(n , `%`)) |>
397+
pivot_wider_name_first(values_from = c(n , `%`)) |>
398398
apa7::add_break_columns(ends_with("%")) |>
399399
relocate(`Full sample_n`, .after = last_col()) |>
400400
mutate(
@@ -670,7 +670,7 @@ d <- d |>
670670
100 * `n` / sum(`n`),
671671
accuracy = .01),
672672
.by = Source) |>
673-
pivot_wider_name_value(values_from = c(`n`, `%`)) |>
673+
pivot_wider_name_first(values_from = c(`n`, `%`)) |>
674674
relocate(chi, .after = dplyr::last_col()) |>
675675
rename(`&chi;^2^(2)` = chi) |>
676676
mutate(across(-Source, align_chr)) |>
@@ -774,7 +774,7 @@ d <- d |>
774774
mutate(Age = paste0(Age, "-year-olds"),
775775
Cohens_d = signs(abs(Cohens_d),
776776
accuracy = .001)) |>
777-
pivot_wider_name_value(
777+
pivot_wider_name_first(
778778
values_from = c(M, SD),
779779
names_from = Age) |>
780780
select(`Logistic Parameter`,
@@ -1122,7 +1122,7 @@ d <- d |>
11221122
summarise(M = mean(x),
11231123
SD = sd(x),
11241124
.by = c(Group, Time)) |>
1125-
pivot_wider_name_value(
1125+
pivot_wider_name_first(
11261126
names_from = Group,
11271127
values_from = c(M, SD)) |>
11281128
mutate(Time = paste("Time", Time))
@@ -1472,7 +1472,7 @@ d_performance <- apa_performance_comparison(
14721472
pivot_wider(names_from = Model)
14731473
14741474
d_parameters |>
1475-
pivot_wider_name_value(
1475+
pivot_wider_name_first(
14761476
id_cols = Variable,
14771477
names_from = Model,
14781478
values_from = 3:5) |>

vignettes/articles/chisquare.qmd

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,28 @@ The flextable package's `tabulator` function has considerable power in creating
102102
Here I calculate the means, standard deviations, and sample sizes within each cell of a contingency table.
103103

104104
```{r}
105-
warpbreaks |>
105+
d_tension <- warpbreaks |>
106106
summarise(
107-
Mean = mean(breaks, na.rm = TRUE),
108-
stdev = sd(breaks, na.rm = TRUE),
109-
sample_size = sum(!is.na(breaks)),
107+
m = mean(breaks, na.rm = TRUE),
108+
sd = sd(breaks, na.rm = TRUE),
109+
n = n(),
110110
.by = c(wool, tension)
111111
) %>%
112112
rename(Wool = wool) |>
113113
mutate(
114114
tension = factor(
115115
tension,
116116
labels = paste(c("Low", "Medium", "High"), "Tension")
117-
)) |>
117+
))
118+
119+
d_tension |>
118120
flextable::tabulator(
119-
rows = "Wool",
121+
rows = c("Wool"),
120122
columns = "tension",
121-
M = as_paragraph(Mean),
122-
SD = as_paragraph(stdev),
123-
n = as_paragraph(sample_size)
124-
) |>
123+
M = as_paragraph(m),
124+
SD = as_paragraph(sd),
125+
N = as_paragraph(n)
126+
) |>
125127
flextable::as_flextable() |>
126128
italic(i = 2, part = "header") |>
127129
align(j = 1, align = "center") |>
@@ -130,33 +132,18 @@ warpbreaks |>
130132

131133
# Processing Data Yourself
132134

133-
What if you want it to look a little different? Unfortunately, you might have to do some of the heavy lifting yourself. The `tidyr::pivot_wider` function can save a lot of time, if you know how to use the `names_glue` argument to get the column names in the right order and `names_vary` argument to get the columns in the right order.
135+
What if you want it to look a little different? Unfortunately, you might have to do some of the heavy lifting yourself. The `pivot_wider_name_first` function is a wrapper around `tidyr::pivot_wider` that prints and sorts column names as "name_variable" instead of "variable_name". I wanted a shortcut because I frequently needed to make a shortcut for setting `names_glue = "{tension}_{.value}"` and `names_vary = "slowest"` when `names_from = "tension"`.
134136

135137
A subtle difference in this version of the table is that the columns are decimal aligned, which is evident in the standard deviation column.
136138

137-
Also, whereas previously the Wool column header was middle aligned, it is now top aligned, which is consistent with comparable examples in the *APA Style Manual*. I am not sure if this is a real rule in APA style, and I actually prefer middle alignment for situations like this.
139+
Also, whereas previously the "stub header" (Wool column) was middle aligned, it is now top aligned, which is consistent with comparable examples in the *APA Style Manual*. I am not sure if this is a real rule in APA style, and I actually prefer middle alignment for situations like this.
138140

139141
```{r warpbreaks}
140-
warpbreaks %>%
141-
summarise(
142-
M = mean(breaks, na.rm = TRUE),
143-
SD = sd(breaks, na.rm = TRUE),
144-
n = sum(!is.na(breaks)),
145-
.by = c(wool, tension)
146-
) %>%
147-
rename(Wool = wool) %>%
148-
mutate(
149-
tension = factor(
150-
tension,
151-
levels = c("L", "M", "H"),
152-
labels = paste(c("Low", "Medium", "High"), "Tension")
153-
)
154-
) |>
155-
pivot_wider(
142+
d_tension |>
143+
rename(M = m, SD = sd) |>
144+
pivot_wider_name_first(
156145
values_from = c(M, SD, n),
157-
names_from = tension,
158-
names_glue = "{tension}_{.value}",
159-
names_vary = "slowest"
146+
names_from = tension
160147
) |>
161148
add_break_columns(ends_with("_n"), omit_last = TRUE) |>
162149
apa_flextable(column_formats = column_formats(accuracy = .1)) |>

0 commit comments

Comments
 (0)