12.1 Use shiny objects in a markdown document - Video Tutorials & Practice Problems
Video duration:
13m
Play a video:
Video transcript
<v Voiceover>Shiny is a really great way</v> to build interactive web applications just using R. It was built by RStudio and they keep improving it at such an incredibly fast pace. Learning Shiny by itself can be a bit intimidating so, fortunately, they built a way that you can put Shiny components right into a markdown document to make it easier to get started. So, to do that, we start with a blank file and we will save it as an RMD document. We save our file as shiny.rmd. And it asks us if we want to convert it from an R script to a R markdown script, and yes, since there's nothing in there. R markdown files start with a YAML header so you can give pertinent metadata. This YAML header has to be in between successive three dashes, so dash dash dash and, at the bottom, dash dash dash. What's in between is the meta information. Title, colon, Shiny document, author is Jared P. Lander and the date for this is August 5, 2015. The output type is an HTML document, that's how it will be rendered, but since we're using Shiny components, we need to specify runtime is shiny. This way, it will act like a Shiny document where necessary. We're going to start by loading some packages we need such as Knitr and ggplot2, setting some options and loading some data. We will create a code chunk using the keyboard shortcut Control alt I. We're not going to bother naming the chunk, we'll just say "Echo equals false". In the chunk, we say "library knitr library of ggplot2 opts underscore chunk dollar sign set, echo equals true" We want our chunks to be Echo generally and we will suppress warnings and messages. And, lastly, we will load in the diamonds data because we will be using that throughout the document. The first section, we're gonna call "Inputs". Since this is a markdown document, we can make it a header one by adding hashtag and saying "inputs" and we can even give it an ID by putting "hashtag inputs" inside of curly braces. We're gonna make another R code chunk and this one we will call "input panel simple". In here, we will make an input panel which holds various inputs. We will start with just one, we will say, "Input panel" then open parenthesis, we will say "checkbox group input", open parenthesis. First thing you do is you give it an ID, which we will call "check group". Then you give it a label, we call it "checkbox group". Then you need to provide choices for each of the checks. So you say, "choices equals list, choice one equals one, choice two equals two, and choice three equals three" and I will say "selected equals one" so that will be the default when it gets started. If you want to see what this looks like printed, you have to first load the Shiny package, so we will say, "library shiny" and then we will highlight the code and run it. And you can see that it creates HTML for us, mostly divs, some labels, some inputs, some spans. Let's see how that looks when we render the document. It pops up in the RStudio viewer pane or it might pop up in an external window or browser, whatever you choose. The code looks a little ill-formatted, that's because the window isn't wide enough, but the important part is it generated these checkboxes that we can use. The input panel can hold more than one input, so let's create a new code chunk. We will call it "input panel multiple". In here, we will start with input panel and I'm going to close the parenthesis now so we don't get confused later. Then I'm going to create a checkbox group input. I'm gonna make it identical to the one before. Check group is the ID, label equals checkbox group. Choices equals list, choice one equals one, choice two equals two, choice three equals three. Started off by selecting one. Then I'm going to make a radio button. Now, these are our arguments in the input panel, so we need a comma. Here, we can put radio buttons. The first argument is its ID. The next argument is the label. We will call it "radio button". The choices and the selected will be the same, so I'm just going to copy and paste. And, lastly, we will put a single checkbox input. Say checkbox, input, the ID is checkbox, label is choice A, and its starting value is true, meaning it's selected. Now, we're not quite happy with the formatting here, we can highlight all the code and hit control I in RStudio and it rearranges the code a little bit for us to make sure it's formatted properly. We can go ahead and Knit this document by using control shift K or, on a Mac, command shift K. And we see now, this code generates multiple input options. You have the single checkbox, the multiple checkboxes, and the radio button. Let's go ahead and make some inputs specific to the diamonds data. So let's make this a header two section, we'll call it "diamonds input" and we will give it an identifier. And now let's make an R code chunk with control alt I and we will call it "diamonds dash input". We'll make this an input panel and we will make a checkbox group input. The input ID will be "check group underscore cut". The label is "diamond cut". The choices consist of a list, ideal premium good very good fair and we'll start off with selected equals ideal. You can close that list and we will close the checkgroup input. We could add other inputs, such as the checkbox input and we can just call this "checkbox underscore log underscore y". Its label will be "plot on log scale" and its value will start off equaling false. So we can run this and we see we get an error. This is because you have to be very careful when you initialize a code chunk. The very first argument which is followed by space is the language for the engine, because Knitr can use multiple languages, it can use R, C++, Dan, Julia, even Python, so it's important you have a space in there. Now we can Knit, then we scroll down and we do have these diamond based inputs. Now we've seen how to create inputs, let's use them. I want to print the values represented in the check group cut and in check box log y. Let's make a new chunk and we will call it "check inputs". In here, I want to print out the value of check group cut. Check group cut is a Shiny input object, so we need to run render print. This'll print out the special type of object, and check group cut is stored as a slot in the input variable, so we do "input dollar check group underscore cut". Likewise, we render print of input, dollar, checkbox underscore log underscore y, so if we Knit this, you can scroll down and see that checkgroup cut is selected as ideal. We check good, it fills in. Very good, that fills in as well. Plot on log scale is currently false, check it, it becomes true. Uncheck, it becomes false. This is really amazing. This is now an HTML document that reacts based on your inputs, so let's go ahead and go a step further and create an interactive plot. We'll make this a header one and call it "interactive plot", and I'll give it an ID called "interactive plot" and we'll make a code chunk. The code chunk will give the label "interactive plot". Now we've used the same header, title, ID, and label, but you don't have to. We are going to build a normal ggplot object, but we wrap it in render plot, so this way it can be interactive, and we also put a curly brace in there in case we have multiple expressions. We say "ggplot of diamonds" and we're going to subset it according to what cuts are selected, so we say "diamonds dollar cut in input dollar check group underscore cut". Leave the column entry blank, then say "aes, x equals carat, y equals price, color equals cut". Close the parenthesis and then "plus geom underscore point" and it might look like the plus is on the next line after we close off the ggplot function, but in reality, that is just RStudio softwrapping a long line of code. If we were following proper code formatting guidelines, we would have put a line break in there automatically but this is just to see how it works. Let's Knit the document, and if we scroll down, perhaps it's better if we open this up in a browser. You can see it's saved as a file on local host. If we scroll down, we see we have this plot and it's only showing ideal diamonds. Let's add in good diamonds and it's updated as necessary and let's add in very good diamonds and it's updated yet again. Let's stick in "fair". And, lastly, let's do "premium" so they are all there. We can also take away levels and this plot fully reacts to the values in the checkboxes. Using Shiny components in a markdown document is not only a great way to learn but it also allows you to quickly generate documents that have interactivity in them, making this a fantastic way to convey your data and your results.