Transcript for:
R Markdown Demo in RStudio

This is just going to be a quick demo on how to use R Markdown in RStudio. So you can see I've got RStudio open here. And so I'm just going to start by opening up a new file and writing a little bit of R Markdown.

So you can go up to the menu here. And there are a couple of options here, but we're going to choose an R Markdown file. The first thing you'll see is this menu here.

that'll ask you kind of what your default output format is going to be. So it will have the HTML option pre-selected, and that's usually what you're going to want to go with. If you choose PDF or Microsoft Word, you're going to have to install other outside tools in order to process those formats.

So I'm just going to create a title here. I'll call it My First Worktown Document. You don't have to put the author in, but I will. And then you can just hit OK. So now you've got...

So we've got a Markdown document here in RStudio. And let me just close this window here. And you can see that it pre-populates the document with a little bit of text.

So the first thing we'll do, which is simple enough, is I'll just hit the knit HTML button to see what happens. You can see that it creates this HTML document in its own viewer here. And I move it over here.

You can see that the text is rendered in HTML. There's code here in the gray boxes. you're summarizing the cars data set, which comes with R.

It gives you the output for that code. And since there was a plot made, you can put the plot, the plot will just appear here at the bottom. So you get this nice HTML document that's generated from your code that's written in this markdown document here. Now, this is obviously not going to be the text that we want to present. So the first thing you're probably need to do is to kind of delete what's automatically populated here by default.

So. You can keep the stuff up here in the top in the preamble, but we'll delete everything else after that. Okay, so now that we've gotten all this boilerplate text out of the way, we can start creating our document.

But before we do that, we probably want to save it first just so we don't accidentally lose anything. So let's click the Save button here, and I'll just save it as Markdown. demo.rmd Okay, so the documents say that I can start writing my text. Maybe I'll just say this is my first breakdown document. And so the first thing you often to want to do when you're in an analysis probably load and summarize some data do a little exploratory analysis so let's load data and you want to open a code block with the three back ticks it's going to be an r code block and i'm just going to load the air quality data set that comes with r so you can load the data sets package and then load the air quality data set and just i'm going to summarize it just very simply We can close the code block like that.

So that's our first R code block. And let's see what this looks like when we knit it. So we can click the Knit button here.

And you can see, first of all, that it saves the document and then it runs Knitter on it. You can see I've got my title, I've got my text here, I've got my code block over here, and then this is the summary of the data set. You can see that for each column of the data frame, it produces a little five-number summary. So that's great. You can close this here and continue working.

So maybe let's do a little bit of exploratory analysis. Maybe we want to visualize some of the data. So we can make a pairs plot or something that's called a scatter plot matrix.

So here's a pairs plot of the data. Again, we open the code block with the three back ticks. I can very simply just do pairs on the air quality.

And then we can run that. So notice that I didn't have to do anything special to create a plot. I just called the plotting code.

And then I can knit the document. You can see now I've got the pairs plot here down at the bottom, and it shows the pairwise scatter plots of all the variables in the data set. And then lastly, we might want to fit some sort of regression model to see if we can predict, for example, ozone from, let's say, solar radiation, wind, and temperature.

So let's put that in the document too. So here's, oops, excuse me. Ozone. So we can open the code block one more time. And I'll call this, I'll say, a linear model of ozone on solar radiation, wind, and temperature.

The data comes from this air quality data frame. And then we can summarize the fit to see what the regression coefficients look like. So I can knit the document one more time.

You can see I've got the summary of the data up here. I've got the pairwise scatter plots down here. And then finally... I've got my code for the linear model. I can summarize it down here, and it summarizes the linear model fit down here at the bottom.

And you can see that all these predictors are all highly associated with the outcome ozone, so the model has some predictive value there. So as we mentioned in the lecture on R Markdown, there are other things you can do. For example, you can create an unordered list by just using either asterisks or any sort of delimiter to indicate the kind of bullets of the list. So here's item 1, item 2. You can create an ordered list.

Just by using numbers, excuse me. And so R Markdown is really useful for very quickly and very simply creating documents. And with the addition of the R code, you can nest your written text with code and output that kind of runs. your data analysis so you can have everything in a single document here.

So all of this is really easy to do in RStudio, so I encourage you to open up RStudio, create your first R Markdown document, and put a little text, put a little code in, and take a look at how it works.