Skip to content

Commit

Permalink
add the data explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
eveyp committed Aug 6, 2021
1 parent f81aeb5 commit 9688378
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions data-explorer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rsconnect/
106 changes: 106 additions & 0 deletions data-explorer/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#

library(shiny)
library(showtext)
library(dplyr)
library(ggplot2)
library(ggiraph)

latest_release = gh::gh("GET /repos/ucsf-bhhi/coc-data/releases/latest")
asset_position = purrr::detect_index(
latest_release$assets,
~ purrr::pluck(.x, "name") == "coc_data.rds"
)
asset_url = purrr::pluck(latest_release$assets, asset_position, "url")

# setup a tempfile
tf = tempfile()

# download the release
response = httr::GET(
asset_url,
httr::add_headers(c(Accept = "application/octet-stream")),
httr::write_disk(tf, overwrite = TRUE)
)

# load the data
coc_data = readr::read_rds(tf)

excluded_variables = c("coc_name", "coc_number")
y_excluded_variables = c("year", "coc_category")

make_choices = function(data, excluded) {
names(data)[!(names(data) %in% excluded)]
}

x_choices = make_choices(coc_data, excluded_variables)
y_choices = make_choices(coc_data, c(y_excluded_variables, excluded_variables))

font_add_google("Libre Franklin", family = "libre franklin")
showtext_auto()

base_plot = ggplot(coc_data) +
scale_y_continuous(labels = scales::label_comma()) +
scale_x_continuous(labels = scales::label_comma()) +
theme_minimal(base_family = "libre franklin") +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "grey93")
,
axis.text = element_text(size = 7, color = "grey50"),
axis.title = element_text(size = 8, color = "grey25")
)

format_values = function(x) {
case_when(
abs(x) <= 1 ~ scales::comma(x, accuracy = 0.01, trim = FALSE),
abs(x) > 1 ~ scales::comma(x, accuracy = 1, trim = FALSE)
)
}

ui <- fluidPage(
tags$head(
tags$style(HTML("@import url('https://fonts.googleapis.com/css2?family=Libre+Franklin&display=swap');"))
),
sidebarLayout(
sidebarPanel(
selectInput("y",
"Y-axis variable",
choices = y_choices,
selected = "homeless_per_1000_total_pop"),
selectInput("x",
"X-axis variable",
choices = x_choices,
selected = "share_rent_over_50_pct_inc"),
width = 3
),
mainPanel(
girafeOutput("scatterPlot", height = "600px"),
width = 9
)
)
)

server <- function(input, output) {
output$scatterPlot <- renderGirafe({
plot = base_plot +
geom_point_interactive(
aes(x = .data[[input$x]],
y = .data[[input$y]],
tooltip = paste0("<b>", coc_number, ": ", coc_name, ", ", year, "</b><br><hr style='margin-top:1px; margin-bottom:4px'>", input$x, ": ", format_values(.data[[input$x]]), "<br>", input$y, ": ", format_values(.data[[input$y]]))
),
alpha = 0.7)

girafe(ggobj = plot, width_svg = 8, options = list(opts_tooltip(css = "background-color: white; rx: 15; padding: 3px 5px 3px 5px; border-color: white; border-radius: 3px; box-shadow: 3px 3px 7px grey; font-family: Libre Franklin,Helvetica,Arial,sans-serif;", opacity = 1)))
})
}

# Run the application
shinyApp(ui = ui, server = server)

0 comments on commit 9688378

Please sign in to comment.