Module 12: R Markdown

This week we will be working with R Markdown and exploring some of the functions and format of R Markdown.
Our assignment this week is simply to make an R Markdown file and play around with some of the features in order for us to use R Markdown in the future to present R input and output neatly. 

Let us begin !

First we have to make a .rmd file. This is very simple in RStudio, where all we have to do is create an R markdown file, much like when creating a new script or project in RStudio.
From here we are prompt what we want to name this file, and the desired output type. R Markdown can produce many different document types, this includes HTML,pdf ,word docs and many more.
Here I decided to simply do HTML .

One thing to note about creating R Markdown with RStudio is that they automatically include a little write up about what is R Markdown and some examples of its feature.  I removed this from my file, making sure to keep the setup code. 

 ```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
From here I decided to play around with R Markdown and see how it works. 
Using the R Markdown cheat sheet and R Markdown definite guide as a guide I explored each of the different formatting features available  (https://rmarkdown.rstudio.com/authoring_basics.html)

First I simply had normal text presented. This is simple, all one has to do is simply type out what they want presented with no needed syntax. Adding "#" before will create a big header of any text after the #. 

One can also add equations by using $$ before and after an equation and using LaTeX typesetting can present equations.
For instance when presenting the famous e=mc^2
$$E=mc^2$$
This is presented as the following in our R markdown document:

After playing around with R Markdowns text formatting, we switch focus now to the R code portion. 
In an R markdown file, R code must be presented like so:
```{r}
# R code
```
Note by default comments will also be presented in the end document as well as written code. 
Using argument like features we can manipulate what is presented.
For instance if we want to hide the actual R code and only want the end output we can set it as such:
```{r,echo=FALSE}
# R code
```
There are also many other properties we can manipulate. For instance when I was exploring how packages function with R Markdown. When I load ggplot2, since my R version was not the same as the version ggplot2 was created in I get a warning message in the R markdown document:
## Warning: package 'ggplot2' was built under R version 4.0.4

This can obviously be something one may not want in their document. I was able to remove this error the next time I loaded lattice library with warning=FALSE
```{r,warning=FALSE}
library(lattice)
```

Finally I decided to see how visualization worked with R markdown, I decided to use xyplot since I previously loaded the library. Note that if a library is already loaded in a previous R code line it does not need to be loaded later just like in R console!
I created a data frame and produce a plot. Using echo=FALSE I was able to just display the data frame and the desired plot. 

```{r,echo=FALSE}
students <- c("Tom","Mark","Jen","Alice","Roy")
age <- c(18,20,25,19,22)
gpa <- c(3.0,3.5,3.8,3.4,3.6)
d <-data.frame(students,age,gpa)
d
xyplot(age~gpa)
```

By default all the figures are shifted to the left side. Often in publication figures are presented centered. Upon looking in the R guide I found that we can also manipulate where figures are aligned with fig.align=!

```{r,fig.align='center'}
xyplot(age~gpa)
```

In conclusion 

I'm very impressed with R Markdown. It is such a simple to use tool that allow one to present aspects of their code very easily. According to the markdown website it can also be used with other coding languages such as Python or SQL which may come in handy in the future . Already by starting this blog one of the main difficulties was presenting code in ways that best communicate my code outside of R. Within just the few moments of playing around in R markdown I can already see how easy and powerful markdown solves this issue! Looking forward to using more R Markdown in my future.

-Anthony

Comments

Popular posts from this blog

R Final project package: Introducing muMotif

Module 8 : I/O, string manipulation and plyr package

Module 9 : Visualization