-
Notifications
You must be signed in to change notification settings - Fork 5
/
PowerPoint in R via Officer.RMd
169 lines (103 loc) · 4.13 KB
/
PowerPoint in R via Officer.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
---
title: "PowerPoint in R via Officer"
output: html_notebook
---
```{r}
library(tidyverse)
library(officer)
library(quantmod)
library(yfinance)
library(ggdark)
library(rvg)
get_placeholder_dims <- function(presentation, label, dim = "width", unit = "in") {
layout <- officer::layout_properties(presentation)
ifelse(
dim=="width",
dimension <- layout$cx[match(label, layout$ph_label)],
dimension <- layout$cy[match(label, layout$ph_label)]
)
if (unit == "in") {
dimension
} else if (unit == "px") {
dimension*96
} else if (unit == "cm") {
dimension * 2.54
} else stop(glue::glue("Only 'in', 'px' and 'cm' are supported as units. You entered '{unit}'."))
}
```
```{r}
presentation <- read_pptx("stock_summary_template.pptx")
ticker <- "BRK-B"
company_name <- yfinance::get_company_names(ticker)
summary <- yfinance::get_summaries(ticker)
ttm_performance_path <- glue::glue("presentations/images/{ticker} TTM Chart.png")
prices <- getSymbols(ticker, from=Sys.Date()-365, to = Sys.Date(), auto.assign = FALSE)
png(filename = ttm_performance_path,
width = get_placeholder_dims(presentation, "TTM Perfromance", dim ="width", unit = "px"),
height = get_placeholder_dims(presentation, "TTM Perfromance", dim = "hight", unit = "px")
)
chartSeries(prices, name = glue::glue("{ticker} - 12 Months"), type="line")
dev.off()
```
```{r}
ebit_margin <-
get_income(ticker) %>%
mutate(
ebit_margin = ebit / totalRevenue
) %>%
ggplot()+
geom_bar(mapping = aes(x = date, y = ebit_margin), stat = "identity", fill = "#5efc82", width = 0.4)+
geom_text(
mapping = aes(x = date, y = ebit_margin, label = paste0(round(ebit_margin*100, 2), "%")),
vjust = -0.2
)+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+
labs(title = "EBIT Margin", x = "Date", y = "EBIT Margin")+
dark_theme_gray()+
theme(
text = element_text(size = 11),
plot.background =element_blank(),
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey30", size = 0.2),
panel.grid.minor = element_blank()
)
```
```{r}
sales_growth <-
get_income(ticker) %>%
arrange(date) %>%
mutate(
sales_growth = totalRevenue / lag(totalRevenue) - 1
) %>%
dplyr::filter(!is.na(sales_growth)) %>%
ggplot()+
geom_bar(mapping = aes(x = date, y = sales_growth), stat = "identity", fill = "#6200ea", width = 0.4)+
geom_text(
mapping = aes(x = date, y = sales_growth, label = paste0(round(sales_growth*100, 2), "%")),
vjust = -0.2
)+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+
labs(title = "Sales Growth", x = "Date", y = "Sales Growth")+
dark_theme_gray()+
theme(
text = element_text(size = 11),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey30", size = 0.2),
panel.grid.minor = element_blank()
)
```
```{r}
my_pres <- presentation %>%
remove_slide(index = 1) %>%
add_slide(layout = "Company Summary", master = "Office Theme") %>%
ph_with(value = company_name, location = ph_location_label(ph_label = "Company Name") ) %>%
ph_with(value = ticker, location = ph_location_label(ph_label = "Ticker") ) %>%
ph_with(value = glue::glue("Sector: {summary$sector}") , location = ph_location_label(ph_label = "Sector") ) %>%
ph_with(value = glue::glue("Industry: {summary$industry}"), location = ph_location_label(ph_label = "Industry") ) %>%
ph_with(value = summary$longBusinessSummary, location = ph_location_label(ph_label = "Company Summary") ) %>%
ph_with(value = external_img(ttm_performance_path), location = ph_location_label(ph_label = "TTM Perfromance") ) %>%
ph_with(value = rvg::dml(ggobj = sales_growth, bg = "transparent"), location = ph_location_label(ph_label = "Sales Growth") ) %>%
ph_with(value = rvg::dml(ggobj = ebit_margin, bg = "transparent"), location = ph_location_label(ph_label = "EBIT Margin") )
print(my_pres, glue::glue("presentations/{company_name} Summary {Sys.Date()}.pptx"))
```