Learning objectives
- Learn basic elements of RMarkdown
- Produce lab reports written in RMarkdown and generated using knitr
Overview
So far you have been uploading your .R file with comments and code. For many making graphs and doing other types of analysis working with .R files is ok and even preferred, since they are only using or sharing the results and not the code. However, a key to reproducibile research is sharing your code and results with other scientists. One popular way to do this is to make a report that includes both using the RMarkdown language and the knitr R package. To install RMarkdown and knitry go to Tools > Install Packages and type the name of the package to install from CRAN repository (e.g. knitr). This will install both knitr and RMarkdown. You only need to do this once unless you are working on the BCRC computers.
RMarkdown is a simple syntax similar to the markup styles used in Wikis. Please take 10 minutes to go through the intro at https://rmarkdown.rstudio.com/. After you install the knitr package you will see a knitting ball in your R console menu in RStudio. There are several important differences between working with .R and Rmd files.
- You can create a new .Rmd file through the menu File > New File > RMarkdown. This generates and example file which you can edit. It is best to make your own template file for the course.
- You need to save your file as .Rmd
- Your R code with have to be bracketed by 3 single quotes.
- You do not need to use hashtags for comments
- You will need to have the files that going to be load in the same working directory as your .Rmd file
- When you knit the file it will automatically generate a new html file in your working directory (This is the file you submit for your lab report).
Lab Reports
Lab Reports will be made using the RMarkdown notation. To make a Lab Report
- Go to File > New File > RMarkdown
- Add a title (e.g. Lab 1) and your name as the author. Select html as the Default Output Format.
- Modify the default file so that the exercise number is preceeded by ###. Cut and paste your answer for the exercise from your .R file into this .Rmd file.
- When you are done Knit the file to generate a .html file (this is saved into the same folder as the .Rmd file).
- You will load this .html file into Moodle.