Section 1 Markdown syntax
We will begging exploring concise markdown syntax in the first section of the book. You will be amazed how with just few additional text markups and the smart functions from rmarkdown R package we can create a world of different document types.
1.1 Introduction
Let’s begin with install rmarkdown
package using the following command
Once we have the package installed, let’s open a plain text file by either executing the following command
or via help menu at the top left of RStudio
File
└── New File
└── Text File
and then saving that file as learning_rmd.Rmd
by pressing ctrl+s
. Note that .Rmd
extension is important it triggers RStudio to understand that we are working with Rmarkdown file, which results in new button Knit
appearing at the top of the text editor window
And let’s write our first bit of text and press Knit
button at the top of the console pane or use keyboard shortbut ctrl+shift+k
— Excellent work there !
1.2 RStudio tweaks
Let’s do a couple quick RStduio tweaks to help us throughout the day. Use menu bar at the top to navigate to Global Options
which should bring a new dialog up.
Tools
└── Global Options
R General -> (untick) Restore .RData into workspace at startup
R General -> Save workspace to .RData on exit: Never
R Markdown -> Show output preview in: Viewer Pane
1.3 Headers
Let’s start out first journey into marking text up (or down?)
Add a #
hash symbol in front of the short sentence that we wrote and press Knit
again
Now play with different header levels and don’t forget to press Knit
— Excellent, you are getting a hang of it!
1.4 Bullet points
Let’s now add a bit more content to our Rmarkdown document. First of all remove all of the previous text and type out this one and press Knit
or ctrl+shift+k
# Introduction
> outcomes:
- learn how to create and share Rmarkdown document
- learn various document type, e.g html, pdf and docx
- ~~learn to fly the rocket!~~
The bottom line is - I would like to learn Rmarkdown!
<br>
Like **seriously** become **good** at it !
— This is looking good!
1.5 Task list
You should understand by now that by using different number of #
symbols we are creating different section in our document, so let’s create another section and Knit
1.6 Code and Math blocks
Both mathematical equations and code chunks can be embedded inline or standalone. Let’s first add new, code section to our document and include a short code chunk
1.6.1 Code chunk
## Code
You can use single backtick on each side of the code for inline or triple backticks for standalone code block
```
#!/bin/bash
echo "Why am I doing BASH in this course?"
Rscript --vanilla run_away.R
```
One useful addition to the code block syntax (triple backticks) is ability to specify language for highlighting purposes, which realy helps with readability. The code about is BASH
let’s add that to our code block ```BASH
and Knit
again.
It doesn’t matter if you use lower case bash
or upper case BASH
word, but for other languages it may matter, you should double check.
We will get to this a bit later in the book, but a little spoiler, if you don’t mark your code chunk with any language name, knitr
won’t be able to recognise that chunk as code and your Rmarkdown document will miss out on some useful features e.g code_folding
, but let’s talk more about shortly
1.6.2 Math chunk
Let’s add another section and include a one math equation into our document.
1.7 Images
We add images the same way we add any other hyperlink using the following syntax, except we also add !
symbol at the front to emphasize that this is an image and not a text hyperlink
[text](link)
where “link” is a path to the file, can be on the internet i.e URL or local path on your system
Let’s add Rmarkdown Hex sticker image to our document under another section “Images” and press Knit
Everything had worked well and we can see the image, except the image is pretty much the only thing we can see now. Let’s adjust image size with the following addition. We are resizing the image to 50 % of the original size.
1.8 Final look
Just in case you’ve got lost, this is how our first section should look like
# Introduction
> outcome:
- learn how to create and share Rmarkdown document
- learn various document type, e.g html, pdf and docx
- ~~learn to fly the rocket!~~
The bottom line is - I would like to learn Rmarkdown!
<br>
Like **seriously** become **good** at it !
## Task list for today
- [X] Get aspired to learn Rmarkdown
- [ ] Get to know three core components of the Rmarkdown document (header, body, R chunks)
- [ ] Practice, practice !
## Code
You can use single backtick on each side of the code for inline code or triple backticks for standalone code block
```bash
#!/bin/bash
echo "Why am I doing BASH in this course?"
Rscript --vanilla run_away.R
```
## Math
This is a covariance equation, random variable $X$ co-variace with random variable $Y$, where $\bar{X} = mean(X)$ and $\bar{Y} = mean(Y)$ and $N$ is a size of a random sample from the population.
$$cov(X,Y) = \frac{\sum(X_{i}-\bar{X})(Y_{i}-\bar{Y})}{N-1}$$
[for more LaxTex syntax here](https://en.wikibooks.org/wiki/LaTeX/Mathematics)
## Images
![](https://bookdown.org/yihui/rmarkdown/images/hex-rmarkdown.png){width=50%}