R "stats" citation for a scientific paper

As hrbrmstr pointed out, a function to create a list of references of only loaded packages would come in handy. As he only showed us an example and not the function, I wrote one myself which I use very often in scientific analyses and papers (sometimes combined with R Markdown).

citations <- function(includeURL = TRUE, includeRStudio = TRUE) {
    if(includeRStudio == TRUE) {
        ref.rstudio <- RStudio.Version()$citation
        if(includeURL == FALSE) {
            ref.rstudio$url <- NULL;
        }
        print(ref.rstudio, style = 'text')
        cat('\n')
    }

    cit.list <- c('base', names(sessionInfo()$otherPkgs))
    for(i in 1:length(cit.list)) {
        ref <- citation(cit.list[i])
        if(includeURL == FALSE) {
            ref$url <- NULL;
        }
        print(ref, style = 'text')
        cat('\n')
    }
}

So, for example, after running

library(readr)
library(dplyr)
library(ggplot2)
library(knitr)

the function citations() will print:

RStudio Team (2016). RStudio: Integrated Development Environment for R. RStudio, Inc., Boston, MA. http://www.rstudio.com.

R Core Team (2017). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org.

Xie Y (2016). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.15.1, http://yihui.name/knitr.

Xie Y (2015). Dynamic Documents with R and knitr, 2nd edition. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 978-1498716963, http://yihui.name/knitr.

Xie Y (2014). “knitr: A Comprehensive Tool for Reproducible Research in R.” In Stodden V, Leisch F and Peng RD (eds.), Implementing Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595, http://www.crcpress.com/product/isbn/9781466561595.

Wickham H (2009). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-0-387-98140-6, http://ggplot2.org.

Wickham H and Francois R (2016). dplyr: A Grammar of Data Manipulation. R package version 0.5.0, https://CRAN.R-project.org/package=dplyr.

Wickham H, Hester J and Francois R (2016). readr: Read Tabular Data. R package version 1.0.0, https://CRAN.R-project.org/package=readr.


There is now a grateful package that can be handy:

The goal of grateful is to make it very easy to cite the R packages used in any report or publication. By calling a single function, it will scan the project for R packages used and generate a document with citations in the desired output format (Word, PDF, HTML, Markdown). Importantly, these references can be formatted for a specific journal so that we can just paste them directly into the bibliography list of our manuscript or report.

https://github.com/Pakillo/grateful

If the package stats is loaded, the reference can be obtained by running:

library(grateful)
cite_packages()

—assuming grateful has already been installed by running:

library(devtools)
install_github("Pakillo/grateful")

In our recent book, my co-author and I did the R citation (in the frontmatter) but also got the publisher to let us give per-package credit as well:

enter image description here

We felt that it was important to ensure those that did the work got credit all the way 'round.

(I wld have made this only a comment, but can't easily embed pix that way and rly didn't want to host the img somewhere.)


The reviewer is wrong:

 citation("stats")

The ‘stats’ package is part of R.  To cite R in publications use:

  R Core Team (2013). R: A language and environment for statistical computing. R
  Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-07-0, URL
  http://www.R-project.org/.

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {R: A Language and Environment for Statistical Computing},
    author = {{R Core Team}},
    organization = {R Foundation for Statistical Computing},
    address = {Vienna, Austria},
    year = {2013},
    note = {{ISBN} 3-900051-07-0},
    url = {http://www.R-project.org/},
  }

We have invested a lot of time and effort in creating R, please cite it when
using it for data analysis. See also ‘citation("pkgname")’ for citing R
packages.