forked from InseeFrLab/utilitR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rprofile
170 lines (140 loc) · 4.61 KB
/
.Rprofile
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
if (file.exists('~/.Rprofile')) sys.source('~/.Rprofile', envir = environment())
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
sprintf("\\textcolor{%s}{%s}", color, x)
} else if (knitr::is_html_output()) {
sprintf("<span style='color: %s;'>%s</span>", color,
x)
} else x
}
knitr::opts_chunk$set(out.width='75%', fig.align='center')
options(bookdown.render.file_scope = FALSE)
print_html_only <- function(x){
if (knitr::is_html_output()) return(x)
return("")
}
print_latex_only <- function(x){
if (knitr::is_latex_output()) return(x)
return("")
}
render_rmd <- function(x) return(cat(htmltools::includeText(x)))
screen_files <- function(){
masterfile <- paste(readLines("_bookdown.yml"),
collapse = " ")
masterfile <- gsub(
'(\\[|\\]|\\")', "",
regmatches(masterfile,
gregexpr("\\[.+?\\]",
masterfile))[[1]]
)
masterfile <- unlist(
strsplit(
x = masterfile,
split = ","
)
)
masterfile <- trimws(masterfile)
masterfile <- masterfile[endsWith(masterfile, suffix = ".Rmd")]
return(masterfile)
}
get_hash <- function(){
list_files <- screen_files()
system2 <- function(...) {
stopifnot(!any(names(list(...)) %in% "intern"))
result <- base::system(..., intern = TRUE)
print(result)
}
return(
sapply(
list_files, function(k)
system2(paste0("git log -n 1 --pretty=format:%h ","./", k))
)
)
}
add_space <- function(){
files <- screen_files()
lapply(files, function(f) writeLines(c(readLines(f)," "),
con = f)
)
}
# needed for bookdown 0.20
options(bookdown.render.file_scope = FALSE)
# Créer un dossier pour les images compressées (s'il n'existe pas déjà)
dossier_images <- './pics' # This folder must exist in the repo
dossier_images_compressees <- './pics_resized'
if (!dir.exists(dossier_images_compressees)) {
dir.create(dossier_images_compressees)
}
# Copier les images si elles ne sont pas déja dans le dossier
invisible(
file.copy(list.files(dossier_images, full.names = TRUE), dossier_images_compressees, recursive=TRUE, overwrite = TRUE)
)
compresser_image <-
function(file_in,
file_out = NA, ratio_compression = 2,
xmax = 300, quality = 0.7, cutoff = 3000) {
# Fonction de compression d'une image png
# xmax <- 1920 # y pixel max
# quality <- 0.7 # passed on to jpeg::writeJPEG()
# cutoff <- 100000 # files smaller than this will not be touched
# file_out <- "test.jpg"
if (is.na(file_out)) {
file_out <- sub(dossier_images, dossier_images_compressees, file_in)
}
if (is.na(file.size(file_in))) {
stop(paste0("File ", file_in, " not found."))
}
else if (file.size(file_in) < cutoff) { # in this case, just copy file
if (!(file_in == file_out)) {
file.copy(from = file_in, to = file_out, overwrite = TRUE)
}
} else {# if larger than cutoff
# magick workflow
image_raw <- magick::image_read(path = file_in)
if (!is.na(ratio_compression)) {
image_resized <-
magick::image_scale(
image = image_raw,
geometry = as.character(as.integer(round(magick::image_info(image_raw)["width"] / ratio_compression))))
} else if (magick::image_info(image_raw)["width"] > xmax) { # only resize if smaller
image_resized <-
magick::image_scale(
image = image_raw,
geometry = as.character(xmax))
} else {
image_resized <- image_raw
}
magick::image_write(image = image_resized,
path = file_out,
format = "png")
}
}
include_image <- function(x, compression = TRUE, ratio_compression = 2, ...) {
# Fonction qui 1/ compresse l'image si nécessaire; 2/ l'inclut dans le Rmd
if (compression) {
compresser_image(file_in = x, ...)
}
knitr::include_graphics(sub(dossier_images, dossier_images_compressees, x), ...)
}
reminder_image <- function(path = "moncheminperso"){
cat(
sprintf(
paste(
"```{r, echo = FALSE}",
"include_image(\"%s\")",
"```",
sep = "\n"
), path)
)
}
message(
cat(
c("Projet source de la documentation utilitR",
"-----------------------------------------",
"",
"Pour pr\u00E9visualiser la version web de l'ouvrage: ",
" * Option 1: utiliser l'onglet 'Build' dans Rstudio;",
" * Option 2: taper dans la commande R: bookdown::render_book(\"index.Rmd\", output_dir = \"_public\")"),
sep = "\n"
)
)