You may need to install some software for this sequence of 3 tutorials.
install.packages(c('knitr','shiny','dplyr','ggplot2','maps','spocc','wallace','rmarkdown'), dep=T)
The same set of tools is used for all these!
Peng 2011, Science 334(6060) pp. 1226-1227
rmarkdown
: minor extensions to allow R code display and execution, embed images in html files, equationsknitr
: Dynamic documents in R {.columns-2}
https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
The reference guide is a little more comprehensive
In Rstudio: File -> New File -> RMarkdown -> Document -> HTML
This will open a document that looks like this:
All R code to be run must be in a code chunk like this:
#```{r,eval=F}
CODE HERE
#```
Add a new code chunk at the bottom of this template file to load these packages (you may need to install some packages):
library(dplyr)
library(ggplot2)
library(maps)
library(spocc)
Do you think you should put
install.packages()
calls in your script?
Now use the occ()
function in new code chunk to download all the occurrence records for the American robin (Turdus migratorius) from the Global Biodiversity Information Facility.
Licensed under CC BY-SA 3.0 via Wikimedia Commons
## define which species to query
sp='Turdus migratorius'
## run the query and convert to data.frame()
d = occ(query=sp, from='ebird',limit = 100) %>% occ2df()
# Load coastline
map=map_data("world")
ggplot(d,aes(x=longitude,y=latitude))+
geom_polygon(aes(x=long,y=lat,group=group,order=order),data=map)+
geom_point(col="red")+
coord_equal()
## Warning: Ignoring unknown aesthetics: order
Update the YAML header to keep the markdown file from this:
title: "Untitled"
author: "Adam M. Wilson"
date: "October 31, 2016"
output: html_document
To this (you need to save the markdown (md) document to make the html):
title: "Demo"
author: "Adam M. Wilson"
date: "October 31, 2016"
output:
html_document:
keep_md: true
And click knit HTML
to generate the output
Changing this YAML header is all that’s needed to change among document types. Try experimenting. (pdf won’t work unless you have latex installed.)
kniting produces a number of files all extracted from the Rmd you wrote, seen below
In case you had trouble generating this document, the R Script associated with this page is available here.
kable()
function for tables (e.g. kable(head(d))
)#```{r,eval=F}
knitr::opts_chunk$set(cache=TRUE)
#```
#```{r, echo=FALSE}
#```{r, eval=FALSE}
How does markdown magically convert between document types (html, pdf, docx)?
A universal document converter, open source, cross-platform
http://kieranhealy.org/blog/archives/2014/01/23/plain-text/
See Rmd file for full references and sources