-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
341 lines (291 loc) · 9.41 KB
/
README.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
---
output: github_document
---
```{r getopenpharma, echo=FALSE,message=FALSE}
# reference PNGs
logos <- tools::file_path_sans_ext(dir("PNG", pattern = "\\.png$"))
# get it
library(magrittr)
openpharma_repos <- readRDS(url(
"https://github.com/openpharma/openpharma_log/blob/main/all-repos/data.rds?raw=true"
))
repositories <- tibble::tibble(
repo = logos
) %>%
dplyr::left_join(
openpharma_repos,
by = "repo"
) %>%
dplyr::mutate(
description = dplyr::case_when(
is.na(description) ~ "No description in github",
TRUE ~ description
),
Project = dplyr::case_when(
is.na(org) ~ as.character(glue::glue(
"{repo} <br />",
'<p style="color:grey">This project is not tracked by openpharma.github.io</p>'
)),
TRUE ~ as.character(glue::glue(
'<a href="https://github.com/{org}/{repo}">{org}/{repo}</a> <br />',
"<p> {description} </p>"
))
)
)
```
# Hex Stickers
Projects where we are involved.
## Adding stickers
To add one, make sure to add your hex sticker under the format `EXACT_PACKAGE_NAME.FORMAT`
- where `EXACT_PACKAGE_NAME` is the exact name of your R package,
- and `FORMAT` is the file format of the hex sticker.
For example, if your package is called `BananaPudding`, and your hex sticker is an SVG, then add `BananaPudding.svg` to the [SVG](SVG) directory.
Then, simply run `rmarkdown::render("README.Rmd")` in R to render the `README.md` file, as it will also automatically generate the hexwall and the thumbnail(s) for your logo(s).
## Hex sticker wall
```{r, echo=FALSE,message=FALSE}
# from https://github.com/mitchelloharawild/hexwall/blob/master/hexwall.R
# Dependencies
library(magick)
library(purrr)
# path: The path to a folder of hexagon stickers
# sticker_row_size: The number of stickers in the longest row
# sticker_width: The width of each sticker in pixels
# remove_small: Should hexagons smaller than the sticker_width be removed?
# coords: A data.frame of coordinates defining the placement of hexagons
# scale_coords: Should the coordinates be scaled to the hexagon size?
# remove_size: Should hexagons of an abnormal size be removed?
# sort_mode: How should the files be sorted?
# background_color: The colour of the background canvas
# n_stickers: The number of hexagons to produce. Recycled in file order.
hexwall <- function(path,
sticker_row_size = 16,
sticker_width = 500,
remove_small = TRUE,
total_stickers = NULL,
remove_size = TRUE,
scale_coords = TRUE,
sort_mode = c("filename", "random", "color", "colour"),
background_color = "white",
n_stickers = NULL) {
sort_mode <- match.arg(sort_mode)
# Load stickers
sticker_files <- list.files(path)
if (is.null(n_stickers)) n_stickers <- length(sticker_files)
sticker_files <- rep_len(sticker_files, n_stickers)
stickers <- file.path(path, sticker_files) %>%
map(function(path) {
intermediate <- switch(tools::file_ext(path),
svg = image_read_svg(path),
pdf = image_read_pdf(path),
image_read(path)
)
# Scale down to avoid cache exhausted errors
result <- image_resize(intermediate, "400x400")
image_destroy(intermediate)
result
}) %>%
map(image_transparent, "white") %>%
map(image_trim) %>%
set_names(sticker_files)
# Low resolution stickers
low_res <- stickers %>%
map_lgl(~ remove_small && image_info(.x)$width < (sticker_width - 1) / 2 && image_info(.x)$format != "svg")
which(low_res)
stickers <- stickers %>%
map(image_scale, sticker_width)
# Incorrectly sized stickers
bad_size <- stickers %>%
map_lgl(
~ remove_size && with(
image_info(.x),
height < (median(height) - 2) | height > (median(height) + 2)
)
)
which(bad_size)
# Remove bad stickers
sticker_rm <- low_res | bad_size
stickers <- stickers[!sticker_rm]
if (any(sticker_rm)) {
message(sprintf(
"Automatically removed %i incompatible stickers: %s",
sum(sticker_rm), paste0(names(sticker_rm[sticker_rm]), collapse = ", ")
))
}
if (is.null(total_stickers)) {
total_stickers <- length(stickers)
}
# Coerce sticker sizes
sticker_height <- stickers %>%
map(image_info) %>%
map_dbl("height") %>%
median()
stickers <- stickers %>%
map(image_resize, paste0(sticker_width, "x", sticker_height, "!"))
# Repeat stickers sorted by file name
stickers <- rep_len(stickers, total_stickers)
if (sort_mode == "random") {
# Randomly arrange stickers
stickers <- sample(c(
stickers,
sample(
stickers,
total_stickers - length(stickers),
replace = TRUE
)
))
} else if (sort_mode %in% c("color", "colour")) {
# Sort stickers by colour
sticker_col <- stickers %>%
map(image_resize, "1x1!") %>%
map(image_data) %>%
map(~ paste0("#", paste0(.[, , 1], collapse = ""))) %>%
map(colorspace::hex2RGB) %>%
map(as, "HSV") %>%
map_dbl(~ .@coords[, 1]) %>%
sort(index.return = TRUE) %>%
.$ix
stickers <- stickers[sticker_col]
}
# Arrange rows of stickers into images
sticker_col_size <- ceiling(length(stickers) / (sticker_row_size - 0.5))
row_lens <- rep(c(
sticker_row_size,
sticker_row_size - 1
), length.out = sticker_col_size)
row_lens[
length(row_lens)
] <- row_lens[length(row_lens)] - (length(stickers) - sum(row_lens))
sticker_rows <- map2(
row_lens, cumsum(row_lens),
~ seq(.y - .x + 1, by = 1, length.out = .x)
) %>%
map(~ stickers[.x] %>%
exec(c, !!!.) %>%
image_append())
# Add stickers to canvas
canvas <- image_blank(
sticker_row_size * sticker_width,
sticker_height + (sticker_col_size - 1) * sticker_height / 1.33526, "white"
)
reduce2(sticker_rows, seq_along(sticker_rows),
~ image_composite(
..1, ..2,
offset = paste0(
"+",
(
(..3 - 1) %% 2) * sticker_width / 2, "+",
round((..3 - 1) * sticker_height / 1.33526)
)
),
.init = canvas
)
}
image <- hexwall("PNG",
sticker_row_size = 6, sticker_width = 120,
sort_mode = "colour",
)
image_write(image, path = "hexwall.png", format = "png")
```
![](hexwall.png)
Hex details.
```{r, echo = FALSE, message = FALSE}
library(magick)
png <- paste0("PNG/", logos, ".png")
svg <- paste0("SVG/", logos, ".svg")
thumb <- paste0("thumbs/", logos, ".png")
resize <- function(path_in, path_out) {
image <- image_read(path_in)
image <- image_resize(image, "278x")
image_write(image, path_out)
}
outdated <- !file.exists(thumb) | file.mtime(thumb) < file.mtime(png)
invisible(Map(resize, png[outdated], thumb[outdated]))
```
```{r, results = "asis", echo = FALSE}
img <- glue::glue(
'<img alt="Logo for {logos}" src="{thumb}" height="139">'
)
png_link <- glue::glue('<a href="{png}">{logos}.png</a>')
cell <- paste0(
"<td>", img, "<br />",
repositories$Project,
png_link,
"</td>"
)
cols <- 3
rows <- ceiling(length(cell) / cols)
row_id <- rep(seq_len(rows), each = cols, length.out = length(cell))
row_cells <- split(cell, row_id)
cat("<table>\n")
cat(paste0("<tr>", sapply(row_cells, paste, collapse = ""), "</tr>"), sep = "")
cat("</table>\n")
```
## HTML
Generated HTML wall of hexes at https://insightsengineering.github.io/hex-stickers
```{r, include = FALSE}
input_data <- tibble::tibble(
package = logos,
png_link = png
) %>%
dplyr::left_join(
repositories,
by = c("package" = "repo")
) %>%
dplyr::select(package, png_link, full_name) %>%
dplyr::mutate(
repo_link = glue::glue(
'<a href="https://github.com/{full_name}">{full_name}</a>'
),
png_link = glue::glue("https://raw.githubusercontent.com/insightsengineering/hex-stickers/main/{png_link}")
)
hexes <- NULL
for (i in 1:nrow(input_data)){
i_row <- input_data[i,]
hexes <- paste0(
hexes,
"
",
glue::glue(
'<li class="hex">
<div class="hexIn">
<a class="hexLink" href="https://github.com/{i_row$full_name}">
<img src="{i_row$png_link}" alt="" >
</a>
</div>
</li>
'
)
)
}
paste0(
'
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="hex.css">
<link href="https://fonts.googleapis.com/css?family=Raleway:300" rel="stylesheet" type="text/css">
</head>
<body>
<ul id="hexGrid">
',
hexes,
"
</ul>
</body>
</html>
"
) %>% write(
file = "docs/index.html"
)
```
# Acknowledgements
Code for this repo has been forked from `rstudio/hex-stickers` and `mitchelloharawild/hexwall`.
Copyright of the images is defined by the sourced project (see source repo for hex-sticker)
## Stargazers
[![Stargazers repo roster for @insightsengineering/hex-stickers](https://reporoster.com/stars/dark/insightsengineering/hex-stickers)](https://github.com/insightsengineering/hex-stickers/stargazers)
[![Stargazers over time](https://starchart.cc/insightsengineering/hex-stickers.svg)](https://starchart.cc/insightsengineering/hex-stickers)
## Forkers
[![Forkers repo roster for @insightsengineering/hex-stickers](https://reporoster.com/forks/dark/insightsengineering/hex-stickers)](https://github.com/insightsengineering/hex-stickers/network/members)