One way to make your analyses reproducible is to use R markdown documents. R markdown documents are an example of a literate programming language that can be used to interleave code, text, plots, and math, in a way that you can distribute them easily to other people. So I'm just going to go through this R markdown document that's in the course R package. So I'm going to start up here at the top. So this YAML, or this is the front banner our markdown document. And so you can define, for example, the title of the document, the author of the document, and what output format you want it to be, whether it's an HTML document or you want it to be a presentation or a PDF file. If you look at the help files for our markdown, you can output it in a variety of different formats. I've actually added some extra information here that, it needs to be there because we have an R package that this is part of. But you don't necessarily have to do that for every R markdown document you used. Now I'm going to skip this next section, this is all for sort of setting up the code so that it looks right for the document to look pretty. And I'm going to go into sort of the different components of this. So, I'm going to start by talking about compiling documents. So you can compile this document here by actually clicking on this knit button. So if I click on knit, what will happen is, our studio will run a knitter on this and will create an R markdown document. Which then looks like this, so here, it's formatted very nicely, and you can see it's an HTML document which I can then share with other people. And so a couple of other things I wanted to talk about with this document. One is, if I change the output format up here, if I go up here and I change this to a PDF document and recompile this, then I actually get a different output. So now I'm going to get a PDF file as an output, instead of before I had an HTML file, so if I compile it like that, now I have this PDF that I can distribute. You can do the same thing with Word or like I said a number of other different output formats. And so that is the first thing is that you can compile it to different types. The next thing is that you can name the code chunk. So here I have a code chunk. Code chunk is just basically some R code that I have invented in the text, and you can do that. You can put a new code chunk in by doing chunks up here. Insert chunk and it puts in like a new chunk of code right there. I'm going to delete that so you can just see here that I have a chunk and after the R part here I just type chunk1 and so that's the name of that chunk. So when I compile the document, I you know click knit then what it's going to do is a lot of text happening up here in the R mark down window. And one of the things it does is it tells you what the chucks are called. And so as its going through one of those chunks the labels was chuck1. And so if you label all of your chucks then you'll be able to go through and figure out which part of your code broke if it did. You can define different levels of headers. So if I can pile this document any two hashtags will be a secondary header, a primary header is 1, 3 is a tertiary header. And so you can see what those look like when I compile the document. You get primary header is bigger, then secondary is smaller and tertiary header is a little bit smaller than that. You can then create bulleted lists or numbered lists by using either just asterisks or by using a numbered list like this. And so that formats nicely into a formatted list here. You can also create figures in the document. And so here I've just included another R code chunk, and I have typed x = rnorm(100), and then I just plotted that object. And so actually, if you look here, you actually get the R output, including the plot. And so a couple of arguments that you might want to know about, there are fig.align=center. We'll center the figures. If you take that off though, it'll all be sort of left justified. You can also do other things with R code chunks. You can set echo equal to false. We'll basically just not output that particular set of code, so here if I say echo equals false you can see the code isn't there but the plot does appear. And so you can hide code from people. If you want to just share a document without the code but without all the writing you could do that. You can also set the cache. So if I un comment this particular piece of code here it tells the document, or the code says to sleep for ten seconds. So if I hit knit we're going to have to wait a bit because it's going to wait here at this sort of long time chunk for ten seconds. So while it's waiting there what this is doing if you say cache equals true. Then it will store that as the values that you ran it with and so, if I run it the first time it's again going to be really slow the first time that I run it. But then after the second time that I run it, it will have stored the files for that document and so It will go a lot faster when you recompile this. This is particularly useful if you're going to be compiling documents that have long running pieces of code in them. But you have to be a little careful because you're saving information there. And then at the end of an R markdown document I like to always put the session information. So that's basically what programming language you're using, what the package is and the versions of the packages that you're using. And then what platform you're computing on. And so, this information is really useful for when you're asking for help, to have it all printed out like this. I usually use the session info information from the devtools package. I like the formatting of it a little bit better. And so then I also usually output the date that the document was compiled so, this also shows an example here at the bottom of this document how you can include inline tech, inline code. So if you do one tick here followed by an R followed by a sys.date followed by another tick. It's going to compile all of this as if it was R code and just stick it right there in the document. And so you can see down here, this has the date that the document was processed at put into the document. So that's a little bit of R mark down. If you play around with this R mark down document and the other R mark down documents in the course R package you can get a better feel for how they work.