To work with R Markdown, if necessary:
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
The following document is mainly based on https://bookdown.org/yihui/rmarkdown/r-code.html.
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I (Cmd + Option + I on macOS).
There are a lot of things you can do in a code chunk: you can produce text output, tables, or graphics. You have fine control over all these output via chunk options, which can be provided inside the curly braces (between ```{r and }).
The value of a chunk option can be an arbitrary R expression, which makes chunk options extremely flexible. For example, the chunk option eval controls whether to evaluate (execute) a code chunk, and you may conditionally evaluate a chunk via a variable defined previously, e.g.,
# execute code if the date is later than a specified day
do_it = Sys.Date() > '2018-02-14'
x = rnorm(10000)
x[1:5]
## [1] -2.0331336 1.0357938 0.5269961 -0.7879876 0.8870512
hist(x,55)
There are a large number of chunk options in knitr documented at https://yihui.name/knitr/options. We list a subset of them below:
eval: Whether to evaluate a code chunk.
echo: Whether to echo the source code in the output document (someone may not prefer reading your smart source code but only results).
results: When set to 'hide', text output will be hidden; when set to 'asis', text output is written “as-is”, e.g., you can write out raw Markdown text from R code (like cat('**Markdown** is cool.\n')). By default, text output will be wrapped in verbatim elements (typically plain code blocks).
collapse: Whether to merge text output and source code into a single code block in the output. This is mostly cosmetic: collapse = TRUE makes the output more compact, since the R source code and its text output are displayed in a single output block. The default collapse = FALSE means R expressions and their text output are separated into different blocks.
warning, message, and error: Whether to show warnings, messages, and errors in the output document. Note that if you set error = FALSE, rmarkdown::render() will halt on error in a code chunk, and the error will be displayed in the R console. Similarly, when warning = FALSE or message = FALSE, these messages will be shown in the R console.
include: Whether to include anything from a code chunk in the output document. When include = FALSE, this whole code chunk is excluded in the output, but note that it will still be evaluated if eval = TRUE. When you are trying to set echo = FALSE, results = 'hide', warning = FALSE, and message = FALSE, chances are you simply mean a single option include = FALSE instead of suppressing different types of text output individually.
Besides code chunks, you can also insert values of R objects inline in text. For example:
For a circle with the radius 15, its area is 706.8583471.
You can write LATEX code easily, e.g. \(y_{i}=\mathbf{x}_{i}^{\top}\mathbf{\beta}+\epsilon_{i}\), \(\epsilon_{i}\sim N(0,\sigma^{2})\).
\(\left(\int_{a}^{b} f(x) \; dx\right)\), \(\sum_{x = a}^{b} f(x)\), \(\prod_{x = a}^{b} f(x)\), \(\log(x)\), \(\exp(x)\)
Greek letters: \(\epsilon \varepsilon E\)
To write an equation \[\sum_{n=1}^{10} n^2\]