R Markdown
R Markdown
is a useful tool for creating documents, reports, presentations, dashboards and even websites. In this chapter, we will cover basics of R Markdown for creating documents including your R codes and results. After reading this chapter, the readers will be able to:
write R codes and show the results in a R Markdown file.
knit
a R Markdown script into apdf
file.
2.4 Basics of R Markdown
2.4.1 Creating a new R Markdown file
To crate a new R Markdown file, click on the New File
icon and select R Marmdown...
.
Let’s select PDF
in the Default Output Format:
section and click OK
.
On the top shows the title, author, date, and the output format. When we knit
this R Markdown file, it will create a pdf document.
2.4.2 Knitting a document
In order to knit
an R Markdown file, just click on the knit
icon . Or use the keyboard short cut, Shift
+ Ctrl
+ K
. Then the R Studio will create a pdf/html file and save it under the current working directory.
2.4.3 Writing code chunks
Inside a code chunk goes the R codes you want to run and show in your document. You can create a code chunk by Code
-> Insert Chucnk
, or the keyboard short cut, Ctrl
+ Alt
+ I
. Below is an example of a code chunk.
In order to run the R codes inside a code chunk, click on the top right side of your code chunk.
Inside the braces tells us that this is an R code chunk, and the name of this chunk is chunkname
. Naming your code chunk helps you keep track of which part of your R Markdown file yields error when knitting it.
The echo=FASLE
option prevents the code from appearing in your document, but not the results. This option is useful when embedding a figure. If you set echo=TRUE
, the code will appear in the knitted document.
2.4.4 Structure of a document
#
signs indicate headers. You can use up to 6-level headers by increasing the number of #
.
Outside the code chunks, you can write plain text explaining the R codes and results.
In order to list an unordered list, put *
in front of the list.
- Item 1
- Item 2
You can create an ordered list by writing 1.
, 2.
, and so on.
- Item 1
- Item 2