-
Notifications
You must be signed in to change notification settings - Fork 3
/
_common.R
137 lines (84 loc) · 3.37 KB
/
_common.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
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
# Packages ----------------------------------------------------------------
library(tidyverse)
library(janitor)
library(fs)
library(rmarkdown)
library(xfun)
library(officer)
# YAML etc ----------------------------------------------------------------
knitr::opts_chunk$set(
comment = "#>",
dpi = 150,
out.width = "100%",
dev = "ragg_png"
)
options(
scipen = 999,
pillar.sigfig = 5,
width = 75
)
# Functions ---------------------------------------------------------------
# * File Names ------------------------------------------------------------
create_nostarch_file_name <- function(file_type = "pdf", chap_number = chapter_number, output_format = "html") {
chapter_number_two_digits <- stringr::str_pad(chap_number, 2, "left", "0")
figure_number_three_digits <- stringr::str_pad(i, 3, "left", "0")
file_name <- stringr::str_glue("F{chapter_number_two_digits}{ figure_number_three_digits }.{file_type}")
file_name
}
print_nostarch_file_name <- function(file_type_to_print = "pdf", output_format = "html") {
file_name_with_brackets <- stringr::str_glue("[{create_nostarch_file_name(file_type = file_type_to_print)}]")
if (output_format == "word") {
cat(file_name_with_brackets)
}
}
# * Tables ----------------------------------------------------------------
save_table_for_nostarch <- function(table_object) {
file_name <- create_nostarch_file_name(file_type = "png")
file_name_with_path <- str_glue(here::here("nostarch/temp/{file_name}"))
gtsave(data = table_object,
filename = file_name_with_path)
save_image_for_nostarch(file_name_with_path)
knitr::include_graphics(file_name_with_path,
dpi = 300)
}
print_table_in_html <- function(table_object, output_format = "html") {
if (output_format == "html") {
table_object
}
}
# * Figures and Images ----------------------------------------------
save_figure_for_nostarch <- function(figure_height = 4) {
save_directory <- here::here("nostarch/figures/")
file_name <- create_nostarch_file_name()
ggsave(plot = last_plot(),
path = save_directory,
filename = file_name,
device = cairo_pdf,
width = 4.675,
height = figure_height)
i <<- i + 1
}
save_image_for_nostarch <- function(image_file, chap_number = chapter_number) {
image_info <- magick::image_read(image_file) %>%
magick::image_info()
image_width_pixels <- image_info %>%
pull(width)
# image_dpi <- image_info %>%
# separate(density, sep = "x", into = c("dpi", NA)) %>%
# mutate(dpi = as.numeric(dpi)) %>%
# pull(dpi)
image_width_inches <- image_width_pixels / 96
image_width_scale_down_factor <- image_width_inches / 4.675
image_new_width <- image_width_pixels / image_width_scale_down_factor
resized_image <- magick::image_read(image_file)
# magick::image_resize(image_new_width) %>%
# magick::image_page(density = "96x96")
chapter_number_two_digits <- stringr::str_pad(chap_number, 2, "left", "0")
figure_number_three_digits <- stringr::str_pad(i, 3, "left", "0")
file_name <- create_nostarch_file_name(file_type = "png")
save_directory <- here::here("nostarch/figures/")
image_full_path <- stringr::str_glue("{save_directory}/{file_name}")
magick::image_write(resized_image,
path = image_full_path)
i <<- i + 1
}