Languages

Using R-markdown

Basic text formatting

Spacing, manual breaks, lines

Tables

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell
{r, echo=TRUE,results='asis'}
library(knitr)
kable(head(iris))
library(knitr)
kable(head(iris))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Fencing

Tables

First Header  | Second Header
------------- | -------------
Content Cell  | Content Cell
Content Cell  | Content Cell
{r, echo=TRUE,results='asis'}
library(knitr)
kable(head(iris))
library(knitr)
kable(head(iris))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Equations

$$y = a + b$$

\[y = a + b\]

Subcripts

$$H_0 = Z_{a + b}$$

\[H_0 = Z_{a + b}\]

Superscripts

$$S = cA^z$$

\[S = cA^z\]

  • elements can be coupled and nested

\[S=cA^z_1 + z_{2 + x}\]

$$S=cA^z_1 + z_{2 + x}$$

Fractions and Greek Symbols

\[\alpha = \frac{\beta}{\delta + \gamma_x}\]

$$\alpha = \frac{\beta}{\delta + \gamma_x}$$

Summation signs

\[z = \sum_{i=1}^X{K}\]

$$z = \sum_{i=1}^X{K}$$

“Escaping” the  special character in LaTeX

Use \backslash

$$\backslash \alpha \le b \backslash$$

\[\backslash \alpha \le b \backslash\]

Rendering plain text in a LaTex Equation

\[P(Occurrence Of Species A) = Z\]

$$P(Occurrence Of Species A) = Z$$

\[P(\mbox{Occurrence Of Species A}) = Z\]

$$P(\mbox{Occurrence Of Species A}) = Z$$

R Code in Chunks

# Use comments extensively in ALL of your coding!
Pred <- seq(1,10)     # make a vector of integers from 1 to 10
Res <- runif(10)      # generate 10 draws from a random uniform (0,1) distribution

# print the random numbers
print(Res)
##  [1] 0.2383757 0.4377084 0.5717281 0.6752646 0.2680563 0.6470778 0.3148969
##  [8] 0.6643741 0.0141859 0.5488485
# plot the graph
plot(x=Pred,y=Res,type="b")

Using chunk options echo and eval to control printing of code and output

plot(runif(50),cex=10*runif(50),col="goldenrod")

print(rnorm(n=10, mean=10, sd=2))
##  [1]  9.799092  7.905844  8.292692 10.982835  9.774276  9.724717 10.017520
##  [8]  9.563490 10.651323 11.543441

Writing R scripts

# First comment to explain what this program is doing.
# Be expansive and describe it in great detail. This may seem trivial, but will become increasingly important as you create complex programs.
# Simple script to examine the distribution of the product of two uniform variables
# Make sure it is readable. Use complete sentences, not cryptic phrases.
# 6 September 2018
# NJG

# Preliminaries
library(ggplot2)
set.seed(100)
library(TeachingDemos) # use this to set the random number seed from a character string
char2seed("green tea")
char2seed("green tea",set=FALSE)
## [1] 7023541
#

# Global variables
nRep <- 10000

# Create or read in data
ranVar1 <- rnorm(nRep)
# print(ranVar1)
head(ranVar1)
## [1] -0.67595800  0.07142749 -0.13803318 -0.99300667 -1.17181879  0.51340840
tail(ranVar1)
## [1]  0.35209291 -0.11819204  0.57509922 -0.05122504 -1.98879289 -1.04776740
ranVar2 <- rnorm(nRep)


# visualize data
qplot(x=ranVar1)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# create product vector
ranProd <- ranVar1*ranVar2
length(ranProd)
## [1] 10000
str(ranProd)
##  num [1:10000] -1.3151 0.0117 0.1435 2.2531 -0.9411 ...
head(ranProd)
## [1] -1.31510790  0.01168855  0.14346105  2.25309300 -0.94114160  0.13995337
# do other stuff..

Converting .R files to .html (creating an R notebook)

Purling to strip and consolidate R chunks from a Markdown file

library(knitr)
purl("FileName.Rmd")

These commands will create a file FileName.R that has all of the R code and none of the markdown text. R comments are still retained.

Leave RStudio and Work in Typora


  1. Originally “Yet Another Markdown Language”, but more recently “YAML Ain’t no Markdown Language”, which is a self-referencing acronym↩︎

  2. Footnoted text, consecutively at bottom of page↩︎

  3. Footnoted text, consecutively at bottom of page↩︎