Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse R code (dataframe) to rmarkdown formatting #208

Open
rempsyc opened this issue May 18, 2022 · 1 comment
Open

Parse R code (dataframe) to rmarkdown formatting #208

rempsyc opened this issue May 18, 2022 · 1 comment

Comments

@rempsyc
Copy link

rempsyc commented May 18, 2022

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.

@rempsyc
Copy link
Author

rempsyc commented Jun 5, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant