Getting package information for a whole project including citations


1) Use grep in project directory, add -R if you want to include subfolders.

If you are not on linux, you can just use the terminal in RStudio for the step below

# bash / terminal
grep -h "library*" *.Rmd | sort --u > packlist.txt

Note1: You might want to have a quick look at this file and remove anything weird that isn’t library(packagename)
Note2: Change .Rmd to .R if you use R scripts instead of markdown.


2) Read this list into R and remove everything except the package name

packs <- read.delim("packlist.txt", header = F)

library(stringr)

packages <- str_extract(string = packs$V1, pattern = "(?<=\\().*(?=\\))")


3) Gets a list of currently installed packages in Rstudio and thier versions.

IMPORTANT: make sure that all of the packages you used in the project are actually installed on the machine you are running this from (if not currently on your usual project computer).

pack_inst <- as.data.frame(installed.packages()[,c(1,3:4)])


4) Extract only the packages in your list

result <- pack_inst[pack_inst$Package %in% packages, ]

# Print onscreen
print(result)
##                   Package Version Priority
## cowplot           cowplot   1.1.1     <NA>
## data.table     data.table  1.14.0     <NA>
## dplyr               dplyr   1.0.7     <NA>
## ggplot2           ggplot2   3.3.4     <NA>
## gridExtra       gridExtra     2.3     <NA>
## RColorBrewer RColorBrewer   1.1-2     <NA>
## readxl             readxl   1.3.1     <NA>
## rehh                 rehh   3.2.1     <NA>
## stringr           stringr   1.4.0     <NA>
# or save to file

# write(result, "package_vers.txt")


5) Get citations

res <- result$Package

for (i in res){
  print(citation(i))
}
## 
## To cite package 'cowplot' in publications use:
## 
##   Claus O. Wilke (2020). cowplot: Streamlined Plot Theme and Plot
##   Annotations for 'ggplot2'. R package version 1.1.1.
##   https://CRAN.R-project.org/package=cowplot
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {cowplot: Streamlined Plot Theme and Plot Annotations for 'ggplot2'},
##     author = {Claus O. Wilke},
##     year = {2020},
##     note = {R package version 1.1.1},
##     url = {https://CRAN.R-project.org/package=cowplot},
##   }
## 
## 
## To cite package 'data.table' in publications use:
## 
##   Matt Dowle and Arun Srinivasan (2021). data.table: Extension of
##   `data.frame`. R package version 1.14.0.
##   https://CRAN.R-project.org/package=data.table
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {data.table: Extension of `data.frame`},
##     author = {Matt Dowle and Arun Srinivasan},
##     year = {2021},
##     note = {R package version 1.14.0},
##     url = {https://CRAN.R-project.org/package=data.table},
##   }
## 
## 
## To cite package 'dplyr' in publications use:
## 
##   Hadley Wickham, Romain François, Lionel Henry and Kirill Müller
##   (2021). dplyr: A Grammar of Data Manipulation. R package version
##   1.0.7. https://CRAN.R-project.org/package=dplyr
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {dplyr: A Grammar of Data Manipulation},
##     author = {Hadley Wickham and Romain François and Lionel Henry and Kirill Müller},
##     year = {2021},
##     note = {R package version 1.0.7},
##     url = {https://CRAN.R-project.org/package=dplyr},
##   }
## 
## 
## To cite ggplot2 in publications, please use:
## 
##   H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
##   Springer-Verlag New York, 2016.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Book{,
##     author = {Hadley Wickham},
##     title = {ggplot2: Elegant Graphics for Data Analysis},
##     publisher = {Springer-Verlag New York},
##     year = {2016},
##     isbn = {978-3-319-24277-4},
##     url = {https://ggplot2.tidyverse.org},
##   }
## 
## 
## To cite package 'gridExtra' in publications use:
## 
##   Baptiste Auguie (2017). gridExtra: Miscellaneous Functions for "Grid"
##   Graphics. R package version 2.3.
##   https://CRAN.R-project.org/package=gridExtra
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {gridExtra: Miscellaneous Functions for "Grid" Graphics},
##     author = {Baptiste Auguie},
##     year = {2017},
##     note = {R package version 2.3},
##     url = {https://CRAN.R-project.org/package=gridExtra},
##   }
## 
## 
## To cite package 'RColorBrewer' in publications use:
## 
##   Erich Neuwirth (2014). RColorBrewer: ColorBrewer Palettes. R package
##   version 1.1-2. https://CRAN.R-project.org/package=RColorBrewer
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {RColorBrewer: ColorBrewer Palettes},
##     author = {Erich Neuwirth},
##     year = {2014},
##     note = {R package version 1.1-2},
##     url = {https://CRAN.R-project.org/package=RColorBrewer},
##   }
## 
## 
## To cite package 'readxl' in publications use:
## 
##   Hadley Wickham and Jennifer Bryan (2019). readxl: Read Excel Files. R
##   package version 1.3.1. https://CRAN.R-project.org/package=readxl
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {readxl: Read Excel Files},
##     author = {Hadley Wickham and Jennifer Bryan},
##     year = {2019},
##     note = {R package version 1.3.1},
##     url = {https://CRAN.R-project.org/package=readxl},
##   }
## 
## 
## To cite package 'rehh' in publications use:
## 
##   Gautier M. and Vitalis R. (2012). rehh: An R package to detect
##   footprints of selection in genome-wide SNP data from haplotype
##   structure. Bioinformatics, 28(8):1176-1177
## 
##   Gautier M., Klassmann A., and Vitalis R. (2017). rehh 2:0. A
##   reimplementation of the R package rehh to detect positive selection
##   from haplotype structure. Molecular Ecology Resources, 17(1):78-90
## 
## To see these entries in BibTeX format, use 'print(<citation>,
## bibtex=TRUE)', 'toBibtex(.)', or set
## 'options(citation.bibtex.max=999)'.
## 
## 
## To cite package 'stringr' in publications use:
## 
##   Hadley Wickham (2019). stringr: Simple, Consistent Wrappers for
##   Common String Operations. R package version 1.4.0.
##   https://CRAN.R-project.org/package=stringr
## 
## A BibTeX entry for LaTeX users is
## 
##   @Manual{,
##     title = {stringr: Simple, Consistent Wrappers for Common String Operations},
##     author = {Hadley Wickham},
##     year = {2019},
##     note = {R package version 1.4.0},
##     url = {https://CRAN.R-project.org/package=stringr},
##   }