Skip to content

Instantly share code, notes, and snippets.

@rpietro
Created July 29, 2013 02:58
Show Gist options
  • Save rpietro/6101906 to your computer and use it in GitHub Desktop.
Save rpietro/6101906 to your computer and use it in GitHub Desktop.
script to test multiple functions from knitcitations
# this gist contains code extracted straight from http://goo.gl/wvKPH , http://goo.gl/YFhU9
library(devtools)
install_github("knitcitations", "cboettig")
require(knitcitations)
setwd("~/Desktop")
#------------------------------------------------------------------------------
# grabbing reference using a DOI (digital object identifier), which is also available through pubmed's xml such as in <ELocationID EIdType="doi" ValidYN="Y">10.1371/journal.pone.0065291</ELocationID>
r <- ref("10.1111/j.1461-0248.2005.00827.x")
#------------------------------------------------------------------------------
# below can be used inside markdown as it takes html
print(r, "html")
#------------------------------------------------------------------------------
# inline citation
citet("10.1111/j.1461-0248.2005.00827.x")
citep(c("10.1111/j.1461-0248.2005.00827.x", "10.1890/11-0011.1")) # this is preferable as it generates the citation within parentheses
#------------------------------------------------------------------------------
# creating citation keys
citep(c(Michaels = "10.1111/j.1755-263X.2012.00241.x"))
citep("Michaels")
citet("Halpern2006") # if not key is passed knitcitations will create an automatic key with first author last name + year
citet("Halpern2006_") # duplicate names and years will add an underscore for differentiation
#------------------------------------------------------------------------------
# creating a bibtex file from DOI
refs <- lapply(c("10.1111/j.1461-0248.2005.00827.x", "10.1890/11-0011.1"),
ref)
write.bibtex(refs, file = "refs.bib")
write.bibtex(c("bibtex", "knitr", "knitcitations"), file = "example.bib") # creates bibtex file for some R packages, pretty cool
#------------------------------------------------------------------------------
#reading bibtex file into R
bib <- read.bibtex("example.bib")
bib[["knitr"]] # creates citations
citet(bib[["knitr"]]) # inline citations
citep(bib[c("knitr", "bibtex")]) # same thing but within parentheses
citet("knitr") # citing the bibkey directly
#------------------------------------------------------------------------------
# within knitr use `r citep("10.1111/j.1461-0248.2005.00827.x")` for inline citations
# at the simply add the following to generate the citations
bibliography("html")
#------------------------------------------------------------------------------
# adding semantics to the citations through the CITO ontology http://goo.gl/IgLFE
citet("10.1186/2041-1480-1-S1-S6", cito="usesMethodIn")
# in a static site like jekyl you can have bootstrap js libraries generate a mouse over that would show all the info about a citation, thus avoiding that you have to navigate to the end of the article to know exactly what the citation is all about. this can toggled by calling
cite_options(tooltip=TRUE)
# the js required for the mouseover effect described above is:
# <script type="text/javascript">
# $(document).ready(function (){
# $(".showtooltip").tooltip();
# });
# </script>
#------------------------------------------------------------------------------
# when DOIs are not available, such as in arXiv papers
citep("http://knowledgeblog.org/greycite", cito="usesMethodIn")
#------------------------------------------------------------------------------
# citation formatting is not supported in knitcitations as it is semantic and targets online environments such as posting on blogs with jekyll. this means that converting to bibtex like demonstrated above might still be necessary when the go is a formal peer-reviewed submission
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment