-
Notifications
You must be signed in to change notification settings - Fork 1
/
docs.qmd
201 lines (179 loc) · 5.41 KB
/
docs.qmd
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
---
title: "Data documentation"
---
Click on each dataset's name to see its description, license, citation, and link to its original source. Code used to create these data can be found [here](https://github.com/ben-domingue/irw/tree/main/data).
```{r}
library(dplyr)
library(purrr)
library(readr)
library(stringr)
library(glue)
# Load datasets
data_index <- read_csv("data/IRW Data Dictionary - data index.csv",
na = character()) |>
select(dataset = `Filename`, url = URL, license = `Derived License`,
description = Description, reference = Reference, doi = DOI) |>
mutate(dataset = str_remove(dataset, ".R.ata$"))
dataset_without_doi <- read_csv("data/BibTex_Manual.csv", na=character())
# Populate missing DOI with BibTex from dataset_without_doi
data_index <- data_index %>%
left_join(dataset_without_doi, by = c("dataset" = "Filename")) %>%
mutate(doi = if_else(is.na(doi) | doi == "", BibTex, doi)) %>%
select(-BibTex)
```
```{=html}
<input type="text" id="searchBar" placeholder="Search datasets..." style="width: 100%; padding: 10px; margin-bottom: 20px; font-size: 16px;">
<script>
function searchDatasets() {
var input = document.getElementById('searchBar');
var filter = input.value.toLowerCase();
var items = document.getElementsByClassName('dataset-item');
for (var i = 0; i < items.length; i++) {
var text = items[i].textContent || items[i].innerText;
items[i].style.display = text.toLowerCase().indexOf(filter) > -1 ? '' : 'none';
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getCitation(doi, buttonId, targetId) {
const button = document.getElementById(buttonId);
button.disabled = true; // Disable the button
button.textContent = 'Loading...'; // Show loading state
try {
let citation;
if (doi.startsWith('@')) {
// Directly assign BibTeX if available
button.textContent = 'Loading...'; // Show loading state
await sleep(500);
citation = doi;
} else {
// Fetch BibTeX using DOI
const response = await fetch(`https://doi.org/${doi}`, {
headers: { Accept: 'application/x-bibtex' },
});
if (!response.ok) throw new Error('Failed to fetch citation');
citation = await response.text();
}
const target = document.getElementById(targetId);
const container = document.createElement('div');
container.className = 'citation-container';
const citationText = document.createElement('pre');
citationText.className = 'citation-pre';
citationText.textContent = citation;
const copyButton = document.createElement('button');
copyButton.className = 'copy-button';
copyButton.innerHTML = '📋 Copy';
copyButton.onclick = () => {
navigator.clipboard
.writeText(citation)
.then(() => {
copyButton.innerHTML = '✓ Copied!';
setTimeout(() => (copyButton.innerHTML = '📋 Copy'), 2000);
})
.catch(() => (copyButton.innerHTML = '❌ Error'));
};
target.innerHTML = '';
container.appendChild(citationText);
container.appendChild(copyButton);
target.appendChild(container);
target.style.display = 'block';
} catch (error) {
console.error('Error fetching citation:', error);
const target = document.getElementById(targetId);
target.textContent = 'Error fetching citation. Please try again.';
target.style.display = 'block';
} finally {
// Re-enable the button after processing
button.disabled = false;
button.textContent = 'Get BibTex';
}
}
document.getElementById('searchBar').addEventListener('input', searchDatasets);
</script>
<style>
.citation-container {
position: relative;
margin-top: 10px;
padding: 10px;
background-color: #f5f5f5;
border-radius: 4px;
min-height: 50px;
}
.citation-pre {
font-family: monospace;
white-space: pre-wrap;
word-wrap: break-word;
margin: 0;
padding: 20px 10px 10px 10px;
max-width: 100%;
overflow-x: auto;
}
.copy-button {
position: absolute;
top: 5px;
right: 5px;
background-color: #ffffff;
border: 1px solid #dddddd;
border-radius: 4px;
padding: 4px 8px;
cursor: pointer;
font-size: 12px;
z-index: 1;
}
.copy-button:hover {
background-color: #f0f0f0;
}
.citation-button {
background-color: #007bff;
color: white;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
}
.citation-button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
</style>
```
```{r}
#| results: asis
el <- function(ds) {
lic <- if (ds$license != "") glue("({ds$license})") else ""
doi <- ds$doi
escaped_doi <- if (!is.na(doi) && doi != "") gsub("'", "\\\\'", doi) else ""
doi_button <- if (escaped_doi != "") {
button_id <- paste0("cite-button-", make.names(ds$dataset))
target_id <- paste0("cite-text-", make.names(ds$dataset))
glue(
'<button id="{button_id}" class="citation-button" onclick="getCitation(`{escaped_doi}`, \'{button_id}\', \'{target_id}\')">
Get BibTex
</button>
<div id="{target_id}" class="citation-text"></div>'
)
} else ""
glue(
"
::: {{.g-col-4 .dataset-item}}
::: {{.callout-note collapse='true'}}
## {ds$dataset}
{ds$description} {lic} [[link]]({ds$url})
<i>{ds$reference}</i>
{doi_button}
:::
:::
"
)
}
els <- transpose(data_index) |> map(el) |> paste(collapse = "\n")
cat(glue(
"
::: {{.grid}}
{els}
:::
"
))
```