Building apps

Building Web Applications with Shiny in R

Kaelen Medeiros

Data Scientist

Explore Life Expectation vs. GDP per Capita

An app displaying an interactive scatterplot of life expectation vs. gdp per capita for a selected continent and year

Building Web Applications with Shiny in R

Explore Life Expectation vs. GDP per Capita

An app displaying an interactive table of life expectation, population, and gdp per capita for a selected continent and year

Building Web Applications with Shiny in R

Building Shiny apps: 4 steps

  1. Add inputs (UI)
  2. Add outputs (UI/Server)
  3. Update layout (UI)
  4. Update outputs (Server)
Building Web Applications with Shiny in R

Step 1: Add inputs (UI)

ui <- fluidPage(
  titlePanel("Life Expectation vs. GDP Per Capita"),

selectInput('continent', 'Select Continent', unique(gapminder$continent)),
sliderInput('year', 'Select Year', 1952, 2007, 1990, step = 5) )
server <- function(input, output, session){ } shinyApp(ui = ui, server = server)
Building Web Applications with Shiny in R

An app displaying controls for selecting a continent and year

Building Web Applications with Shiny in R

Step 2: Add outputs (UI)

ui <- fluidPage(
  titlePanel("Life Expectation vs. GDP Per Capita"),
  selectInput('continent', 'Select Continent', unique(gapminder$continent)),
  sliderInput('year', 'Select Year', 1952, 2007, 1990, step = 5),

plotOutput('plot'), DT::DTOutput('table')
)
Building Web Applications with Shiny in R

Step 2: Add outputs (Server)

server <- function(input, output, session){

output$plot <- renderPlot({ ggplot() })
output$table <- DT::renderDT({ gapminder })
}
Building Web Applications with Shiny in R

An app displaying an empty plot and interactive table along with controls for selecting a continent and year

Building Web Applications with Shiny in R

Step 3: Update layout (UI)

ui <- fluidPage(
  titlePanel("Life Expectation vs. GDP Per Capita"),

sidebarLayout(
sidebarPanel( selectInput('continent', 'Select Continent', unique(gapminder$continent)), sliderInput('year', 'Select Year', 1952, 2007, 1990, step = 5) ),
mainPanel( plotOutput('plot'), DT::DTOutput('table') )
) )
Building Web Applications with Shiny in R

Step 3: Update layout (UI)

ui <- fluidPage(
  titlePanel("Life Expectation vs. GDP Per Capita"),

sidebarLayout(
sidebarPanel( selectInput('continent', 'Select Continent', unique(gapminder$continent)), sliderInput('year', 'Select Year', 1952, 2007, 1990, step = 5) ),
mainPanel( tabsetPanel( tabPanel("Plot", plotOutput('plot')), tabPanel("Table", DT::DTOutput('table')) ) )
) )
Building Web Applications with Shiny in R

An app displaying an interactive table of life expectancy, population, and gdp per capita for a selected continent and year

Building Web Applications with Shiny in R

Step 4: Update outputs (Server)

server <- function(input, output, session){
  output$plot <- renderPlot({

data <- gapminder %>% filter(year == input$year) %>% filter(continent == input$continent) print(data) ggplot(data, aes(x = gdpPercap, y = lifeExp)) + geom_point()
}) output$table <- DT::renderDT({
gapminder %>% filter(year == input$year) %>% filter(continent == input$continent) })
}
Building Web Applications with Shiny in R

An app displaying an interactive scatterplot of life expectancy vs. gdp per capita for a selected continent and year, in a tab

Building Web Applications with Shiny in R

Let's practice!

Building Web Applications with Shiny in R

Preparing Video For Download...