Build a "Hello, world" Shiny app
1. Build a "Hello, world" Shiny app
I'm Kaelen Medeiros, the other instructor for this course. Now that you know what Shiny is and when to build an app, let's build your first app. We'll make a "Hello, world" app that begins to demonstrate the building blocks of Shiny apps.2. Parts of a Shiny app
As Ramnath explained in the last lesson, an app needs a user interface, or UI, and a server. The magic of the Shiny package is that can create both of those using R code, plus run your app using an additional Shiny function. First, you'll always have to load Shiny using the library function, as with any R package. Creating the UI doesn't require a specialized Shiny function. Most people use fluidPage, which allows for implementation of a blank HTML fluid page layout, organized by rows and columns, for your app. The server is created in R by defining a custom function. There are other courses that teach you how to do that in more detail, but to build a server you just need the basics. Create an object called server, and then assigned to it should be a function with, at minimum, the arguments input and output, though there are optional arguments that help you create more advanced apps. In this course, we'll also pass the session argument to specify the specific session. Finally, run the app using the shinyApp function. You'll have to pass in your UI and server objects, usually just called ui and server, as arguments.3. Hello, world!!!
Though not terribly interesting, here's an example of a "Hello, world" Shiny app. Notice that all that's been added to the basic app skeleton is the character string "Hello, world!!!" in the UI, while the server is still empty. Given that the only addition to the app is in the UI, what do you think might happen? If you guessed that this app will display the "Hello, world!!!" character string, you're right!4. Ask a question (with an input!)
Our "Hello, world" app wasn't really that exciting. What if we could take in a name from the user, and use that name to wish that person "hello", or to ask them a question? Shiny provides a function, textInput, that makes that possible. It allows users to enter text, and takes three arguments: a unique id that will be used to refer to this input, a label that is displayed to the user, and an optional default value, which we haven't used in this app. Shiny actually provides a variety of functions to accept many different kinds of users inputs, which you'll see more of throughout the course. Our full output, the question itself, is built in the server using the renderText function, and is assigned to an output object, output$q. Inside of that, you can use paste to create a longer character string, and if you add input$name, you can access the name added using textInput. Back up in the UI, use the textOutput function to display the output q. Voila, an app that takes and uses one input! As the course progresses, we'll build apps that use more than one input. For the record, I like both cats and dogs, though I have a cat.5. Let's practice!
Now that we're familiar with how to implement a Shiny app in R, let's practice building one.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.