10.2 Weave R code into LaTeX using knitr - Video Tutorials & Practice Problems
Video duration:
5m
Play a video:
<v Voiceover>Continuing</v> with our LaTeX document it's time to start interweaving R code to make things very reproducible. In the past you might have written some R code, gotten the results, and copied and pasted it into a word document. Well, what happens two weeks later when your data changed and you have to do it all over again and copy and past it again? Using LaTeX you can just interweave it in and you hit the run button. All the results are updated. So let's create a new section here called diamonds. And we'll take a look at our diamonds data. Let's make sure we give it a label. Call it section diamonds. You don't need a label, but it's good practice. So in here we want to put a chunk of R code. The way you delineate R code is you have two less than signs. Some sort of label. We'll say diamonds-model as our label. And then you can have options, but you don't need them and you put two greater than signs and an equal sign. All the R code goes in between this opening token and this @ symbol. That's the basics of a chunk. You need these basics. Now once you're in here it's just like running R. Even in RStudio you can see it highlights it. And now we're in R land again. So we say load ggplot. And we do require ggplot2. Then lets load in the diamonds. And view them. Now lets fit a model. We'll do that by saying mod1 <- lm price ~ carrot + cut and it's coming from diamonds. And let's view the model. So, assuming all our code runs perfectly, we can run this and see the results in the PDF document. And here have in the diamond section we see R code, followed by the results of R code, more code, more results. All that integrated perfectly into a PDF document. Some things to note here. By default knitr does not give the little prompt symbol in the code. And for results it comments them out. This is actually a really nice touch. That way if I were to come in here, copy and paste all this, I could copy it and paste it into R and it'll run just fine assuming I've already loaded ggplot. So now I can paste all that code and it works. Normally if the results weren't commented out you get all sorts of errors. Now, of course I just dumped far too much into the console so it's locking up a bit, otherwise it be running just fine. So, now that we've shown how to fit a model lets make another chunk to show a plot. So again the chunk is two <<. We'll call this diamonds plot. And we'll say in here to fig.cap=scatterplot of diamonds. And we will say out.width= .95\\linewidth. That means make the image .95 of the page. There's lots of other options we can add in here. But let's just do this for now. We will say ggplot diamonds aes x=carat y=price color=color + geom_point. So, we ran this and it failed. So now it's up to us to figure out why. Which isn't always so easy using LaTeX. So going through the document we're trying to find what went wrong. And it turns out a common problem with LaTeX is specifying the type of image file. Errors are a common thing in LaTeX, happen all the time. And so we have to get used to debugging them. Up here we declare the graphics extensions to be png and jpg. But without specifying a dev for the image it automatically generated a PDF not a png. So we can specify dev=png and a png will be generated and that should solve our problem. And here we go. Here's our document. Here's our output from the model. And here is a graphic with a figure caption and everything. And one step further. We can come right up here and say the diamonds are plotted in figure ref fig:diamonds-plot. It's using the chunk label as part of reference label. So if we run this... Come down here... We can see that diamonds are plotted in Figure 1. This is Figure 1 it automatically numbers it. Let's say you have two figures and you reference those two figures as Figure 1 and Figure 2. Then all of the sudden you insert a figure in between them. LaTeX is smart and it'll change your reference from Figure 2 to Figure 3 automatically. That is such a great thing and makes life so much easier. So this is just a very quick intro to LaTeX and knitr. For more details, especially about all the options avaible in knitr, check out the chapter in the book called Using knitr with LaTeX where it explains many more of the options.