knitr::opts_chunk$set(echo = TRUE)
This lab will show you how to write an R Markdown document.
In this lab, you will learn:
You will be completing each lab and writing assignment (WA) by writing your code and notes in the corresponding R Markdown document.
For example, for lab 1, open the answer template Rmd file called Lab_1.rmd in your project’s directory for lab 1. Use it to add your code and notes for the lab. For WA 1, open the answer template Rmd file called WA_1.rmd in the same folder in your directory.
Markdown is a way of adding to a plain text document formatting, such as headers, lists, emphasis, quotes, links to URLs and images, and more.
Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to HTML.
R Markdown allows you to write HTML, PDF, and/or MS Word documents, a presentation, or even a book by just using Markdown syntax and embedded programming code.
R Markdown files have extension .Rmd
The “R” in R Markdown stands for the R statistical computing language.
R Markdown files contain three important types of content:
We will cover these contents in this lab.
To create an Rmd file in Cocalc. com, go to the directory (location) in your project where you want the file to be. Then:
Figure: Creating Rmd files
Create an Rmd file named “My_File” in the same directory where this lab is located.
Packages are collections of R functions, data, and some compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages. Others packages are available for download and must be installed.
In Cocalc, many packages are already installed, so there is no need to install them.
Once a package is installed, they must be loaded into the session to be used.
To load a package in R, we invoke the command
library(package) where in ‘’package’’ will be replaced with
the name of the package you want to load.
Here is an example on how to invoke a package in R Markdown.
library("knitr")
Note: To run any R chunk of code, you must use the 3 backticks followed by R in curly braces and after the code you want to run, close the chunk with another set of 3 backticks. More on this later in this lab.
The knitr package extends the basic markdown syntax to
include chunks of executable R code.
Cocalc already have this package loaded for you. But if you are
running your code, say in R Studio on your own computer, you would need
to install and include the knitr in the library of your
document or script.
Now load the package called dplyr.
Enter your code below:
Note: You will learn how to install packages for R in Cocalc in lab 2 (Stay tuned!!!).
I hope you already noticed some of the formatted text above in this rmd file.
Here some basics formatting of text in Markdown.
Plain code blocks are displayed in a fixed-width font but not evaluated by R.
2*3
This text is displayed verbatim
You will see this throughout this rmd file to display examples of codes that are not evaluated.
You can include section headers by using pound signs.
# First-level header
## Second-level header
### Third-level header
*italic* **bold**
_italic_ __bold__
Note that the word Statistics is in italic here and the work markdown is in bold.
Here is an example: Bianca Sosnovski
Write your full name in both italic and bold font.
Enter your answer below:
The code for the list above is:
* Item 1
* Item 2
+ Item 2a
+ Item 2b
The code for the list above is:
1. Item 1
2. Item 2
3. Item 3
+ Item 3a
+ Item 3b
Create a list of textbooks that you will need to buy and/or download for free for your classes this semester.
Enter your code below:
To add hyperlinks, we surround the links with brackets and then provide the link address in parentheses, like this QCC.
[QCC](https://www.qcc.cuny.edu)
As you see at the beginning of this lab, you can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
#cars is a built-in data set in R
Note that the line preceded by # is a comment in the R
code and that you can add comments in the R code chunk with a pound
sign.
Enter a R code chunk that produces as a result the number 6.
Enter your code below:
To add an inline code, use a pair of single backticks and the letter
r. For example, 6 (compare this with the Rmd file). We can also mark
text as inline code by using just the pair of backticks. For example,
code.
You can also embed plots in your rmd files. For example:
You can do a lot of things in a code chunk: produce text output,
tables, or graphics. You have complete control over all these output via
chunk options, which can be provided inside the curly braces (between
{r and }).
Note: the echo = FALSE parameter was
added to the code chunk to prevent printing of the R code that generated
the plot (if applicable).
Create a code chunk that will produce the summary of the dataset
Seatbelts.
Enter your code below:
Create a code chunk that will produce a plot of the dataset
Seatbelts.
Enter your code below:
Of course, you can include math formulas by using dollar signs around the equation in Latex syntax (Using $ symbols around the equation).
The following is the code for writing the equation of the area of a circle in two formats.
$ A= \pi r^2 $
$$ A= \pi r^2 $$
Write the equation of the circumference of a circle.
Enter your code below:
Parts of the material used in this document are adaptations or excerpts from the following:
Grolemund, Garrett. “Introduction to R Markdown.” July 16, 2014. https://rmarkdown.rstudio.com/articles_intro.html
“Markdown Basics.” https://rmarkdown.rstudio.com/authoring_basics.html
Comments
Note that you can add comments in Markdown documents as follows:
<!-- your comment -->. The comments will not be displayed in any output format.Compare this line here in the rmd file (on the left in Cocalc.com) and the output format in HTML on the right (Note that the comment below is missing!!.