Add new function to create .bib bibliography file for later processing
Closed this issue · 6 comments
I am making my CV in R with the vitae
package and I would like to import my publications from Google Scholar with the scholar
package and format them in rmarkdown (primary author in bold, journals in italics, etc.). The problem is turning R code to rmarkdown and making it look good.
In my code chunk I use results = "asis"
and create the dataframe, but the result is a single squeezed paragraph instead of several lines with spaces between them.
Unless you would be aware of a trick to turn R code to good-looking rmarkdown code when knitted (is there one?), one solution would be to add a function to create a .bib bibliography file that could then be used with vitae::bibliography_entries
.
In the meanwhile, here is a workaround and function that give decent results. Reproducible rmarkdown example:
---
output: html_document
---
# Publications
```{r, results = "asis", echo = FALSE, message = FALSE}
format.authors <- function(scholar.profile, author.name) {
library(dplyr)
swap_initials <- function(author.name) {
sub("(.*) (.*)", "\\2, \\1.", trimws(author.name))
}
pubs <- scholar::get_publications(scholar.profile)
pubs %>%
strsplit(x = .$author, split = ",") -> pubs2
lapply(pubs2, function(x) {
x <- swap_initials(x)
x[length(x)] <- paste0("& ", x[length(x)])
x <- paste0(x, collapse = ", ")
ifelse(startsWith(x, "& "), sub("& ", "", x), x)
}
) -> pubs$author
author.name2 <- swap_initials(author.name)
pubs %>%
arrange(desc(year)) %>%
mutate(journal = paste0("*", journal, "*"),
Publications = paste0(author, " (", year, "). ",
title, ". ", journal, ". ",
number),
Publications = gsub(author.name2, paste0("**", author.name2, "**"), Publications)) %>%
select(Publications)
}
pubs <- format.authors("NrfwEncAAAAJ", "R Thériault")
cat(unlist(pubs), sep = "\\\n \\\n")
```
However, this workaround won't work if you are indenting your publications in vitae
/LaTeX
: the other references will all be indented by LaTex as if they were the same unit/paragraph. Using the following to indent:
\setlength{\parindent}{-0.2in}
\setlength{\leftskip}{0.2in}
Despite the indenting problem, would you consider adding this function to scholar
?
Amazing!
One reflection I had was whether we should include within the function output the following bit for proper rmarkdown formatting:
cat(unlist(pubs), sep = "\\\n \\\n")
If not included the full rmarkdown workflow would need to be something like:
scholar::format_publications("NrfwEncAAAAJ", "R Thériault") |>
unlist() |>
cat(sep = "\\\n \\\n")
Or should we let the user do that? I think adding it would be one fewer step for the user using rmarkdown. However, it would not be in a proper format for further manipulation outside of rmarkdown (and not consistent with other functions). Yet, I don't know how people would use this outside of rmarkdown. Perhaps only to make some manual edits if the function fails in some aspects.
Second, be aware that there is a minor issue that my function does not deal with yet, which is the situation of having multiple initials. For example, it formats the initials SA as "SA.", whereas technically according to APA style it should be "S. A.". But that's probably the lowest possible priority. If I find a fix eventually I'll come back to post it here.
try this:
---
output: html_document
---
```{r results = "asis"}
scholar::format_publications("NrfwEncAAAAJ", "R Thériault") |> cat(sep='\n\n')
```
```{r results = "asis"}
scholar::format_publications("NrfwEncAAAAJ", "R Thériault")
```
```{r results = "asis"}
scholar::format_publications("NrfwEncAAAAJ", "R Thériault") |> print(quote=FALSE)
```
Awesome, the first one looks good for APA style, and the last one I think could be useful for other styles that use numbering. I think it would be neat to eventually have a vignette (or blog post) for this.
You are welcome to create a PR for the vignette.