-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.qmd
More file actions
90 lines (77 loc) · 2.36 KB
/
library.qmd
File metadata and controls
90 lines (77 loc) · 2.36 KB
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
---
title: "Library"
format: html
---
Resources that we have found useful for data visualisation work.
```{r, echo=FALSE, message=FALSE, warning=FALSE}
suppressPackageStartupMessages(library(DT))
suppressPackageStartupMessages(library(readr))
data <- read_csv("data/library.csv", show_col_types = FALSE)
section_index <- which(names(data) == "Section") - 1
url_index <- which(names(data) == "URL")
if (length(url_index) == 1) {
data$URL <- ifelse(
is.na(data$URL) | data$URL == "",
"",
sprintf(
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
data$URL,
htmltools::htmlEscape(data$URL)
)
)
}
# Set widths per column (in px). Change these values as needed.
column_widths <- c(
"Name/Alt text" = 300,
"URL" = 320,
"Section" = 120,
"Medium" = 120
)
column_defs <- lapply(names(column_widths), function(column_name) {
list(
width = paste0(column_widths[[column_name]], "px"),
targets = which(names(data) == column_name) - 1
)
})
DT::datatable(
data,
filter = "top",
escape = if (length(url_index) == 1) which(names(data) != "URL") else TRUE,
rownames = FALSE,
options = list(
pageLength = 10,
autoWidth = FALSE,
columnDefs = column_defs,
initComplete = JS(sprintf(
"function() {
var api = this.api();
var sectionIdx = %d;
var headerRows = $(api.table().node()).find('thead tr');
var filterRow = headerRows.last();
if (!filterRow.length || filterRow.find('input').length === 0) {
return;
}
var filterCell = filterRow.find('th').eq(sectionIdx);
var existingInput = filterCell.find('input');
if (!filterCell.length || !existingInput.length) {
return;
}
var select = $('<select style=\"width: 100%%;\"><option value=\"\">All</option></select>')
.appendTo(filterCell.empty())
.on('change', function() {
var val = $.fn.dataTable.util.escapeRegex($(this).val());
api.column(sectionIdx).search(val ? '^' + val + '$' : '', true, false).draw();
});
api.column(sectionIdx).data().unique().sort().each(function(d) {
if (d !== null && d !== undefined && d !== '') {
select.append('<option value=\"' + d + '\">' + d + '</option>');
}
});
}",
section_index
))
)
)
```
<div id="footer">
You can contact us at [digitalcontent@ons.gov.uk](mailto:digitalcontent@ons.gov.uk) and we will set up a consultation to see how we can best support you.</div>