At some point during your academic and/or professional careers you will be required to write. For many this means using a word processor, but what if your document contained code, plots and tables of numbers? Simple right? That’s what Copy&Paste was invented for - imagine then that after getting the formatting perfect, the figures and tables in the right sections you find errors in the code and all the plots and tables need updating.
This is the reproducability problem of the above method and something that can be avoided by using RStudio and RMarkdown to write. For an interesting explanation of this see A reproducible workflow by Ignasi Bartomeus and Francisco Rodríguez-Sánchez.
This Vignette will help you write your own in a couple of ways:
First open RStudio and navigate to File > New File > R Markdown - see Figure 1
Figure 1 - New File
Name your document and add your name to the document - see Figure 2
Figure 2 - R Markdown File
Then select an appropriate template, the Vignette template is used here - see Figure 3
Figure 3 - Vignette Template
Your screen should now show the blank template - see Figure 4
Figure 4 - RStudio Console
Now you should have a brand-new template to fill in.
Now that you have your template open you may wish to employ some of these features;
Please Note Keystrokes for non-printing keys appear within round parenthesis.
Here are a few simple ways to format text in the document
Headings are easy to create in RMarkdown, below are some examples of the 6 differnet heading sizes and the syntax for creating them
#(space)Heading 1 Sample
##(space)Heading 2 Sample
###(space)Heading 3 Sample
####(space)Heading 4 Sample
#####(space)Heading 5 Sample
######(space) Heading 6 Sample
Ordered and Unordered lists are typed normally as you would see them the lists below show how this is done
Text formatting in bold is done by typing between two sets of double asterisks ‘**YourBoldText**’ and italics is done by typing between two underscores ‘_YourItalicText_’.
There are many ways to add hyperlinks in R Markdown, I like this one because it’s simple:
[YourDisplayText](<YourWebAddress>)
A quote is added by using > this code produces the below quote with hyperlink:
’ >“Documentation is a love letter that you write to your future self.” ~ Damian Conway [AZ Quotes](<https://www.azquotes.com/quotes/topics/documentation.html>);’
“Documentation is a love letter that you write to your future self.” ~ Damian Conway (AZ Quotes)
You can write mathematic expressions: \(2\infty + >\) like this ‘$2\infty + > $’
Footnotes1 like this ‘footnotes^[This footer will be seen at the bottom of this document.]’, and tables using knitr::kable().
There are two styles that code can be presented; code chunks and inline code
This is what code chunk looks like, it has been customised so that you can easily put them side-by-side (Figure 5).
nycflights13::flights
#> # A tibble: 336,776 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 517 515 2 830 819
#> 2 2013 1 1 533 529 4 850 830
#> 3 2013 1 1 542 540 2 923 850
#> 4 2013 1 1 544 545 -1 1004 1022
#> 5 2013 1 1 554 600 -6 812 837
#> 6 2013 1 1 554 558 -4 740 728
#> 7 2013 1 1 555 600 -5 913 854
#> 8 2013 1 1 557 600 -3 709 723
#> 9 2013 1 1 557 600 -3 838 846
#> 10 2013 1 1 558 600 -2 753 745
#> # ... with 336,766 more rows, and 11 more variables: arr_delay <dbl>,
#> # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
#> # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
all_flights <- flights
not_cancelled <- flights %>%
filter(!is.na(dep_delay), !is.na(arr_delay))
delays <- not_cancelled %>%
group_by(tailnum) %>%
summarise(
delay = mean(arr_delay, na.rm = TRUE),
n = n()
)
delays %>%
ggplot(aes(x = n, y = delay)) +
geom_point(alpha = 1/10)
delays %>%
filter(n>25) %>%
ggplot(aes(x= n, y = delay)) +
geom_point(alpha = 1/10)Figure 5 - side-by-side plots
Inline R code can be achieved by typing between single quotes and look like this when knitted: fig.cap = "Your figure caption." in knitr.
You may wish to add images or embedded videos to your document
This is code that adds and resizes an image from a file saved on a computer desktop, just replace the file path and file name with images you wish to use. Pro Tip .png are the best images for use in RMD for image sharpness.
{width=80%}
And here’s an example of an embedded video using HTML iframes in Figure 6
<iframe src=“YourWebAddress” width=“560” height=“315” frameborder=“0” allowfullscreen> </iframe>
Figure 6 - embedded youtube video
After you’ve finished writing and formatting there’s only one thing left to do… Knit (Figure 7)
Figure 7 - Knit image
Just some things I learnt:
Helpful resources I used to put this together - read and enjoy!
This footer will be at the bottom of this document.↩