Lectures

Change the overall appearance of your report

In the YAML header, more than just the title and output format can be specified. You can also customize things such as syntax highlighting or the overall appearance by specifying a custom theme.

output:
html_document:
theme: cosmo
highlight: monochrome

Add a table of contents

Another cool feature of RMarkdown reports (whether HTML or PDF) is an automatically generated table of contents (TOC). And with several settings, you can customize your TOC quite a bit: You add a table with toc: true and specify whether it should be floating (= whether it moves along as you scroll) with toc_float. The depth of your TOC is set with toc_depth.

output:
html_document:
theme: cosmo
highlight: monochrome
toc: true
toc_float: false
toc_depth: 4

More YAML hacks

There are many more customizations for RMarkdown reports, and most of
them can be configured via the YAML header. Before you dig deeper into custom stylesheets, let’s enable code folding with code_folding: …. This will allow your readers to completely hide all code chunks or show them.

output:
html_document:
theme: cosmo
highlight: monochrome
toc: true
toc_float: false
toc_depth: 4
number_sections: true
code_folding: hide

Change style attributes of text elements

With CSS, it’s easy to change the appearance of text in your report. In this exercise, you’re going to change the font to a font with serifs, in accordance with the style of your plots. You’re also going to try out a few other CSS selectors in order to change some colors and font sizes in your report. For example, the font of the R code elements is currently a little on the larger side, compared to the surrounding prose. You’ll use CSS to reduce their size. Here, all of your CSS should go inside the <style> tags above the Summary. In the next exercise, you’ll learn how to reference an external CSS file using the YAML header. If you need more help regarding the styling of text, you can refer to the Mozilla Developer reference.

<style>
body, h1, h2, h3, h4 {
font-family: "Bookman", serif;
}
body {
color: #333333;
}
a, a:hover {
color: red;
}
pre {
font-size: 10px;
}
</style>

Reference the style sheet

See the new pane in the exercise interface called styles.css? As mentioned in the previous exercise, you can reference an external CSS file in the YAML header of your RMarkdown document like so:

title: "Test"
output:
html_document:
css: styles.css

Your CSS from before is now contained in styles.css. It’s time to reference styles.css in your YAML header so that the CSS rules are applied to your report.

Beautify a table with kable

You’ve just heard it: There are two ways to beautify a table with thekable package: either directly in code chunks by calling theknitr::kable() function or in the YAML header. Here you will try out the former. my_data_frame %>% knitr::kable()

Example

http://rpubs.com/Evan_Jung/Customized_Report

+ Recent posts