This file aims to demonstrate the basics of writing up homework solutions in R markdown and output it as an html file. The html file can then be printed and turned in.

Install needed R pacakges

Assume R and R studio have been installed. Open Rstudio, the lower left corner of Rstudio is the Console window, where you can execute R commands.

Now install R packages Markdown and knitr (make sure you have internet connection since the following two commands install these packages from the online respository as follows:

  1. install R package Markdown for formatting syntax:
  1. install R package knitr for compling
  1. set knitr as the compiler in Rstudio

In Rstudio, from the task menu, click on “Tools”, from the drop down menu click “Global options….” and you will get a pop up window; from the left panel of the pop up window, click “Sweave”; on the top of the right panel of the same pop up window, there is a line “Weave Rnw file using:”; click the triangle right after this line, and choose “knitr”.

You have been set to use knitr+Markdown to write up homework solutions. Note you may need to restart Rstudio for these settings to take effect.

Create a Markdown file

Note Markdown files have suffices “.Rmd”. [R markdown cheat sheet]{}.

First, choose a working directory (always a good step). You need to let Rstudio know of your working directory, so that all files for current R session will be stored there. Open Rstudio, In the console, execute command:

# path_to_directory is the directory path of the directory you want to set as working directory

Once you are done editing the markdown file, the generated html file will be stored in this directory.

Secondly, create the markdown file using Rstudio. From the task menu of Rstudio, click “File”, and a drop down menu will appear; position your mouse cursor on “New File” on the menu, a pop up menu will appear; from this pop up menu, select “R Markdown” and you will see a second pop up meanu; proceed, fill in the Title and Author information, and choose HTML as the Defult Output Format. You have justed created a Markdown file. Save it somewhere.

You are ready to write up solutions …

Load data via txt file

All homework datasets as .txt files for version 6 of the text book can be downloaded from: I have also stored the tab delimited version of these files in txt format in: The naming convention for these txt file are: exA-B, where A is the Chapter number and B the exercise number; e.g., ex3-3.TXT means data set for exercise 3 of Chapter 3.

You can load a data set by the command:

NameOfVariable <- read.table("path_to_file/file_name.txt",sep="\t", header=FALSE)
# path_to_file is the web link or directoray path


NameOfVariable <- read.table("path_to_file/file_name.txt",sep="", header=FALSE)
# path_to_file is the web link or directoray path

Remember to use quotes for path_to_file/file_name.txt when loading a file. Please look into the help file for command “read.table”:


R codes in Markdown file

R codes are put into chunks, and each chunk is wrapped by two pairs of triple backticks. If you do not want the R codes in a chunk to be evaluated use {r, eval=FALSE} right after the beginning triple backticks. To see a R codes chunk, you need to open the .Rmd file.

Examples of answering

Question: For the data in Exercise 14 of Chapter 3, find the mean and median of sales amount and create a histogram for it.

Answer: (1) The mean and median of sale amount are 118.7 and 117.0; (2) The histogram is given below.

# load data from tab delimited txt file online and assign data to variable salesData
salesData <- read.table("",sep="\t", header=TRUE)
# in the above, sep="\t" is needed since a tab delimited txt file is being loaded; in addition, header=TRUE is needed since the txt file has column names: 'Year'   'Month' 'Sales'.

class(salesData) # by default salesData is a data.frame
## [1] "data.frame"
# the 3rd column of salesData is Sales; extract it and assign it to  salesAmount
salesAmount = salesData[,3] 
# the above line is the same as 
salesAmount = salesData$Sales

# a summary of salesAmount: giving mean, median, 1st quartile, etc
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    93.0   108.3   117.0   118.7   128.2   159.3
# Now a histogram for salesAmount 

Answer to Exercise 2:

Your answer

Your codes

Compile Rmd file into html file

Near the upper left corner of the window in which you are editing the markdown file, there is a button “Knit HTML”, click on it to generate the html file. Printe the html file …