Last active
August 20, 2019 01:28
-
-
Save jnolis/79bb88a0424322d8cd867ad5710c31dc to your computer and use it in GitHub Desktop.
Code for the my-classroom Shiny app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(dplyr) | |
library(ggplot2) | |
library(babynames) | |
size <- 25 | |
colors <- c("F" = "#DE94AF", "M" = "#6C939F", "X" = "#D4C62A") | |
ui <- fluidPage( | |
titlePanel("My classroom"), | |
sidebarLayout( | |
sidebarPanel( | |
sliderInput("year", | |
"The year you were born", | |
min = babynames %>% pull(year) %>% min(), | |
max = babynames %>% pull(year) %>% max(), | |
step=1, | |
sep="", | |
value = 2000), | |
actionButton("run","Regenerate"), | |
hr(), | |
div("Based on data from the United State Social Security Administration. It includes all names that occured at least five times in the year.") | |
), | |
mainPanel( | |
plotOutput("classroom", height="600px") | |
) | |
) | |
) | |
server <- function(input, output) { | |
output$classroom <- renderPlot({ | |
input[["run"]] | |
babynames %>% | |
filter(year == isolate(input[["year"]])) %>% | |
sample_n(size - 1, replace=TRUE, weight=n) %>% | |
select(sex, name) %>% | |
arrange(name) %>% | |
mutate(id = row_number()) %>% | |
bind_rows(data_frame(id = size/2, sex="X",name="You"),.) %>% | |
arrange(id) %>% | |
mutate(id = row_number(id), | |
y = sqrt(size) - 1 - floor((id-1)/sqrt(size)), | |
x = (id - 1) %% (sqrt(size))) %>% | |
ggplot(aes(x=x,y=y,label=name))+ | |
geom_point(size=24, aes(color=sex))+ | |
geom_text(color = "#404040")+ | |
scale_color_manual(values=colors)+ | |
ylim(-1,sqrt(size))+ | |
xlim(-1,sqrt(size))+ | |
coord_fixed() + | |
theme_void()+ | |
theme(legend.position = "none") | |
}) | |
} | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment