mitchelloharawild / vitae

R Markdown Résumés and CVs

Home Page:https://pkg.mitchelloharawild.com/vitae/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parse R code (dataframe) to rmarkdown formatting

rempsyc opened this issue · comments

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.), steps I am currently doing manually. 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. The result when knitted formats italics and bold correctly, however all entries end up squeezed in a single paragraph instead of several lines with spaces between them.

I know you mentioned in another issue it might not be possible to create a .bib bibliography file that could then be used with scholar::bibliography_entries. However, I was wondering if there was any way to get nicely formatted knitted rmarkdown code from an R code chunk. I feel that, given the Google Scholar data is all there (and accurate in my case), it's a shame I can't use it to format it properly.

We would just need a way to declutter the squeezed paragraph on separate lines that so it looks like other regular rmarkdown entries. I've tried pasting \\newline and \\linebreak at the end of each entry but it seems LaTeX doesn't end up parsing it correctly as it just prints those commands as part of the text entries.

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")
```

pubs

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}

pubs2


Despite the indenting problem, would you consider adding this function to vitae?