Chapter 1. Your First Shiny App
Introduction
In this chapter, we’ll create a simple Shiny app. I’ll start by showing you the minimum boilerplate needed for a Shiny app, and then you’ll learn how to start and stop it. Next you’ll learn the two key components of every Shiny app: the UI (short for user interface), which defines how your app looks, and the server function, which defines how your app works. Shiny uses reactive programming to automatically update outputs when inputs change, so we’ll finish off the chapter by learning the third important component of Shiny apps: reactive expressions.
If you haven’t already installed Shiny, install it now with:
install.packages("shiny")
If you’ve already installed Shiny, use packageVersion("shiny") to check that you have version 1.5.0 or greater.
Then load in your current R session:
library(shiny)
Create App Directory and File
There are several ways to create a Shiny app. The simplest is to create a new directory for your app and put a single file called app.R in it. This app.R file will be used to tell Shiny both how your app should look and how it should behave.
Try it out by creating a new directory and adding an app.R file that looks like this:
library(shiny)ui<-fluidPage("Hello, world!")server<-function(input,output,session){}shinyApp(ui,server)
This is a complete, if trivial, Shiny app! Looking closely at the preceding code, our app.R does four things:
-
It calls
library(shiny)to load the shiny package. -
It defines ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access