-
Notifications
You must be signed in to change notification settings - Fork 11
/
99-references.Rmd
116 lines (108 loc) · 3.71 KB
/
99-references.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
`r if (knitr:::is_html_output()) '# References {-}'`
```{r}
#| label: refs-package-list
#| include: FALSE
# generate a BibTeX database automatically for some R packages
library(targets)
library(visdat)
library(readxl)
library(httr2)
library(jsonlite)
library(tidycensus)
library(pak)
our_write_bib <- function (x = .packages(), file = "", tweak = TRUE, width = NULL,
prefix = getOption("knitr.bib.prefix", "R-"), lib.loc = NULL)
{
.this.year = sprintf(' year = {%s},', format(Sys.Date(), '%Y'))
system.file = function(...) base::system.file(..., lib.loc = lib.loc)
citation = function(...) utils::citation(..., lib.loc = lib.loc)
x = x[nzchar(x)]
idx = mapply(system.file, package = x) == ""
if (any(idx)) {
warning("package(s) ", paste(x[idx], collapse = ", "),
" not found")
x = x[!idx]
}
x = setdiff(x, setdiff(xfun::base_pkgs(), "base"))
x = sort(x)
bib = sapply(x, function(pkg) {
cite = citation(pkg, auto = if (pkg != "base") {
meta = packageDescription(pkg, lib.loc = lib.loc)
if (identical(meta$Repository, "CRAN") && !is.null(meta$URL)) {
if (!grepl("[, ]", meta$URL))
meta$Repository = NULL
}
meta
})
if (tweak) {
cite$title = gsub(sprintf("^(%s: )(\\1)", pkg),
"\\1", cite$title)
cite$title = gsub(pkg, paste0("{", pkg, "}"), cite$title)
cite$title = gsub("\\b(R)\\b", "{R}", cite$title)
cite$title = gsub("\\b(ggplot2)\\b", "{ggplot2}", cite$title)
cite$title = gsub("\\b(dplyr)\\b", "{dplyr}", cite$title)
cite$title = gsub("\\b(tidyverse)\\b", "{tidyverse}", cite$title)
cite$title = gsub("\\b(sf)\\b", "{sf}", cite$title)
cite$title = gsub(" & ", " \\\\& ", cite$title)
}
entry = toBibtex(cite)
entry[1] = sub("\\{,$", sprintf("{%s%s,", prefix, pkg),
entry[1])
entry
}, simplify = FALSE)
if (tweak) {
for (i in intersect(names(knitr:::.tweak.bib), x)) {
message("tweaking ", i)
bib[[i]] = merge_list(bib[[i]], knitr:::.tweak.bib[[i]])
}
bib = lapply(bib, function(b) {
b["author"] = sub("Duncan Temple Lang", "Duncan {Temple Lang}",
b["author"])
# b["title"] = gsub("(^|\\W)'([^']+)'(\\W|$)", "\\1\\2\\3",
# b["title"])
if (!is.na(b["note"]))
b["note"] = gsub("(^.*?https?://.*?),\\s+https?://.*?(},\\s*)$",
"\\1\\2", b["note"])
if (!("year" %in% names(b)))
b["year"] = .this.year
b
})
}
bib2 = lapply(x, function(pkg) {
if (pkg == "base")
return()
if (system.file("CITATION", package = pkg) == "")
return()
cites = citation(pkg, auto = FALSE)
cites = Filter(x = cites, function(cite) {
!isTRUE(grepl("R package version", cite$note))
})
s = knitr:::make_unique(unlist(lapply(cites, function(cite) {
if (is.null(cite$year))
format(Sys.Date(), "%Y")
else cite$year
})))
mapply(cites, s, FUN = function(cite, suffix) {
if (isTRUE(grepl("R package version", cite$note)))
return()
entry = toBibtex(cite)
entry[1] = sub("\\{,$", sprintf("{%s%s,", pkg, suffix),
entry[1])
entry
}, SIMPLIFY = FALSE)
})
bib = c(bib, unlist(bib2, recursive = FALSE))
bib = lapply(bib, function(b) {
idx = which(names(b) == "")
if (!is.null(width))
b[-idx] = str_wrap(b[-idx], width, 2, 4)
structure(c(b[idx[1L]], b[-idx], b[idx[2L]], ""), class = "Bibtex")
})
if (!is.null(file) && length(x))
xfun::write_utf8(unlist(bib), file)
invisible(bib)
}
our_write_bib(c(
.packages(), 'bookdown', 'knitr', 'rmarkdown', 'renv', 'here'
), 'packages.bib')
```