R Markdown Quick Tutorial (Part II)

2 minute read

In the previous post, I have demonstrated five simple steps to construct your own R Markdown document. In this post, I am going to share some useful tips for adding mathematical expressions, inline graphs or tables to represent your results with clarity.

1. Inline Code

To add explanations to our R Markdown, we sometimes need to refer to the objects we created in the R Studio environment. Therefore, just simply insert its name with ` `` `. The results will appear as text without code.

For example,

According to the Table.1, the overall five-year survival rate for colon cancer patients is ` colon_five_year `

Results:

According to the Table.1, the overall five-year survival rate for colon cancer patients is 0.63

2. Mathematical expressions

Typing mathematical expressions in R markdown is the same in LATEX. The following table lists some frequently used symbols or mathematical operators.

Input Output Input Output
\langle formula \rangle formula
\lbrace formula \rbrace formula
\sum formula \lim formula
\alpha formula \beta formula
\gamma formula \delta formula
\lambda formula \mu formula

For complete lists of mathematical expressions in LATEX format, Please visit the The Comprehensive LaTeX Symbol List.

Moreover, there are two writing modes for mathematical expressions in R markdown: the inline mode and the display mode. The first one is to add formulas that exist as a part of the text. The second one is to write expressions that are put on separate independent lines.

  • Let’s see an example of the inline mode:

    The formula for the mean is $ \overline{X} = \frac{\sum{X}}{n} $ where x represents each of the values in the data set.

Result: inline

  • The example of displayed mode:

The formula for the mean is \(\overline{X} = \frac{\sum{X}}{n}\) where x represents each of the values in the data set.

Result:
displayed

3. Graphs

We have mentioned a lot of useful syntax in Markdown in my first post. The syntax is nearly the same in creating images in R Markdown & Markdown

For example:

![ Pythagora’s theorem]( https://cdn.zmescience.com/wp-content/uploads/2017/05/Pythagorean-Theorem.jpeg)

Result:

4. Table

We use |, -, and enter to create table with Markdown.

  1. The first row has to be the “header row”, which determines the number of columns in this table.
  2. The second row must contain - separating pipes |.
  3. The pipes | separate each column.

For example:

| header 1 | header 2 |
|---|---|
|row 1 col 1 | row 1 col 2|
|row 2 col 1 | row 2 col 2|

Result:

header 1 header 2
row 1 col 1 row 1 col 2
row 2 col 1 row 2 col 2

Reference

  1. The most beautiful and important mathematical equations
  2. Mathematical Expressions

Written in January 2nd, 2021

Updated: