Introduction

Markdown documents are fully reproducible and work with several programming languages (e.g. Python, SQL), for more details see [@RStudio:2016;@GitHub:2014].

Code formatting

Using Latex commands

Use the latex commands:

Code in the text

Code can be inserted in the text using grave accent ( $\grave{}$ ), e.g. $\grave{}$ x=1 $\grave{}$ will look like this x=1.

Code chunck

A code chunk can be inserted in regular \proglang{R} markdown blocks using the keyboard shortcut \code{Ctrl + Alt + I} (OS X: \code{Cmd + Option + I}) or by typing the chunk, such that

$\grave{}$$\;\grave{}$$\;\grave{}${r \<chunk-name> [, options]}

\<R-code>

$\grave{}$$\;\grave{}$$\;\grave{}$

The example below creates an \proglang{R} chunk named \code{simple-r-code}. I set the following options, \code{echo=TRUE} to show the chunk code, \code{eval=TRUE} to execute the \proglang{R} code, and \code{results="markup"} to show the results in markup. To see all chunk options type \code{?knitr::opts_chunk}.

$\grave{}$$\;\grave{}$$\;\grave{}${r simple-r-code, echo=TRUE, eval=TRUE, results='markup'}

x <- seq(1, 10, length.out = 100)

round(x,2)

$\grave{}$$\;\grave{}$$\;\grave{}$

The chunk above will produce the following result in the text

x <- seq(1, 10, length.out = 100)
round(x,2)

\proglang{R} plot

An \proglang{R} plot can be inserted in regular \proglang{R} markdown blocks including caption. Below I show an example using plot to create a figure in the text.

$\grave{}$$\;\grave{}$$\;\grave{}${r simple-r-plot, echo=TRUE, eval=TRUE, results="markup", fig.cap='\proglang{R} plot example.'}

y <- cos(x)

plot(x, y, type = "l", col = "red")

lines(x, -y, col = "blue")

$\grave{}$$\;\grave{}$$\;\grave{}$

This chunk produces the Figure \ref{fig:simple-r-plot} the code below. The label of the figure is automatically created as \code{fig:}. To refer to the produced figure you can use the \proglang{Latex} command \code{\textbackslash ref{fig:simple-r-plot}}.

r plot example. For \\code{LaTex} code in the caption use double backslash \\textbackslash\\textbackslash.', fig.pos="ht"} y <- cos(x) plot(x, y, type = "l", col = "red") lines(x, -y, col = "blue")



iiasa/iiasaRmarkdown documentation built on May 18, 2019, 3:41 a.m.