-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflexable to word.R
More file actions
109 lines (102 loc) · 2 KB
/
flexable to word.R
File metadata and controls
109 lines (102 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
library(tidyverse)
library(gt)
library(flextable)
hawaiian_sales <- gt::pizzaplace |>
filter(name == 'hawaiian') |>
mutate(
month = month(
date, label = TRUE, abbr = FALSE,
locale = 'en_US.UTF-8' # English month names
),
quarter = paste0('Q', quarter(date))
) |>
summarise(
sales = n(),
revenue = sum(price),
.by = c(month, quarter)
)
hawaiian_sales
flextable(hawaiian_sales)
hawaiian_sales |>
as_grouped_data(groups='quarter') |>
flextable() |>
set_header_labels(
month = 'Month',
quarter = 'Quarter',
sales = 'Sales',
revenue = 'Revenue'
) |>
add_header_lines(
'Hawaiian Pizza Sales in 2015'
) |>
add_header_lines(
'Based on the fake `pizzaplace` data from `{gt}`'
) |>
colformat_double(
j = 'revenue',
digits=2,
prefix= '$'
)
order_of_months_w_total <- c(
levels(hawaiian_sales$month),
'Total'
)
totals <- hawaiian_sales |>
summarise(
month = factor(
'Total',
levels = order_of_months_w_total
),
sales = n(),
revenue = sum(revenue),
.by = quarter
)
combined_data <- hawaiian_sales |>
mutate(
month = factor(
month,
levels = order_of_months_w_total,
ordered = FALSE
)
) |>
bind_rows(totals) |>
arrange(quarter, month)
combined_data
table_with_totals <- combined_data |>
as_grouped_data(groups = 'quarter') |>
flextable() |>
set_header_labels (
month = 'Month',
quarter = 'Quarter',
sales = 'Sales',
revenue = 'Revenue'
) |>
add_header_lines(
'Hawaiian Pizza Sales in 2015'
) |>
add_header_lines(
'Based on the fake `pizzaplace` data from `{gt}`'
) |>
colformat_double(
j = 'revenue',
digits = 2,
prefix = '$'
)
table_with_totals
styled_table <- table_with_totals |>
bg(
i =~!is.na(quarter),
bg = '#E7EDF3'
) |>
bold(
i = ~(month == 'Total')
)
styled_table
styled_table |>
save_as_docx(
path = 'word_export.docx'
)
styled_table |>
save_as_pptx(
path = 'word_export.pptx'
)