Customizing a publication list with R Markdown

Rstats
publication list
Notes on creating a customizable list of publications for an academic website using R markdown, .bib files, and other fun stuff.
Author

Matt Crump

Published

May 17, 2022

I’ve been using R Markdown to generate my lab website for years. I recently switched from the generic R markdown website to a website generated by pkgdown. I’m happy with the result. As a part of the migration I’m revisiting individual pages like my publications page.

Over the years I’ve tried different ways to list publications. I like any process that takes a .bib file containing my publications, and then auto-generates everything I want to have.

bibbase

I was previously using bibbase, which takes a .bib file as input and embeds a list of publications into a webpage. For example, I used to generate a publication list by inserting a script into the .Rmd for my publications page.

 <script src="https://bibbase.org/show?bib=https://crumplab.github.io/Crump.bib&jsonp=1&nocache=1&theme=side&authorFirst=1"></script>

It was quick, easy, and pretty good overall.

bibbase issues

But, there were nuisances. I couldn’t get the formatting exactly right. I don’t think bibbase supports different .csl formats, so it doesn’t display citations in APA format.

Bibbase recognizes extra tags in the .bib file to define arbitrary links, and then have the links printed to each citation. For example, a citation might have a pdf, a website, and data associated with it. That was nice.

However, the links double-clicked themselves. I’m not sure why this happened to me, but clicking a link to download a .pdf would cause the file to be downloaded twice. That was annoying.

What I wanted

Here is the workflow that I wanted to achieve:

  1. Maintain my list of publications in a zotero folder. Then, export the folder as a biblatex repository (with .pdfs).
  2. Have an .Rmd file that reads in the .bib file, and then outputs the list of publications
  3. The list ideally could be formatted by any .csl file, which would make it easy to output in APA format
  4. The list should automatically add any extra links and stuff that I want (provided those things can be extracted from the .bib file).

R Markdown issues and solutions

R Markdown is generally great for citing things. For example, I could cite a paper (Vuorre and Crump 2021), the citation would appear in the text, and a full citation would be printed in a reference section at the end of the document.

However, it’s not so easy to print a full citation in the middle of an R Markdown document, in a style that you want defined by .csl, and with additional stuff you might want like extra links.

At least, I couldn’t find a way to do that until this morning, when I came across a life-saver function from stevemisc called print_refs().

There’s at least a handful of ways to input a .bib file into R, and then print out a single entry. For example, RefManageR can do something like this, but it doesn’t support .csl, so the output may not be in the style you want (and it doesn’t output to APA).

Here’s a quick example of print_refs() in action.

library(bib2df)
library(stevemisc)
library(stringi)

# load a bib file to data frame
bib_df <- bib2df(file="Crump.bib")

# clean entries
bib_df$TITLE <- stri_replace_all_regex(bib_df$TITLE, "[\\{\\}]", "")
bib_df$JOURNAL <- stri_replace_all_regex(bib_df$JOURNAL, "[\\{\\}]", "")
bib_df$BOOKTITLE <- stri_replace_all_regex(bib_df$BOOKTITLE, "[\\{\\}]", "")

# convert a single row back to .bib entry
bib_entry <- paste0(capture.output(df2bib(bib_df[1,])), collapse="")
bib_entry
#> [1] "@Article{allanPsychophysicsContingencyAssessment2008,  Author = {Allan, Lorraine G. and Hannah, Samuel D. and Crump, Matthew J. C. and Siegel, Shepard},  Number = {2},  Pages = {226--243},  Publisher = {{American Psychological Association (APA)}},  Title = {The Psychophysics of Contingency Assessment.},  Volume = {137},  Ids = {Allan_2008},  Date = {2008},  Journaltitle = {Journal of Experimental Psychology: General},  Issn = {1939-2222, 0096-3445},  Doi = {10.1037/0096-3445.137.2.226},  Url = {https://CrumpLab.github.io/CognitionPerformanceLab/CrumpPubs/Allan et al. - 2008.pdf},  Urldate = {2013-07-03},  Abstract = {The authors previously described a procedure that permits rapid, multiple within-participant evaluations of contingency assessment (the “streamed-trial” procedure, M. J. C. Crump, S. D. Hannah, L. G. Allan, \\& L. K. Hord, 2007). In the present experiments, they used the streamed-trial procedure, combined with the method of constant stimuli and a binary classification response, to assess the psychophysics of contingency assessment. This strategy provides a methodology for evaluating whether variations in contingency assessment reflect changes in the participant’s sensitivity to the contingency or changes in the participant’s response bias (or decision criterion). The sign of the contingency (positive or negative), outcome density, and imposition of an explicit payoff structure had little influence on sensitivity to contingencies but did influence the decision criterion. The authors discuss how a psychophysical analysis can provide a better understanding of findings in the literature such as mood and age effects on contingency assessment. They also discuss the relation between a psychophysical approach and an associative account of contingency assessment.},  File = {/Users/mattcrump/Zotero/storage/AMVABHNJ/Allan et al. - 2008 - The psychophysics of contingency assessment..pdf}}"

# print out the citation
stevemisc::print_refs(bib_entry,
                      csl = "apa.csl",
                      spit_out = TRUE,
                      delete_after = FALSE)
#> Allan, L. G., Hannah, S. D., Crump, M. J. C., & Siegel, S. (2008). The
#> psychophysics of contingency assessment. *Journal of Experimental
#> Psychology: General*, *137*(2), 226–243.
#> <https://doi.org/10.1037/0096-3445.137.2.226>

I’m so glad this function exists. It turns the .bib file into markdown that can be printed directly inside an .Rmd. And, this can be done programmatically using knitr chunks. For example, using results=asis in the knitr chunk options allows the citation to printed to the .Rmd document.


```{r, results="asis", echo=FALSE}
  print_me <- paste0(stevemisc::print_refs(bib_entry,csl = "apa.csl",
                                        spit_out = FALSE,
                                        delete_after = FALSE), collapse=" ")
  cat(print_me)
```

And, this means the citation should show up nicely on the webpage, like this:

Allan, L. G., Hannah, S. D., Crump, M. J. C., & Siegel, S. (2008). The psychophysics of contingency assessment. Journal of Experimental Psychology: General, 137(2), 226–243. https://doi.org/10.1037/0096-3445.137.2.226

That’s all

I now have a working pipeline that inputs a .bib file, and outputs a list of publications in APA format, with a few customizable bells and whistles.

I would feel like this excursion was wrapped up if I refactored the script into a set of functions. But, I’ll leave that for another day.

via GIPHY

Functionalizing

Ideally I would like to run a single function like this, and have a whole publication list generated, complete with extra links and icons add to each entry.

bib_2_pub_list("mybib.bib")

I don’t have that solution yet, but may update this post when I have time to make progress in that direction.

In order for the above to work it be necessary to include any metadata for the links in the .bib file. This could be done using the extras field in zotero. I’m already using this approach to export urls. I ran into a few roadblocks attempting to generalize this approach.

Alternatively, two inputs might be better. For example, a .yml file could be used to define metadata for links.

bib_2_pub_list("mybib.bib","mybib.yml")

Hmmm, need to brainstorm a .yml structure. This should work. A citation key, followed by numbered links, each containing a name, url, and font awesome icon.

vuorreSharingOrganizingResearch2021:
  link1:
    name: 'website'
    url: 'https://www.crumplab.com/vertical'
    icon: 'fas fa-globe'
  link2:
    name: 'github'
    url: 'https://github.com/CrumpLab/vertical'
    icon: 'fas fa-github'

behmerCrunchingBigData2017:
  link1:
    name: 'data'
    url: 'https://github.com/CrumpLab/BehmerCrump2017_BigData'
    icon: 'fas fa-database'

I can read in the .yml like this, which turns everything into a list.

yml_links <- yaml::read_yaml("Crump.yml")

Then, need to write some functions…


::: {.cell}

```{.r .cell-code}
add_link_icon <- function(url_path,url_text, icon_class){
  html <- glue::glue('<a href = "{url_path}"> <i class="{icon_class}"> {url_text} </i></a>')
  cat("  ",html, sep="")
}

:::


bib_2_pub_list <- function(bib,yml,pdf_dir,base_url_to_pdfs){

  # load bib file to df
  bib_df <- bib2df::bib2df(bib)

  # clean {{}} from entries
  # to do: improve this part
  bib_df$TITLE <- stringi::stri_replace_all_regex(bib_df$TITLE, "[\\{\\}]", "")
  bib_df$JOURNAL <- stringi::stri_replace_all_regex(bib_df$JOURNAL, "[\\{\\}]", "")
  bib_df$BOOKTITLE <- stringi::stri_replace_all_regex(bib_df$BOOKTITLE, "[\\{\\}]", "")

  # sort bib_df by year
  # to do: add sort options
  bib_df <- bib_df[order(bib_df$DATE, decreasing=T),]

  # read yml with links for bib entries
  yml_links <- yaml::read_yaml(yml)

  # print entries

  for (i in 1:dim(bib_df)[1] ){

    # convert row to .bib entry
    # to do: make row to bib entry a function
    t_bib_entry <- paste0(capture.output(bib2df::df2bib(bib_df[i,])), collapse="")
    # generate markdown text for citation
    t_md_citation<- paste0(stevemisc::print_refs(t_bib_entry,csl = "apa.csl",
                                                 spit_out = FALSE,
                                                 delete_after = FALSE), collapse=" ")
    cat(t_md_citation)

    cat("<span class = 'publinks'>")

    ### add pdf links
    if( !is.na(bib_df$FILE[i]) ) { #check pdf exists

      pdf_name <- basename(bib_df$FILE[i])
      rel_path_to_pdf <- list.files(here::here(pdf_dir),
                                    basename(bib_df$FILE[i]),
                                    recursive=T)
      build_url <- paste0(base_url_to_pdfs,"/",rel_path_to_pdf,collapse="")
      crumplab::add_link_icon(build_url,"pdf","fas fa-file-pdf")

    }

    ## add all other links
    if( exists(bib_df$BIBTEXKEY[i],yml_links) ) { # check yml bib entry exists

      link_list <- yml_links[[bib_df$BIBTEXKEY[i]]]

      for(l in link_list){
        crumplab::add_link_icon(l$url,l$name,l$icon)
      }

    }
    cat("</span>")
    cat("\n\n")
  }

}

Does it blend?

crumplab::bib_2_pub_list("Crump.bib",
                         "Crump.yml",
                         "pkgdown/assets/Crump/files",
                         "https://www.crumplab.com/Crump/files")

Brosowsky, N. P., & Crump, M. J. C. (2021). Contextual recruitment of selective attention can be updated via changes in task relevance. Canadian Journal of Experimental Psychology, 75, 19–34. https://doi.org/10.1037/cep0000221 pdf

Vuorre, M., & Crump, M. J. C. (2021). Sharing and organizing research products as r packages. Behavior Research Methods, 53, 792–802. https://doi.org/10.3758/s13428-020-01436-x pdf website github

Crump, M. J. C., Jamieson, R. K., Johns, B. T., & Jones, M. N. (2020). Controlling the retrieval of general vs specific semantic knowledge in the instance theory of semantic memory. In S. Denison, M. Mack, Y. Xu, & B. C. Armstrong (Eds.), Proceedings of the 42nd annual conference of the cognitive science society (pp. 3261–3267). Cognitive Science Society. pdf

Crump, M. J. C. (2020). Reproducible statistics for psychologists with r: Lab tutorials. OSF. https://crumplab.github.io/rstatsforpsych/

Johns, B. T., Jamieson, R. K., Crump, M. J. C., Jones, M. N., & Mewhort, D. J. K. (2020). Production without rules: Using an instance memory model to exploit structure in natural language. Journal of Memory and Language, 115, 104165. https://doi.org/ghcm2c pdf

Aujla, H., Crump, M. J. C., Cook, M. T., & Jamieson, R. K. (2019). The semantic librarian: A search engine built from vector-space models of semantics. Behavior Research Methods, 51(6), 2405–2418. https://doi.org/gf6cmv pdf

Braem, S., Bugg, J. M., Schmidt, J. R., Crump, M. J. C., Weissman, D. H., Notebaert, W., & Egner, T. (2019). Measuring adaptive control in conflict tasks. Trends in Cognitive Sciences, 23(9), 769–783. https://doi.org/gf48x7 pdf

Crump, M. (2019). Answering questions with data: Course website. https://doi.org/ghrn62

Crump, M., Navarro, D., & Suzuki, J. (2019). Answering questions with data (textbook): Introductory statistics for psychology students. https://doi.org/ghrn59

Crump, M., Krishnan, A., Volz, S., & Chavarga, A. (2019). Answering questions with data: The lab manual for r, excel, SPSS and jamovi. https://doi.org/ghrn63

Crump, M. J. C., Lai, W., & Brosowsky, N. P. (2019). Instance theory predicts information theory: Episodic uncertainty as a determinant of keystroke dynamics. Canadian Journal of Experimental Psychology/Revue Canadienne de Psychologie Expérimentale, 73(4), 203–215. https://doi.org/http://dx.doi.org/10.1037/cep0000182 pdf

Crump, M. J. C. (2019). Portfolio and prosper. Nature Human Behaviour, 3(10), 1008–1008. https://doi.org/ghrnn6 pdf

Behmer, L. P., Jantzen, K. J., Martinez, S., Walls, R., Amir-Brownstein, E., Jaye, A., Leytze, M., Lucier, K., & Crump, M. J. C. (2018). Parallel regulation of past, present, and future actions during sequencing. Journal of Experimental Psychology: Human Perception and Performance, 44(8), 1147–1152. https://doi.org/gdznzb pdf

Brosowsky, N. P., & Crump, M. J. C. (2018). Memory-guided selective attention: Single experiences with conflict have long-lasting effects on cognitive control. Journal of Experimental Psychology: General, 147(8), 1134–1153. https://doi.org/gd2w6z pdf

Crump, M. J. C., Milliken, B., Leboe-McGowan, J., Leboe-McGowan, L., & Gao, X. (2018). Context-dependent control of attention capture: Evidence from proportion congruent effects. Canadian Journal of Experimental Psychology/Revue Canadienne de Psychologie Expérimentale, 72(2), 91–104. https://doi.org/gdrskv pdf

Crump, M. J. C., Brosowsky, N. P., & Milliken, B. (2017). Reproducing the location-based context-specific proportion congruent effect for frequency unbiased items: A reply to hutcheon and spieler (2016). Quarterly Journal of Experimental Psychology, 70(9), 1792–1807. https://doi.org/gcx8qn pdf

Behmer, L. P., & Crump, M. J. C. (2017). The dynamic range of response set activation during action sequencing. Journal of Experimental Psychology: Human Perception and Performance, 43(3), 537–554. https://doi.org/f9w78w pdf

Behmer, L. P., & Crump, M. J. C. (2017). Crunching big data with finger tips: How typists tune their performance towards the statistics of natural language. In M. N. Jones (Ed.), Big data in cognitive science (pp. 319–341). Routledge. pdf data

Behmer, L. P., & Crump, M. J. C. (2017). Spatial knowledge during skilled action sequencing: Hierarchical versus non-hierarchical representations. Attention, Perception & Psychophysics, 79, 2435–2448. https://doi.org/10.3758/s13414-017-1389-3 pdf

Zumsteg, J. W., Crump, M. J., Logan, G. D., Weikert, D. R., & Lee, D. H. (2017). The effect of carpal tunnel release on typing performance. The Journal of Hand Surgery, 42(1), 16–23. https://doi.org/f9mjkj pdf

Brosowsky, N. P., & Crump, M. J. C. (2016). Context-specific attentional sampling: Intentional control as a pre-requisite for contextual control. Consciousness and Cognition, 44, 146–160. https://doi.org/f848wz pdf

Crump, M. J. C. (2016). Learning to selectively attend from context-specific attentional histories: A demonstration and some constraints. Canadian Journal of Experimental Psychology/Revue Canadienne de Psychologie Expérimentale, 70(1), 59–77. https://doi.org/gf7h25 pdf

Curtis, E. T., Chubala, C. M., Spear, J., Jamieson, R. K., Hockley, W. E., & Crump, M. J. C. (2016). False recognition of instruction-set lures. Memory, 24, 32–43. https://doi.org/10.1080/09658211.2014.982657 pdf

Johns, B. T., Jamieson, R., Crump, M., Jones, M. N., & Mewhort, D. J. (2016). The combinatorial power of experience. Proceedings of the 38th Annual Conference of the Cognitive Science Society. pdf

Crump, M. J. C., McDonnell, J. V., & Gureckis, T. M. (2013). Evaluating amazon’s mechanical turk as a tool for experimental behavioral research. PLoS ONE, 8(3), e57410. https://doi.org/f4qw94 pdf

Crump, M. J. C., & Logan, G. D. (2013). Prevention and correction in post-error performance: An ounce of prevention, a pound of cure. Journal of Experimental Psychology: General, 142(3), 692–709. https://doi.org/f46hg6 pdf

Yamaguchi, M., Crump, M. J. C., & Logan, G. D. (2013). Speed–accuracy trade-off in skilled typewriting: Decomposing the contributions of hierarchical control loops. Journal of Experimental Psychology: Human Perception and Performance, 39(3), 678–699. https://doi.org/10.1037/a0030512 pdf

Bugg, J. M., & Crump, M. J. C. (2012). In support of a distinction between voluntary and stimulus-driven control: A review of the literature on proportion congruent effects. Frontiers in Psychology, 3(367), 1–16. https://doi.org/10.3389/fpsyg.2012.00367 pdf

Crump, M. J. C., Logan, G. D., & Kimbrough, J. (2012). Keeping an eye on guitar skill: Visual representations of guitar chords. Music Perception: An Interdisciplinary Journal, 30(1), 37–47. https://www.jstor.org/stable/10.1525/mp.2012.30.1.37 pdf

Crump, M. J. C. (2012). Review of guitar zero: The new musician and the science of learning. https://doi.org/10.1037/a0030741 pdf

Jamieson, R. K., Crump, M. J. C., & Hannah, S. D. (2012). An instance theory of associative learning. Learning & Behavior, 40(1), 61–82. https://doi.org/dwkrm5 pdf

Logan, G. D., & Crump, M. J. C. (2011). Hierarchical control of cognitive processes: The case for skilled typewriting. In B. H. Ross (Ed.), Psychology of learning and motivation (Vol. 54, pp. 1–27). Elsevier. pdf

Crump, M. J. C., & Logan, G. D. (2010). Contextual control over task-set retrieval. Attention, Perception, & Psychophysics, 72(8), 2047–2053. https://doi.org/c6psw7 pdf

Crump, M. J. C., & Logan, G. D. (2010). Warning: This keyboard will deconstruct— the role of the keyboard in skilled typewriting. Psychonomic Bulletin & Review, 17(3), 394–399. https://doi.org/d9jmzm pdf

Crump, M. J. C., & Logan, G. D. (2010). Hierarchical control and skilled typing: Evidence for word-level control over the execution of individual keystrokes. Journal of Experimental Psychology: Learning, Memory, and Cognition, 36(6), 1369–1380. https://doi.org/ccjxg5 pdf

Crump, M. J. C., & Logan, G. D. (2010). Episodic contributions to sequential control: Learning from a typist’s touch. Journal of Experimental Psychology: Human Perception and Performance, 36(3), 662–672. https://doi.org/ccw3b5 pdf

Jamieson, R. K., Hannah, S. D., & Crump, M. J. C. (2010). A memory-based account of retrospective revaluation. Canadian Journal of Experimental Psychology/Revue Canadienne de Psychologie Expérimentale, 64(3), 153–164. https://doi.org/fn46rb pdf

Liu, X., Crump, M. J. C., & Logan, G. D. (2010). Do you know where your fingers have been? Explicit knowledge of the spatial layout of the keyboard in skilled typists. Memory & Cognition, 38(4), 474–484. https://doi.org/ccq7jw pdf

Logan, G. D., & Crump, M. J. C. (2010). Cognitive illusions of authorship reveal hierarchical error detection in skilled typists. Science, 330(6004), 683–686. https://doi.org/bktxxk pdf

Crump, M. J. C., & Milliken, B. (2009). The flexibility of context-specific control: Evidence for context-driven generalization of item-specific control settings. The Quarterly Journal of Experimental Psychology, 62(8), 1523–1532. https://doi.org/10.1080/17470210902752096 pdf

Hannah, S. D., Crump, M. J. C., Allan, L. G., & Siegel, S. (2009). Cue-interaction effects in contingency judgments using the streamed-trial procedure. Canadian Journal of Experimental Psychology/Revue Canadienne de Psychologie Expérimentale, 63(2), 103–112. https://doi.org/fgwn3x pdf

Logan, G. D., & Crump, M. J. C. (2009). The left hand doesn’t know what the right hand is doing: The disruptive effects of attention to the hands in skilled typewriting. Psychological Science, 20(10), 1296–1300. https://doi.org/10.1111/j.1467-9280.2009.02442.x pdf

Siegel, S., Allan, L. G., Hannah, S. D., & Crump, M. J. C. (2009). Applying signal detection theory to contingency assessment. Comparative Cognition & Behavior Reviews, 4, 116–134. https://doi.org/10.3819/ccbr.2009.40012 pdf

Allan, L. G., Hannah, S. D., Crump, M. J. C., & Siegel, S. (2008). The psychophysics of contingency assessment. Journal of Experimental Psychology: General, 137(2), 226–243. https://doi.org/10.1037/0096-3445.137.2.226 pdf

Crump, M. J. C., Vaquero, J. M. M., & Milliken, B. (2008). Context-specific learning and control: The roles of awareness, task relevance, and relative salience. Consciousness and Cognition, 17(1), 22–36. https://doi.org/d2fm7p pdf

Crump, M. J. C., Milliken, B., & Ansari, I. (2008). Shifting views on the symbolic cueing effect: Cueing attention through recent prior experience. Psicológica, 29(1), 97–114. https://CrumpLab.github.io/CognitionPerformanceLab/CrumpPubs/Crump et al. - 2008.pdf pdf

Leboe, J. P., Wong, J., Crump, M. J. C., & Stobbe, K. (2008). Probe-specific proportion task repetition effects on switching costs. Perception & Psychophysics, 70(6), 935–945. https://doi.org/10.3758/PP.70.6.935 pdf

Crump, M. J. C., Hannah, S. D., Allan, L. G., & Hord, L. K. (2007). Contingency judgements on the fly. Quarterly Journal of Experimental Psychology, 60(6), 753–761. https://doi.org/b9jjc4 pdf

Schmidt, J. R., Crump, M. J. C., Cheesman, J., & Besner, D. (2007). Contingency learning without awareness: Evidence for implicit control. Consciousness and Cognition, 16(2), 421–435. https://doi.org/d9bv53 pdf

Crump, M. J. C., Gong, Z., & Milliken, B. (2006). The context-specific proportion congruent stroop effect: Location as a contextual cue. Psychonomic Bulletin & Review, 13(2), 316–321. https://doi.org/fpxkfs pdf

via GIPHY

That works pretty well.

Next step is to include this function in my crumplab package that is part of this webpage, and make it work for real.

References

Vuorre, Matti, and Matthew J. C. Crump. 2021. “Sharing and Organizing Research Products as R Packages.” Behavior Research Methods 53: 792–802. https://doi.org/10.3758/s13428-020-01436-x.