1 Official R Markdown Guide

The link above is what you should explore to understand R Markdown.

Can replace ‘html_document’ with ‘pdf_document’ in the .Rmd (Rmarkdown) file above manually to generate the output in your preferred format. However, I would strongly suggest using HTML format initially as the setup is likely to reduce math symbol issues if you do not have Latex installed locally.

Also, make sure to change the settings of the document based on your preferences by clicking on the Setting symbol (next to Knit button) -> Output Options -> Include table of contents/Output Format…

2 Title: Major Heading here

This contains the link to R Markdown Cheatsheet. Please read it - it has everything you need to know about R Markdown for our course.

2.1 R Markdown: Sub Heading - Indent 2

This is my first attempt - normal text.

This is my second attempt - bold.

This is my third attempt - italicize.

2.1.1 Eval Function: Sub-sub Heading - Indent 3

  1. eval function evaluates code and includes its results in block.
  • Default is True, thus can be omitted.
  • Make False if code giving errors. R will skip evaluating the code chunk. Useful when there are errors in code but you do not want to delete the chunk and possibly come back later to fix it.
# Clear the workspace
rm(list = ls())   # Clear environment 
gc()              # Clear unused memory
##          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 521312 27.9    1154817 61.7         NA   669291 35.8
## Vcells 959786  7.4    8388608 64.0      16384  1839828 14.1
cat("\f")         # Clear the console
par(mfrow=c(1,1)) # Clear plots.  Can use dev.off() 

Run a chunk with eval=FALSE now, thus no R output printed but commands still printed.

?factorial
factorial(5)
5*4*3*2*1

df<- 5

Note df object not created, as code chunk is skipped/not evaluated.

2.1.2 Include Function: Sub-sub Heading - Indent 3

  1. Command names printed as include=TRUE by default. However, sometimes the output is too long and you may want to omit the output from being printed to avoid making the submission file too long / spanning multiple pages.
  • Default is True - so output will be printed.
  • Make False if you want the results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. However, the reader may find it hard to follow if you use this too much as they will not the the output and will have to guess what is the output.

2.1.2.1 Echo Function: Sub-sub-sub Heading - Indent 4

This is not the most important option.

  1. echo function controls display of code command. Use it if you do not want the commands to be printed but want the results to be printed.
  • Default is TRUE.
  • Make False if you do not want R to show code command (but only the output). Again, echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.
?factorial
factorial(5)
## [1] 120
5*4*3*2*1
## [1] 120
factorial(0)
## [1] 1
?choose
choose(n = 3,k = 3)         # combinations 
## [1] 1
choose(3,0)                 # only 1 way to make this selection
## [1] 1

Run the same chunk above with echo=False. Make sure to change the r code chunk name if you simply copy-pasted the code chunk. We will run the factorial(5) command below.

## [1] 120

2.1.3 Graphs: Sub-sub Heading - Indent 3

Can print graphs in your file too.

plot(iris[1:4], col=iris$Species)

2.2 Latex: Sub Heading - Indent 2

Put the math symbols,or equations within $ math symbol here $ signs to generate -

  • \(X_1\) with $X_{1}$

  • \(X_{12}\)

  • \(\pi\) with $\pi$. See all Greek Symbols.

  • \(Y^{23}+X_{19}\) with $Y^{23}+X_{19}$

  • \(\frac{N}{D}\) with $\frac{N}{D}$

  • \(Prob(AB)=\frac{N}{D}\)

  • \(\bar x\) with $\bar x$

  • \(\hat y\) with $\hat y$

If the math Latex math symbols are not working/giving your errors even with HTML format, it could be due to you not being connected online. You could simply install Latex locally to avoid this issue.

2.3 Conclusion

Again, please read the R markdown cheat sheet for more features.

Use the Outline view, and Code Chunk view to navigate your document more effortlessly.

Do note that # have the same regular comment meaning in a code chunk, but a heading/subheading implication outside R code chunks in the .Rmd documents.