-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
426 lines (303 loc) · 9.71 KB
/
index.Rmd
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
---
title: "No API, No Problem"
subtitle: "<span style=font-size:50%>Automating content delivery with R and Excel</span> <hr>"
author: "<br>Joshua Goldberg | Hasib Neaz<br><i class='fab fa-twitter' style='color:#6CADDE'></i><i class='fab fa-github'></i> @GoldbergData"
date: "slides at [github.com/GoldbergData/no-api-no-problem](https://github.com/GoldbergData/no-api-no-problem)"
output:
xaringan::moon_reader:
css: [default, style.css, robot-fonts]
lib_dir: libs
nature:
titleSlideClass: ["left", "middle", "inverse"]
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(fig.retina = 3, warning = FALSE, message = FALSE)
# Set Java location for rjava.
# This is required for mailR package. You will need to go to here to download latest Java
# https://java.com/en/download/
library(tidyverse)
library(glue)
library(furrr)
library(DT)
```
```{r eval=FALSE, echo=FALSE}
Sys.setenv(JAVA_HOME = "C:\\Program Files\\Java\\jdk1.8.0_102")
library(XLConnect)
library(sendmailR)
xaringan::decktape(file = "index.html", output = "index.pdf")
```
# We are in the future...
<iframe width="683" height="414" src="https://www.youtube.com/embed/tlThdr3O5Qo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
---
class: center, middle
# So we can move on, right?
<img src="img/excel.gif" width="100%" height="100%" align="center"/>
---
class: center, middle
<img src="img/kanye.gif" width="80%" height="80%" align="center"/>
---
# Your boss wants 49 Excel sheets
<img src="img/office.gif" width="125%" height="125%" align="center"/>
---
# Data dump
```{r Data, echo=FALSE}
# https://community.tableau.com/docs/DOC-1236
people_data <- readxl::read_excel("Sample - Superstore.xls", sheet = 3)
sales_data <- readxl::read_excel("Sample - Superstore.xls", sheet = 1) %>%
select(-`Row ID`) %>%
left_join(people_data, "Region") %>%
select(Person, everything())
sales_split <- sales_data %>%
split(.$State)
```
```{r Show-data}
sales_data
```
---
class: middle center
<img src="img/xlconnect.png" width="27%" height="27%" align="center"/>
<img src="img/tidyverse.png" width="30%" height="30%" align="center"/>
.footnote[XLConnect created by Mirai Solutions GmbH]
---
# Excel interface
What can XLConnect do?
--
- Create/load Excel workbooks
--
- Add/read data from Excel
--
- Format worksheets (some limitations)
--
- Include images
--
- And much more... see [here](https://cran.r-project.org/web/packages/XLConnect/XLConnect.pdf)
--
All programatically!
---
class: middle center, inverse
# Make a template
<img src="img/blank_template.png" width="100%" height="100%" align="center"/>
---
class: middle center, inverse
# Create cell styles
<img src="img/cell_styles.png" width="75%" height="75%" align="center"/>
---
# Load/create workbook and styles
```{r eval=FALSE}
wb <- loadWorkbook("template.xlsx")
```
```{r eval=FALSE}
date_cellstyle <- getCellStyle(wb, "date")
num_comma_cellstyle <- getCellStyle(wb, "number_comma")
dec_cellstyle <- getCellStyle(wb, "decimal")
setStyleAction(wb, XLC$STYLE_ACTION.NONE)
```
---
# Write data/format data
```{r eval=FALSE}
writeWorksheet(wb, data = data,
sheet_name,
startRow = 2,
header = FALSE)
walk(
2:(row_count + 1),
~ setCellStyle(
wb,
sheet = sheet_name,
row = .x,
col = 2:3,
cellstyle = date_cellstyle
)
)
walk(
2:(row_count + 1),
~ setCellStyle(
wb,
sheet = sheet_name,
row = .x,
col = c(sales, discount, profit),
cellstyle = num_comma_cellstyle
)
)
```
---
# Add a plot to Excel
```{r eval=FALSE}
createName(wb, "plot", formula = glue("Summary!{idx2cref(c(1, 6))}") %>% as.character())
p <- data %>%
group_by(Segment, Category) %>%
summarise_if(is.numeric, ~ sum(.x, na.rm = TRUE)) %>%
gather(key = key, value = value, -c("Segment", "Category", "Postal Code")) %>%
filter(!key %in% c("Quantity", "Discount")) %>%
ggplot(aes(Category, value, fill = key)) +
geom_col(position = "dodge") +
scale_fill_brewer(name = "Metric", type = "qual", palette = 2) +
scale_y_continuous(label = scales::comma) +
facet_wrap( ~ Segment) +
ggthemes::theme_tufte() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Profit/Sales by Segment/Category",
y = NULL)
ggsave(p, filename = "segment-category.png", width = 4, height = 3)
addImage(wb, filename = "segment-category.png", name = "plot", originalSize = TRUE)
saveWorkbook(wb, file = file_name)
```
---
```{r echo=FALSE, eval=FALSE}
style <- function(wb, cell_style, column, row_count, sheet_name) {
walk(2:(row_count + 1), ~ setCellStyle(wb, sheet = sheet_name, row = .x, col = column, cellstyle =
cell_style))
}
make_plot <- function(data, file_name) {
p <- data %>%
group_by(Segment, Category) %>%
summarise_if(is.numeric, ~ sum(.x, na.rm = TRUE)) %>%
gather(key = key, value = value, -c("Segment", "Category", "Postal Code")) %>%
filter(!key %in% c("Quantity", "Discount")) %>%
ggplot(aes(Category, value, fill = key)) +
geom_col(position = "dodge") +
scale_fill_brewer(name = "Metric", type = "qual", palette = 2) +
scale_y_continuous(label = scales::dollar) +
facet_wrap( ~ Segment) +
ggthemes::theme_tufte() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Profit/Sales by Segment/Category",
subtitle = data$State[[1]],
y = NULL)
ggsave(p, filename = file_name, width = 4, height = 3)
}
build_excel <- function(data, wb, directory = "reports", plot_file_name = "segment-category.png") {
if (!dir.exists(directory)) {
dir.create(directory)
}
file_name <- glue("{data$State[[1]]}.xlsx") %>% as.character()
sheet_name <- "Sales"
column_count <- ncol(data)
row_count <- nrow(data)
data <- data %>%
select(-Person)
wb <- loadWorkbook("template.xlsx")
date_cellstyle <- getCellStyle(wb, "date")
num_comma_cellstyle <- getCellStyle(wb, "number_comma")
dec_cellstyle <- getCellStyle(wb, "decimal")
sales <- data %>% names %>% str_detect("Sales") %>% which()
discount <- data %>% names %>% str_detect("Discount") %>% which()
profit <- data %>% names %>% str_detect("Profit") %>% which()
quantity <- data %>% names %>% str_detect("Quantity") %>% which()
setStyleAction(wb, XLC$STYLE_ACTION.NONE)
writeWorksheet(wb, data = data, sheet_name, startRow = 2, header = FALSE)
style(
wb = wb,
cell_style = date_cellstyle,
column = 2:3,
row_count = row_count,
sheet_name = sheet_name
)
style(
wb = wb,
cell_style = dec_cellstyle,
column = c(sales, discount, profit),
row_count = row_count,
sheet_name = sheet_name
)
style(
wb = wb,
cell_style = num_comma_cellstyle,
column = quantity,
row_count = row_count,
sheet_name =
sheet_name
)
make_plot(data, plot_file_name)
createName(wb, "plot", formula = glue("Summary!{idx2cref(c(1, 6))}") %>% as.character())
addImage(wb, filename = plot_file_name, name = "plot", originalSize = TRUE)
saveWorkbook(wb, file = glue("{directory}/{file_name}"))
invisible(file.remove(plot_file_name))
}
```
# Functional
```{r eval=FALSE}
make_plot()
style()
build_excel()
```
--
```{r eval=FALSE}
walk(sales_split, function(split) {
build_excel(split, "reports")
})
```
--
```{r eval=FALSE}
plan(multiprocess)
furrr::future_map(sales_split, ~ build_excel(.x, "reports"))
```
---
class: middle center
<img src="img/reports.png" width="100%" height="100%" align="center"/>
---
class: middle center
<img src="img/example_report.png" width="100%" height="100%" align="center"/>
---
class: middle center
<img src="img/example_plot.png" width="100%" height="100%" align="center"/>
---
# Compose a message
```{r eval=FALSE}
subject = "Sales State Coverage Report"
message <- paste0(
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Sales Coverage</title>
<style type="text/css">
</style>
</head>
<body>
Hello,
</p>
<p>Attached is sales data for your state coverage.</p>
<p>A visualization and summary are available on the second tab.</p>
<p>Let us know if you have questions.</p>
<p>Kind regards,</p>
<p>Analysis Group,</p>
</body>
</html>'
)
```
---
# mailR/sendmailR
```{r eval=FALSE}
send.mail(
from = "user@domain.com",
to = "user@domain.com",
subject = subject,
body = message,
html = TRUE,
smtp = list(
host.name = "smtp.gmail.com",
port = 465,
user.name = rstudioapi::askForPassword(),
passwd = rstudioapi::askForPassword(),
ssl = TRUE
),
attach.files = "file_path",
authenticate = TRUE,
send = TRUE
)
```
---
# Nice to know
- Java/rJava are required to use XLConnect and mailR/sendmailR.
- Latest version of java: https://java.com/en/download/.
- If you have trouble installing Java on Mac, see https://www.chrisjmendez.com/2018/11/16/installing-xlconnect-and-rjava-on-macos/.
- It's typically required you set your JAVA_HOME, such as `Sys.setenv(JAVA_HOME = "C:\\Program Files\\Java\\jdk1.8.0_102")`. The path is relative to your machine, so you must locate your directory and version of Java.
- Useful: `vignette("XLConnect")`.
- XLConnect runs on memory until `saveWorkbook()` is called. If you experience out-of-memory errors, try allocating more memory with `options(java.parameters = "-Xmx1024m")`. See vignette above.