From the course: Creating Interactive Presentations with Shiny and R

Add code and charts

From the course: Creating Interactive Presentations with Shiny and R

Start my 1-month free trial

Add code and charts

- [Voiceover] So let's add code to our R Markdown presentation using code chunks and use that code to insert a chart into our presentation. So, code chunks are inserted by pressing the Insert Code Chunk button, or with the keyboard shortcut Cmd+Shift+I. It's important to provide names for your code chunks. This is because at some point you will have an error in your code, and a name for your code chunk allows you to easily see where the error is. It's also possible to embed code in text with backtick r, your R expression to be evaluated, and then backtick. So, let's switch to RStudio. So, as we're doing something new in RStudio, we'll create a project by going to Project in top right-hand corner, New Project. We'll create a new directory, an empty project, we'll add it to the desktop, and we'll call it 02_03_codechunks. And we'll create the project. And now, we'll add a template R Markdown presentation to this by going to File, New File, R Markdown, and we'll choose Presentation, my favorite format, Slidy. And now underneath the slide heading, R Markdown, we're going to insert a code chunk. I could press the Insert Code Chunk button here, or I could use the keyboard shortcut cmd + shift + i. So here we see my code chunk. Code chunks begin with ```{r}. Now, after the r, which tells R Markdown that this is the language that we're going to be writing here, we can name the code chunk. So we're going to call this curve_plot, as I'm going to add to this a plot of a curve. Now, everything between these three backticks is going to be interpreted as R code. So, I'm going to use the function curve, and I'm going to visualize x to the power of two from minus five to five. And then I'm going to press Knit HTML in order to knit my presentation together into an HTML document. Master save the file. I'll save it into the project directory, and I'll press Save. Now, I'm getting a parse error. And I'm being told that a parse error is on Line 13. But if I go to the output here of the R Markdown section of the RStudio interface, so we can see the error is in the curve plot code chunk. I know where the error is. I've just missed a comma here, as R is a comma-delimited language. So, this is how you insert code into your presentation. But how about if you want to include the output of code into your text? Well, in order to do that, you need to use single backticks. So, let's come up with a little bit of an example here. So, it's a bit of a silly example, but let's say, "The result of 2+2" is something I want to insert into my presentation, I do that by typing backtick, r, my R expression to be evaluated, and close my expression with another backtick. And now, if I knit together my presentation, and make these side by side, and again I don't need my Environment right now, I can go to my presentation, and if I go to the second slide, we can see my inserted chart, my parabola, and I can also see my interpreted R code here. So the result of two plus two is indeed four. So that's how you insert code into your presentations with code chunks and within text using backtick.

Contents