From the course: Creating Reports and Presentations with R Markdown and RStudio

R Markdown, knitting, and generating outputs - RStudio Tutorial

From the course: Creating Reports and Presentations with R Markdown and RStudio

Start my 1-month free trial

R Markdown, knitting, and generating outputs

- [Narrator] One of the things that's important to understand when creating reports and presentations with R Markdown is that behind the scenes there is a very complex underbelly of interdependent tools. These include the R Markdown and Knitr packages but the actual workhorse of generating your documents is called Pandoc. Fortunately, in general, we primarily work with the R Markdown package and occasionally Knitr. For the vast majority of users, there's not a need to work with Pandoc directly. So let's establish the basic workflow of creating something with R Markdown. We're going to use an example from the exercises folder, so let's navigate there now, and we'll select the folder 03_02. There's an Rproj file here that we're going to double-click to open up our project in RStudio. Now we've opened up the project we're ready to get going with R Markdown. If you've never worked with our studio projects before, I highly recommend that you start to. Check out our dedicated chapter on RStudio projects in the library for an overview of the benefits of this workflow. Now let's actually open up our R Markdown file, which is the file with the .rmd extension. These files are plain text, and so can be opened in any text editor, but RStudio is designed to work well with these files. Some folks initially think that R Markdown documents are much more complicated than they are. If you've got experience with HTML, Markdown or tec files you're feel right at home with .rmd files. If not, it doesn't take much experimentation before you'll be confident in what you're writing. So we have our .rmd file where we describe our report, our presentation, in this case a PDF document. How do we actually generate the output? Well RStudio notices that we've opened a file with the extension .rmd and it gives us this handy knit button. If we click that, then our document will be knitted together, and we'll have a PDF document generated. There's a PDF viewer built into RStudio, and here we can see our PDF document with a handy little screenshot of the top of an R Markdown document. You'll notice that if we switch to the files tab in RStudio, we can see our file here, rmarkdown-workflow.pdf, so the output of your .rmd file has the same filename but a different extension. But how about creating a new R Markdown file from scratch? To do that we need to go to the menu bar. We'll select file, new file, and R Markdown. RStudio presents us with a dialogue asking exactly what do we want to make. This is essentially a helpful wizard that sets you up ready to make specific types of output. We'll select the HTML document type, and click OK. And so RStudio has created us a template R Markdown file. I personally think that these templates are a little bit more complicated than strictly necessary. There's quite a lot going on in there, but they might be useful for you. If we try to knit this document together by clicking on the knit button, or, pressing the keyboard shortcut which in Mac is cmd + shift + k and in Windows ctrl + shift + k we get a dialogue telling us that we need to save our file before we can generate our output. So, let's call this html-report and hit save, and now the file has been saved, it's knitted together, and we have a HTML document generated for us. So this is quite a nice looking webpage that we could publish to the web if we wanted to. So that, in a nutshell, is the workflow for working with R Markdown. You have .rmd files which describe your document and you knit them together to get the output.

Contents