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.
``{r warning = FALSE, error = FALSE}
"four" + "five"
```
Three of the most popular chunk options are echo, eval and 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).
You can embed R code into the text of your document with the `r `
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
```
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.
```{r ref.label='simple_sum', echo = FALSE}
```
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
---
But what if you are using R in a terminal window? You can run the command rmarkdown::render(<file path>) to render any .Rmd file with R.
You can also export your file as a slideshow by changing the output field to:
---
output: beamer_presentation
---
which creates a beamer pdf slideshow,
---
output: ioslides_presentation
---
which creates an ioslides HTML slideshow or
---
output: slidy_presentation
---
which creates a slidy HTML slideshow.
R Markdown will start a new slide at each first or second level header in your document. You can insert additional slide breaks with Markdown's horizontal rule syntax:
***
Everywhere you add these three asteriks in your text, pandoc will create a new slide.
Each R Markdown output template is a collection of knitr and pandoc options. You can customize your output by overwriting the default options that come with the template.
For example, the YAML header below overwrites the default code highlight style of the pdf_document template to create a document that uses the zenburn style:
---
title: "Demo"
output:
pdf_document:
highlight: zenburn
---
The YAML header below overwrites the default bootstrap CSS theme of the html_document template.
---
title: "Demo"
output:
html_document:
theme: spacelab
---
Pay close attention to the indentation of the options inside the YAML header; if you do not do this correctly, pandoc will not correctly understand your specifications. As an example, notice the difference between only specifying the output document to be HTML:
---
output: html_document
---
and specifying an HTML output document with a different theme:
---
output:
html_document:
theme: spacelab
---
We can change the CSS style of your HTML output: you can set the theme option of html_document to one of default, cerulean, journal, flatly, readable, spacelab, united, or cosmo.
But what if you want to customize your CSS in more specific ways? You can do this by writing a .css file for your report and saving it in the same directory as the .Rmd file. To have your report use the CSS, set the css option of html_document to the file name, like this
---
title: "Demo"
output:
html_document:
css: styles.css
---
Shiny is an R package that uses R to build interactive web apps such as data explorers and dashboards. You can add shiny components to an R Markdown file to make an interactive document.
When you do this, you must ensure that
You add runtime: shiny to the file's YAML header
You use an HTML output format (like html_document, ioslides_presentation, or slidy_presentation).
To learn more about interactivity with Shiny and R, visit shiny.rstudio.com.
ou can also use R Markdown to create reports that use interactive ggvis graphics. ggvis relies on the shiny framework to create interactivity, so you will need to prepare your interactive document in the same ways:
You need to add runtime: shiny to the YAML header
You need to ensure that your output is a HTML format (like html_document, ioslides_presentation, or slidy_presentation)
``{r warning = FALSE, error = FALSE}
"four" + "five"
```
Three of the most popular chunk options are echo, eval and 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).
You can embed R code into the text of your document with the `r `
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
```
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.
```{r ref.label='simple_sum', echo = FALSE}
```
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
---
But what if you are using R in a terminal window? You can run the command rmarkdown::render(<file path>) to render any .Rmd file with R.
You can also export your file as a slideshow by changing the output field to:
---
output: beamer_presentation
---
which creates a beamer pdf slideshow,
---
output: ioslides_presentation
---
which creates an ioslides HTML slideshow or
---
output: slidy_presentation
---
which creates a slidy HTML slideshow.
R Markdown will start a new slide at each first or second level header in your document. You can insert additional slide breaks with Markdown's horizontal rule syntax:
***
Everywhere you add these three asteriks in your text, pandoc will create a new slide.
Each R Markdown output template is a collection of knitr and pandoc options. You can customize your output by overwriting the default options that come with the template.
For example, the YAML header below overwrites the default code highlight style of the pdf_document template to create a document that uses the zenburn style:
---
title: "Demo"
output:
pdf_document:
highlight: zenburn
---
The YAML header below overwrites the default bootstrap CSS theme of the html_document template.
---
title: "Demo"
output:
html_document:
theme: spacelab
---
Pay close attention to the indentation of the options inside the YAML header; if you do not do this correctly, pandoc will not correctly understand your specifications. As an example, notice the difference between only specifying the output document to be HTML:
---
output: html_document
---
and specifying an HTML output document with a different theme:
---
output:
html_document:
theme: spacelab
---
We can change the CSS style of your HTML output: you can set the theme option of html_document to one of default, cerulean, journal, flatly, readable, spacelab, united, or cosmo.
But what if you want to customize your CSS in more specific ways? You can do this by writing a .css file for your report and saving it in the same directory as the .Rmd file. To have your report use the CSS, set the css option of html_document to the file name, like this
---
title: "Demo"
output:
html_document:
css: styles.css
---
Shiny is an R package that uses R to build interactive web apps such as data explorers and dashboards. You can add shiny components to an R Markdown file to make an interactive document.
When you do this, you must ensure that
You add runtime: shiny to the file's YAML header
You use an HTML output format (like html_document, ioslides_presentation, or slidy_presentation).
To learn more about interactivity with Shiny and R, visit shiny.rstudio.com.
ou can also use R Markdown to create reports that use interactive ggvis graphics. ggvis relies on the shiny framework to create interactivity, so you will need to prepare your interactive document in the same ways:
You need to add runtime: shiny to the YAML header
You need to ensure that your output is a HTML format (like html_document, ioslides_presentation, or slidy_presentation)
Comments
Post a Comment