Skip to content

Instantly share code, notes, and snippets.

@jsta
Created September 16, 2021 01:26
Show Gist options
  • Save jsta/521b21f15ab25131f2edec16fd8bc694 to your computer and use it in GitHub Desktop.
Save jsta/521b21f15ab25131f2edec16fd8bc694 to your computer and use it in GitHub Desktop.
Display a bib file as bootstrap cards
---
title: "Papers"
description: >
My papers!
output:
html_document:
theme:
version: 4
---
```{css, echo=FALSE, eval=TRUE}
card {
margin-top: 0px;
margin-bottom: 0px;
margin-right: 0px;
margin-left: 0px;
}
```
```{r, include = FALSE, message=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(tibble)
library(bs4cards)
library(bib2df)
library(dplyr)
library(janitor)
library(stringr)
library(interleave)
format_author <- function(x) {
# x <- bib$author[14]
which_jsta <- which(stringr::str_detect(x, "Stachelek"))
if (length(which_jsta) == 0) {
return(x)
}
if (which_jsta == 1 | length(x) == 2) {
return(paste0(x, collapse = ", "))
} else {
if (which_jsta != 3) {
paste0(
c(
x[c(1:2)],
paste0("<", which_jsta - 3, " authors>"),
x[which_jsta],
paste0("<", length(x) - which_jsta, " authors>")
),
collapse = ", ")
} else {
paste0(
c(
x[c(1:2)],
x[which_jsta],
paste0("<", length(x) - which_jsta, " authors>")
),
collapse = ", ")
}
}
}
```
```{r eval=TRUE, echo=FALSE, message=FALSE, warning = FALSE}
bib <- bib2df::bib2df("static/data/jsta.bib") %>%
clean_names() %>%
dplyr::filter(category != "BOOK", !is.na(journal)) %>%
dplyr::select(-abstract:-isbn, -shorttitle:-issn,
-type:-volume, -key:-series, -address:-annote, -booktitle:-institution,
-language:-file) %>%
dplyr::arrange(desc(year)) %>%
mutate(title = stringr::str_remove_all(title, "\\{\\{")) %>%
mutate(title = stringr::str_remove_all(title, "\\}\\}")) %>%
mutate(title = stringr::str_replace_all(title, "\\\\textendash", " -"))
bib$author <- sapply(bib$author, function(x) format_author(x)[[1]])
bib <- mutate(bib, author = str_replace_all(author, "Joseph", "J"))
key <- data.frame(bibtexkey = bib$bibtexkey, category = NA)
key$category <- c(
list(c("ml", "limnology")), "coastal",
list(c("limnology", "ml")), list(c("limnology", "ml")),
list(c("ml", "limnology")), "limnology",
"limnology", "limnology",
list(c("limnology", "geocomp")), "limnology",
"limnology", "limnology",
"coastal", "limnology",
"coastal", "coastal",
list(c("geocomp", "limnology")), list(c("opensci", "geocomp")),
"limnology", list(c("geocomp", "opensci")),
"coastal", "coastal")
bib <- left_join(dplyr::select(bib, -category), key, by = "bibtexkey")
bib$category_primary <- unlist(lapply(bib$category, function(x) x[1]))
# bib[grep("coastal", bib$category_primary),]
bib[
interleave::interleave(
as.matrix(data.frame(
ml = grep("ml", bib$category_primary)[1:2],
limnology = grep("limnology", bib$category_primary)[1:2],
coastal = grep("coastal", bib$category_primary)[c(3, 5)]
))
),
] %>%
cards(
title = title,
text = author,
link = paste0("https://doi.org/", doi),
footer = paste0(year, " - ", journal),
layout = "label-only",
# width = "row-cols-3",
tags = category_primary
)
key_table <- data.frame(table(unlist(key$category))) %>%
dplyr::arrange(desc(Freq))
key_table <- setNames(data.frame(t(key_table$Freq)), key_table$Var1)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment