NOTE: You should delete all of the text below this line, including this line before submitting - all of the text below is just here as a helpful guide

Settings

Make sure you update the settings in the YAML for your report (the part at the very top between the “—” symbols). Update the title, subtitle, name, date, and even the theme if you wish.

Also note that I’ve added code_folding: hide to the settings - this adds a drop-down menu at the very top so that your code chunks can be hidden or shown on command. It makes for a nice, more polished report.

Template structure

Folders

There are four folders in this template:

  1. data_raw: This is where you should put all of your “raw” data used in your analysis. Put the files there, then NEVER touch them again. Opening the raw file in a program like Microsoft Excel can (and often will) corrupt your data file. If you feel you must open the data file in Excel to look at it, then create a copy of the raw data, open and look at the copy, then DELETE IT. This will leave your “raw” data untouched and uncorrupted.

  2. data_processed: If you create any processed or “cleaned” up data frames in R that you want to export, save them in this folder. This can be helpful, for example, if the data processing takes a long time to run. In that case, you might want to create a separate .R file called clean_data.R (or something like that) where you clean and save the data. Then you can just read in the pre-processed data from the data_processed folder.

  3. figs: This is where any chart you create inside a code chunk will be saved.

  4. images: Sometimes you’ll want to include images other than those generated by code in your report - put those here. For example, I put the class sticker called logo.png in this folder.

Files

There are three files:

  1. report.Rproj: Open this whenever working on your project to make sure that you are in the correct working directory.

  2. report.Rmd: (i.e. this file) Write your report in this file. Write code and text together, then compile it to a single html file that you can then share with others to communicate your analysis.

  3. report.html: This is the compiled report. Once you’re done writing your .Rmd file, click the “knit” button in RStudio to create this file.

Writing your report

Use markdown to format your document, e.g. use the # symbol for a level 1 header, the ## symbol for a level 2 header, etc.

Run R code using chunks like this:

head(mtcars)
#>                    mpg cyl disp  hp drat    wt
#> Mazda RX4         21.0   6  160 110 3.90 2.620
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875
#> Datsun 710        22.8   4  108  93 3.85 2.320
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440
#> Valiant           18.1   6  225 105 2.76 3.460
#>                    qsec vs am gear carb
#> Mazda RX4         16.46  0  1    4    4
#> Mazda RX4 Wag     17.02  0  1    4    4
#> Datsun 710        18.61  1  1    4    1
#> Hornet 4 Drive    19.44  1  0    3    1
#> Hornet Sportabout 17.02  0  0    3    2
#> Valiant           20.22  1  0    3    1

Create charts and change the name and dimensions by changing the chunk settings, like this:

ggplot(mtcars) + 
    geom_point(aes(x = mpg, y = hp))

Finally, if you want to insert an image in the images folder, you can insert it using html code like this:

The code <center> and </center> just puts the image in the center of the page, the src="" part defines the path to the image, and the width=300 part defines the image width in pixels - use that to adjust the image size in your report.