Академический Документы
Профессиональный Документы
Культура Документы
Explore R Markdown
The document to the right is a template R Markdown document. It includes the most
familiar parts of an R Markdown document:
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code
that generated the plot.
*italics*
**bold**
`code`
You can turn a word into a link by surrounding it in hard brackets and then placing the
link behind it in parentheses, like this:
[RStudio](www.rstudio.com)
To create titles and headers, use leading hastags. The number of hashtags determines
the header's level:
Lists in R Markdown
To make a bulleted list in Markdown, place each item on a new line after an asterisk
and a space, like this:
* item 1
* item 2
* item 3
You can make an ordered list by placing each item on a new line after a number
followed by a period followed by a space, like this
1. item 1
2. item 2
3. item 3
In each case, you need to place a blank line between the list and any paragraphs that
come before it.
LaTeX equations
You can also use the Markdown syntax to embed latex math equations into your
reports. To embed an equation in its own centered equation block, surround the
equation with two pairs of dollar signs like this,
$$1 + 1 = 2$$
To embed an equation inline, surround it with a single pair of dollar signs, like this: $1 +
1 = 2$.
You can use all of the standard latex math symbols to create attractive equations.
$$ celsius = kelvin - 273.15 $$
celsius=kelvin−273.15
fahrenheit=celsius×9/5+32
R code chunks
You can embed R code into your R Markdown report with the knitr syntax. To do this,
surround your code with two lines: one that contains ```{r} and one that contains ```.
The result is a code chunk that looks like this:
```{r}
# some code
```
When you render the report, R will execute the code. If the code returns any results, R
will add them to your report.
For example, R Markdown would ignore any errors or warnings generated by the chunk
below.
If eval = FALSE, R Markdown will not run the code or include its results, (but it will still
display the code unless told otherwise).
If results = 'hide', R Markdown will not display the results of the code (but it will still
run the code and display the code itself unless told otherwise).
Inline R code
You can embed R code into the text of your document with the `r ` syntax. Be sure to
include the lower case r in order for this to work properly. R Markdown will run the code
and replace it with its result, which should be a piece of text, such as a character string
or a number.
For example, the line below uses embedded R code to create a complete sentence:
When you render the document the result will appear as:
Inline code provides a useful way to make your reports completely automatable.
EXERCISE
Labeling and reusing code chunks
Apart from the popular code chunk options you have learned by now, you can define
even more things in the curly braces that follow the triple backticks.
An interesting feature available in knitr is the labeling of code snippets. The code
chunk below would be assigned the label simple_sum:
However, because the results option is equal to hide, no output is shown. This is what
appears in the output document:
2 + 2
What purpose do these labels serve? knitr provides the option ref.label to refer to
previously defined and labeled code chunks. If used correctly, knitr will copy the code
of the chunk you referred to and repeat it in the current code chunk. This feature
enables you to separate R code and R output in the output document, without code
duplication.
## [1] 4
Notice that the echo option was explicitly set to FALSE, suppressing the R code that
generated the output.
In the sample code on the right, you see a rather large code chunk that contains R code
to load packages dplyr and ggvis and functions to create a ggvis graph
First, you can click the triangle icon next to "Knit HTML" at the bottom of the pane that
displays your R Markdown file. This will open a drop down menu that gives you the
choice of rendering as an HTML document or a pdf document.
Second, you can change the output field in the YAML block at the top of your document.
For example, this YAML block will create an HTML file:
---
output: html_document
---
---
output: pdf_document
---
---
output: word_document
---
And this one will create a Markdown file:
---
output: md_document
---