5. Report Writing and Wrap-Up
Creating a markdown file from scratch
So far you have been working with the R notebooks supplied with the course, but for your own analyses you’ll want to start from scratch.
File → New File → R Markdown
- Choose ‘Document’ and the default output type (HTML)
- A new tab is created in RStudio
The header allows you to specify a Page title, author and output type
---
title: "Untitled"
author: "Mark Dunning"
date: "22/12/2016"
output: html_document
---
Text formatting
See Help → Markdown Quick Reference in RStudio:
- Enclose text in * to format in italics
- Enclose text in ** to format in bold
- *** for bold italics
- ` to format like
code
- $ to include equations: \(e =mc^2\)
> quoted text:
To be or not to be
- See Help -> Markdown Quick Reference for more:
- Adding images
- Adding web links
- Tables
Not quite enough for a reproducible document
- Minimally, you should record what version of R, and the packages you used.
- Use the
sessionInfo()
function
- e.g. for the version of R I used to make the slides
sessionInfo()
Defining chunks
- It is not great practice to have one long, continuous R script
- Better to break-up into smaller pieces; ‘chunks’
- You can document each chunk separately
- Easier to catch errors
- The characteristics of each chunk can be modified:
- You might not want to print the R code for each chunk
- …or the output
Chunk options
- It’s a good idea to name each chunk
- Easier to track-down errors
- We can display R code, but not run it
- We can run R code, but not display it
echo=FALSE
- e.g. setting display options
- Suppress warning messages
Chunk options: eval
- Sometimes we want to format code for display, but not execute; we want to show the code for how we read our data, but want our report to compile quickly
e.g.
data <- read.delim("path.to.my.file")
Chunk options: echo
- Might want to load some data, silently, from disk
- e.g. the R object from reading the data in the previous slide
- or your P.I. wants to see your results, but doesn’t really want to know about the R code that you used
Chunk options: results
- Some code or functions might produce lots of output to the screen that we don’t need
Chunk options: message and warning
- Loading an R package will sometimes print messages and / or warnings to the screen
- This is not always helpful in a report:
- Using
message=FALSE
and warning=FALSE
library(DESeq)
- Could also need
suppressPackageStartupMessages
Chunk options: cache
- The argument
cache=TRUE
will stop certain chunks from being evaluate if their code does not change
- It speeds-up the compilation of the document
- we don’t want to reload our dataset if we’ve only made a tiny change downstream
Running R code from the main text
- We can add R code to our main text, which gets evaluated
- make sure we always have the latest figures, p-values etc
- See example
gene-expression-analysis.Rmd
End of Course
Wrap-up
- Thanks for your attention
- Practice, practice, practice
- Need inspiration? R code is freely-available, so read other people’s code!
- Please fill in the feedback form for us to improve the course
LS0tCnRpdGxlOiAiSW50cm9kdWN0aW9uIHRvIFNvbHZpbmcgQmlvbG9naWNhbCBQcm9ibGVtcyBVc2luZyBSIC0gRGF5IDIiCmF1dGhvcjogTWFyayBEdW5uaW5nLCBTdXJhaiBNZW5vbiBhbmQgQWlvcmEgWmFiYWxhLiBPcmlnaW5hbCBtYXRlcmlhbCBieSBSb2JlcnQgU3Rvam5pxIcsCiAgTGF1cmVudCBHYXR0bywgUm9iIEZveSwgSm9obiBEYXZleSwgRMOhdmlkIE1vbG7DoXIgYW5kIElhbiBSb2JlcnRzCmRhdGU6ICdgciBmb3JtYXQoU3lzLnRpbWUoKSwgIkxhc3QgbW9kaWZpZWQ6ICVkICViICVZIilgJwpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgojIDUuIFJlcG9ydCBXcml0aW5nIGFuZCBXcmFwLVVwCgoKIyMgQ3JlYXRpbmcgYSBtYXJrZG93biBmaWxlIGZyb20gc2NyYXRjaAoKU28gZmFyIHlvdSBoYXZlIGJlZW4gd29ya2luZyB3aXRoIHRoZSBSIG5vdGVib29rcyBzdXBwbGllZCB3aXRoIHRoZSBjb3Vyc2UsIGJ1dCBmb3IgeW91ciBvd24gYW5hbHlzZXMgeW91J2xsIHdhbnQgdG8gc3RhcnQgZnJvbSBzY3JhdGNoLgoKKioqRmlsZSDihpIgTmV3IEZpbGUg4oaSIFIgTWFya2Rvd24qKioKCi0gQ2hvb3NlICdEb2N1bWVudCcgYW5kIHRoZSBkZWZhdWx0IG91dHB1dCB0eXBlIChIVE1MKQotIEEgbmV3IHRhYiBpcyBjcmVhdGVkIGluIFJTdHVkaW8KLSBUaGUgaGVhZGVyIGFsbG93cyB5b3UgdG8gc3BlY2lmeSBhIFBhZ2UgdGl0bGUsIGF1dGhvciBhbmQgb3V0cHV0IHR5cGUKYGBgCi0t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