Skip to content

Instantly share code, notes, and snippets.

@riinuots
Created October 17, 2017 14:21
Show Gist options
  • Save riinuots/24d1e334c5f570a84eff9b556439f47a to your computer and use it in GitHub Desktop.
Save riinuots/24d1e334c5f570a84eff9b556439f47a to your computer and use it in GitHub Desktop.
Shiny app basic example
library(shiny)
library(tidyverse)
library(gapminder)
# User Interface
ui <- basicPage(
sliderInput("year", "Select year:", animate = TRUE,
min = 1952, max = 2007, value = 2007,
step = 5,
sep='' # so thousands are not separated with a comma (without this defaults to 1,952 - 2,007)
), #note this comma here - different to our usual R code
plotOutput(outputId = "myplot")
)
# Server
server <- function(input, output) {
output$myplot <- renderPlot({
gapminder %>%
filter(year == input$year) %>%
ggplot(aes(y = lifeExp, x = continent)) +
geom_jitter(aes(size = pop/1000000, fill = lifeExp),
shape = 21, colour = "grey", alpha = 0.8, width = 0.15) +
scale_fill_distiller(palette = "Paired") +
coord_cartesian(ylim = c(30,85)) +
scale_size("Population, millions", range = c(5, 25), breaks = c(10, 50, 100, 250, 1000)) +
theme_bw() +
labs(caption = "Each bubble is a country.")
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment