forked from business-science/free_r_tips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
003_automate_powerpoint.R
69 lines (51 loc) · 1.94 KB
/
003_automate_powerpoint.R
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
# R TIPS ----
# TIP 003: Automate PowerPoint Slide Decks with R ----
#
# 👉 For Weekly R-Tips, Sign Up Here: https://learn.business-science.io/r-tips-newsletter
# 1.0 LIBRARIES ----
library(officer)
library(flextable)
library(tidyverse)
library(tidyquant)
library(timetk)
# 2.0 DATA ----
# - Use tidyquant to pull in some stock data
stock_data_tbl <- c("AAPL", "GOOG", "META", "NVDA") %>%
tq_get(from = "2019-01-01", to = "2020-08-16")
# 3.0 DATA WRANGLING ----
# - dplyr (DS4B 101-R Weeks 2 & 3)
stock_returns_tbl <- stock_data_tbl %>%
select(symbol, date, adjusted) %>%
group_by(symbol) %>%
summarise(
week = last(adjusted) / first(tail(adjusted, 7)) - 1,
month = last(adjusted) / first(tail(adjusted, 30)) - 1,
quarter = last(adjusted) / first(tail(adjusted, 90)) - 1,
year = last(adjusted) / first(tail(adjusted, 365)) - 1,
all = last(adjusted) / first(adjusted) - 1
)
# 4.0 PLOTS & TABLES ----
# - ggplot2 (DS4B 101-R Weeks 4)
# * Stock Plot ----
stock_plot <- stock_data_tbl %>%
group_by(symbol) %>%
summarize_by_time(adjusted = AVERAGE(adjusted), .by = "week") %>%
plot_time_series(date, adjusted, .facet_ncol = 2, .interactive = FALSE)
stock_plot
# * Stock Return Table -----
stock_table <- stock_returns_tbl %>%
rename_all(.funs = str_to_sentence) %>%
mutate_if(is.numeric, .funs = scales::percent) %>%
flextable::flextable()
stock_table
# 5.0 MAKE A POWERPOINT DECK -----
doc <- read_pptx()
doc <- add_slide(doc)
doc <- ph_with(doc, value = "Stock Report", location = ph_location_type(type = "title"))
doc <- ph_with(doc, value = stock_table, location = ph_location_left())
doc <- ph_with(doc, value = stock_plot, location = ph_location_right())
print(doc, target = "003_powerpoint_slidedeck/stock_report.pptx")
# LEARNING MORE ----
# FREE MASTERCLASS
# - 10 SECRETS TO BECOMING A DATA SCIENTIST
# https://learn.business-science.io/free-rtrack-masterclass