Вы находитесь на странице: 1из 6

R Markdown

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:

 A YAML header that contains some metadata


 Narrative text written in Markdown
 R code chunks surrounded by ```{r} and ```; a syntax that comes from the knitr
package

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code
that generated the plot.

Styling narrative sections


You can use Markdown to embed formatting instructions into your text. For example,
you can make a word italicized by surrounding it in asterisks, bold by surrounding it in
two asterisks, and monospaced (like code) by surrounding it in backticks:

*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:

# First level header


## Second level header
### Third level header

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 $$

$$ fahrenheit = celsius \times \frac{9}{5} + 32 $$

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.

Customize R code chunks


You can customize each R code chunk in your report by providing optional arguments
after the r in ```{r}, which appears at the start of the code chunk. Let's look at one set
of options.

R functions sometimes return messages, warnings, and even error messages. By


default, R Markdown will include these messages in your report. You can use
the message, warning and error options to prevent R Markdown from displaying these. If
any of the options are set to FALSE R Markdown will not include the corresponding type
of message in the output.

For example, R Markdown would ignore any errors or warnings generated by the chunk
below.

```{r warning = FALSE, error = FALSE}


"four" + "five"
```

Popular chunk options


Three of the most popular chunk options are echo, evaland results.
If echo = FALSE, R Markdown will not display the code in the final document (but it will
still run the code and display its results unless told otherwise).

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:

The factorial of four is `r factorial(4)`.

When you render the document the result will appear as:

The factorial of four is 24.

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:

```{r simple_sum, results = 'hide'}


2 + 2
```

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.

Let's continue the example; the following code chunk:

```{r ref.label='simple_sum', echo = FALSE}


```

produces the output you would expect:

## [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

Alternative output formats


You can render the same R Markdown file into several different formats. There are two
ways to change a file's output format.

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
---

This one will create a pdf file:

---
output: pdf_document
---

This one will create a MS Word file:

---
output: word_document
---
And this one will create a Markdown file:

---
output: md_document
---

Вам также может понравиться