Knit early. Knit often.

The following section makes more sense in the code than in the html

Insert an R code chunk by using the insert button above. Shortcut keys Ctrl+Alt+i should create a chunk but if not, this can be set by choosing your own shortcut in Tools/Keyboard Shortcuts Help.

A code chunk looks like this:

You can name a chunk (optional), like so

The next chunk should be familiar; it’s the code to import the beds data. Code has been copied across but code can be written directly into the chunk.

Knit the file

By clicking on the drop down menu beside the Knit button, a document will be generated (note: first time you do this, you’ll be prompted to name your file. Try “test_rmarkdown”). As PDF requires additional installations, we will knit to HTML (web page) for now, which is the default and also better for accessbility.

Text

Markdown formatting

You can play with headers that get smaller

You can play with headers - and smaller

You can play with headers - and smaller

You can play with headers - and smallest

You can make a line make a line break


You can do all the normal italics or italics and bold and superscript and strike-though

Things like block quotes look for quotes
works really well if split over several lines

Other things that are useful to code:

  • lists
  • putting stuff in lists
    • putting stuff in sub lists
    • such as this
  • did I say lists?

You can also do numbered lists

  1. Like this
  2. Fine example of a list
    • and do sub lists in a numbered list
      1. and do sub sub lists
        A. and sub sub sub list

Write some stuff in the middle of your list

  1. and then go back to the list
  2. NOTE: the numbers in the list are not specified, they are dynamic and if you go and edit one out it will adjust them automatically

You can colour in a block to assist with highlighting an area.

  • This is my first conclusion
  • This is my second conclusion

You can add footnotes1

Plotting Mental Health bed availabilty

If the chunk is run (not knitted), the plot will be displayed in line with the code (i.e. below the chunk).

Note the button to the left of “play” will run all the previous chunks above the current one.

You may want to show your code, either in the html reports or presentation slides. As the settings have been set globally you can set any chunk to deviate from the global:

# echo=TRUE means show this code
# eval=FALSE means don't check this code. As it is a chunk that won't run correctly, eval=TRUE would result in an error.

ggplot(beds_ts) +
  geom_line(aes(date, mean_beds)) +
  geom_point(aes(date, mean_beds)) +
# This code hasn't been finished.

This chunk refers to the previous chunk code and shows the code but not the chart. Just note the name of the chunk is made into a string with “” quotes.

ggplot(beds_ts) +
  geom_line(aes(date, mean_beds))+
  geom_point(aes(date, mean_beds))+
  ylim(0, 350)

Acknowledgements

I combined Andrew Jones’s original RMarkdown training report with some of Simon Wellesley-Miller’s code for this RMarkdown example report. I thoroughly recommend running Simon’s more detailed RMarkdown to learn more, as this is just a brief introduction.


  1. This is the footnote from the footnote added way way up above↩︎

LS0tCnRpdGxlOiAiSW50cm9kdWN0aW9uIHRvIFJNYXJrZG93biIKZGF0ZTogImByIGZvcm1hdChTeXMudGltZSgpLCAnJWQgJUIgJVknKWAiCm91dHB1dDoKICBodG1sX2RvY3VtZW50OgogICAgY29kZV9kb3dubG9hZDogeWVzCiAgICB0b2M6IHRydWUKICAgIHRvY19kZXB0aDogNAogICAgdG9jX2Zsb2F0OiB0cnVlCi0tLQoKYGBge3Igc2V0dXAsIGluY2x1ZGUgPSBGQUxTRX0KbGlicmFyeShrbml0cikKbGlicmFyeSh0aWR5dmVyc2UpCgpvcHRzX2NodW5rJHNldCgKICBlY2hvID0gRkFMU0UsCiAgZXZhbCA9IEZBTFNFLAogIG1lc3NhZ2UgPSBGQUxTRSwKICB3YXJuaW5nID0gRkFMU0UsCiAgZmlnLndpZHRoID0gNy4yNTIsCiAgZmlnLmhlaWdodCA9IDQsCiAgZHBpID0gMzAwLAogIGRldi5hcmdzID0gbGlzdCh0eXBlID0gImNhaXJvIikKKQpgYGAKCgojIEtuaXQgZWFybHkuIEtuaXQgb2Z0ZW4uIAoKIyMgVGhlIGZvbGxvd2luZyBzZWN0aW9uIG1ha2VzIG1vcmUgc2Vuc2UgaW4gdGhlIGNvZGUgdGhhbiBpbiB0aGUgaHRtbAoKSW5zZXJ0IGFuIFIgY29kZSBjaHVuayBieSB1c2luZyB0aGUgKippbnNlcnQqKiBidXR0b24gYWJvdmUuIFNob3J0Y3V0IGtleXMgQ3RybCtBbHQraSBzaG91bGQgY3JlYXRlIGEgY2h1bmsgYnV0IGlmIG5vdCwgdGhpcyBjYW4gYmUgc2V0IGJ5IGNob29zaW5nIHlvdXIgb3duIHNob3J0Y3V0IGluIFRvb2xzL0tleWJvYXJkIFNob3J0Y3V0cyBIZWxwLgoKQSBjb2RlIG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