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

Add new function to create .bib bibliography file for later processing #110

Closed
rempsyc opened this issue May 18, 2022 · 6 comments
Closed

Comments

@rempsyc
Copy link

rempsyc commented May 18, 2022

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.

@rempsyc
Copy link
Author

rempsyc commented Jun 5, 2022

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

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 scholar?

@GuangchuangYu
Copy link
Collaborator

incorporated, see YuLab-SMU@03a9394#diff-3fc538c4fa03e2b111fe20ab306291513d8d306fb319e4ea6cb64b671f7f014e.

@rempsyc
Copy link
Author

rempsyc commented Jun 21, 2022

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.

@GuangchuangYu
Copy link
Collaborator

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

@rempsyc
Copy link
Author

rempsyc commented Jun 22, 2022

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.

@GuangchuangYu
Copy link
Collaborator

You are welcome to create a PR for the vignette.

GuangchuangYu added a commit to YuLab-SMU/scholar that referenced this issue Aug 9, 2022
updated vignette and readme with format_publications function issue jkeirstead#110
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

2 participants