Skip to content

Instantly share code, notes, and snippets.

@vpnagraj
Last active January 25, 2018 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpnagraj/55352bd47dc1a89c6155b7d6379d6584 to your computer and use it in GitHub Desktop.
Save vpnagraj/55352bd47dc1a89c6155b7d6379d6584 to your computer and use it in GitHub Desktop.
files for shiny workshop
# install.packages("babynames")
library(babynames)
# install.packages("tidyverse")
library(tidyverse)
# install.packages("ggplot2")
# install.packages("dplyr")
# let's take a look at the data
babynames %>%
View()
# now see one name
babynames %>%
filter(name == "Ida")
# do all kinds of things with this ...
babynames %>%
group_by(year, sex) %>%
filter(prop > quantile(prop, probs = 0.99))
# but let's compare two names
babynames %>%
filter(name == "Bert" | name == "Ernie") %>%
filter(sex == "M")
# and visualizing it
babynames %>%
filter(name == "Bert" | name == "Ernie") %>%
ggplot(aes(year, prop, group = name)) +
geom_line(aes(col = name))
# the above is spiky ... let's try filtering for sex
babynames %>%
filter(name == "Bert" | name == "Ernie") %>%
filter(sex == "M") %>%
ggplot(aes(year, prop, group = name)) +
geom_line(aes(col = name))
# and the same thing but with a little more detail
babynames %>%
filter(name == "Bert" | name == "Ernie") %>%
filter(sex == "M") %>%
ggplot(aes(year, prop, group = name)) +
geom_line(aes(col = name)) +
xlab("Year") +
ylab("Proportion of Population") +
theme_minimal()
library(shiny)
shinyServer(function(input, output) {
})
library(shiny)
shinyUI(fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
),
mainPanel(
)
)
))
library(tidyverse)
library(babynames)
library(shiny)
shinyServer(function(input, output) {
output$plot <- renderPlot({
babynames %>%
filter(name == input$name1 | name == input$name2) %>%
filter(sex == input$sex) %>%
ggplot(aes(year, prop, group = name)) +
geom_line(aes(col = name)) +
xlab("Year") +
ylab("Proportion of Population") +
theme_minimal()
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("NAMES"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "name1", label = "Name 1"),
textInput(inputId = "name2", label = "Name 2"),
radioButtons(inputId = "sex", label = "Sex", choices = c("M", "F"))
),
mainPanel(
plotOutput("plot")
)
)
))
library(tidyverse)
library(babynames)
library(shiny)
shinyServer(function(input, output) {
output$plot <- renderPlot({
babynames %>%
filter(name == input$name1 | name == input$name2) %>%
filter(sex == input$sex) %>%
ggplot(aes(year, prop, group = name)) +
geom_line(aes(col = name)) +
xlab("Year") +
ylab("Proportion of Population") +
theme_minimal()
})
output$sumname1 <- DT::renderDataTable({
babynames %>%
filter(name == input$name1) %>%
filter(sex == input$sex) %>%
arrange(desc(prop)) %>%
head(5)
})
output$sumname2 <- DT::renderDataTable({
babynames %>%
filter(name == input$name2) %>%
filter(sex == input$sex) %>%
arrange(desc(prop)) %>%
head(5)
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("NAMES"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "name1", label = "Name 1"),
textInput(inputId = "name2", label = "Name 2"),
radioButtons(inputId = "sex", label = "Sex", choices = c("M", "F"))
),
mainPanel(
plotOutput("plot"),
DT::dataTableOutput("sumname1"),
DT::dataTableOutput("sumname2")
)
)
))
library(tidyverse)
library(babynames)
library(shiny)
shinyServer(function(input, output) {
dat <- eventReactive(input$go, {
name1 <-
babynames %>%
filter(name == input$name1) %>%
filter(sex == input$sex)
name2 <-
babynames %>%
filter(name == input$name2) %>%
filter(sex == input$sex)
list(name1,name2)
})
output$plot <- renderPlot({
rbind(dat()[[1]], dat()[[2]]) %>%
ggplot(aes(year, prop, group = name)) +
geom_line(aes(col = name)) +
xlab("Year") +
ylab("Proportion of Population") +
theme_minimal()
})
output$sumname1 <- DT::renderDataTable({
dat()[[1]] %>%
arrange(desc(prop)) %>%
head(5)
})
output$sumname2 <- DT::renderDataTable({
dat()[[2]] %>%
arrange(desc(prop)) %>%
head(5)
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("NAMES"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "name1", label = "Name 1"),
textInput(inputId = "name2", label = "Name 2"),
radioButtons(inputId = "sex", label = "Sex", choices = c("M", "F")),
actionButton("go", "Submit")
),
mainPanel(
plotOutput("plot"),
DT::dataTableOutput("sumname1"),
DT::dataTableOutput("sumname2")
)
)
))
library(shiny)
shinyUI(fluidPage(
titlePanel("NAMES"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "name1", label = "Name 1"),
textInput(inputId = "name2", label = "Name 2"),
radioButtons(inputId = "sex", label = "Sex", choices = c("M", "F")),
actionButton("go", "Submit")
),
mainPanel(
plotOutput("plot")
)
),
fluidRow(
column(4, DT::dataTableOutput("sumname1")),
column(4, DT::dataTableOutput("sumname2"), offset = 2)
)
))
library(shiny)
library(shinythemes)
shinyUI(fluidPage(theme = shinytheme("flatly"),
titlePanel("NAMES"),
sidebarLayout(
sidebarPanel(
textInput(inputId = "name1", label = "Name 1"),
textInput(inputId = "name2", label = "Name 2"),
radioButtons(inputId = "sex", label = "Sex", choices = c("M", "F")),
actionButton("go", "Submit")
),
mainPanel(
plotOutput("plot")
)
),
fluidRow(
column(4, DT::dataTableOutput("sumname1")),
column(4, DT::dataTableOutput("sumname2"), offset = 2)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment