Skip to contents

Built with R 4.3.1


This example will build a site to explore arrest data in Illinois, which is made publicly available by the Illinois Criminal Justice Information Authority.

Initialize

Start by initializing the site’s structure in your desired directory:

library(community)
dir <- "../arrests_site"
init_site(dir, "Illinois Arrests")

Add Data

We can use the build.R file to set up a repeatable data importing process. Replace its contents with this:

# remotes::install_github("uva-bi-sdad/catchment")
library(catchment)
library(community)
library(sf)

if (!is.character(dir)) dir <- "../arrests_site"

# download county shapes -- only needs to be run once
if (!file.exists(paste0(dir, "/docs/counties.geojson"))) {
  shapes <- download_census_shapes(
    paste0(dir, "/docs"), "Illinois", "county", "counties",
    simplify = rmapshaper::ms_simplify, keep_shapes = TRUE
  )
  
  ## the data separates Chicago from the rest of the county it's in,
  ## so we will need separate shapes for Cook County
  shapes_subs <- download_census_shapes(
    paste0(dir, "/docs"), "Illinois", "cousub", "subcounties",
    simplify = rmapshaper::ms_simplify, keep_shapes = TRUE
  )
  
  ### get the subcounties within Cook County
  cook_county <- shapes_subs[shapes_subs$COUNTYFP == "031",]
  
  ### Replace the overall Cook county shape with Chicago and Cook county suburb shapes
  cook <- st_drop_geometry(shapes[shapes$COUNTYFP == "031",])
  
  chicago <- st_as_sf(cbind(cook[, 1:3], cook_county[1, -(1:4)]))
  chicago$NAME = "cook chicago"
  
  cook_suburbs <- cbind(cook[, 1:5], cook_county[2, -(1:6)])
  cook_suburbs$NAME = "cook county suburbs"
  cook_suburbs[, c("ALAND", "AWATER")] <- colSums(st_drop_geometry(
    cook_county[-1, c("ALAND", "AWATER")]
  ))
  st_geometry(cook_suburbs) <- st_union(cook_county[-1,])
  
  shapes[shapes$NAME == "De Witt", "NAME"] <- "dewitt"
  cols <- c("GEOID", "NAME", "geometry")
  shapes <- rbind(shapes[shapes$NAME != "Cook", cols], chicago[, cols], cook_suburbs[, cols])
  unlink(paste0(dir, "/docs/counties.geojson"))
  st_write(shapes, paste0(dir, "/docs/counties.geojson"))
}
  
# download arrests data -- run any time the data are updated
data_sex <- read.csv(
  "https://icjia.illinois.gov/arrestexplorer/api/?split=county,gender"
)
data_age <- read.csv(
  "https://icjia.illinois.gov/arrestexplorer/api/?split=county,age_group"
)
data_race <- read.csv(
  "https://icjia.illinois.gov/arrestexplorer/api/?split=county,race"
)
data <- cbind(data_sex, data_age[, -(1:2)], data_race[, -(1:2)])
data <- data[data$county != "non county agencies",]

## get GEOIDs from county names in the shapes file
data$GEOID <- structure(shapes$GEOID, names = tolower(shapes$NAME))[data$county]

## write data
write.csv(data[, -2], paste0(dir, "/docs/data/data.csv"), row.names = FALSE)

## document the data
data_add(
  paste0(dir, "/docs/data/data.csv"),
  list(
    ids = "GEOID",
    time = "Year"
  )
)

Build Interface

Now we can specify the interface in site.R and build it:

library(community)
if (!is.character(dir)) dir <- "../arrests_site"

page_head(
  title = "Illinois Arrests",
  icon = "https://icjia.illinois.gov/researchhub/docs/assets/favicon.ico"
)
page_navbar(
  title = "Illinois Arrests",
  input_button("About ICJIA", "https://icjia.illinois.gov")
)

page_menu(
  input_select("Variable", "variables", "gender_female", id = "selected_variable"),
  input_number(
    "Focal Year", "selected_year", variable = "Year", default = "max"
  ),
  default_open = TRUE
)

input_dataview(
  "main_view", "selected_variable", time = "Year", time_agg = "selected_year"
)

output_info(
  "features.name", c("variables.long_name" = "value"),
  floating = TRUE, subto = c("main_map", "main_plot")
)
output_map(
  list(
    name = "data", url = paste0(dir, "/docs/counties.geojson"), id_property = "GEOID"
  ), id = "main_map", dataview = "main_view", subto = c("main_plot", "main_legend")
)
output_legend(id = "main_legend")
page_tabgroup(
  list(
    name = "Plot",
    output_plot(
      "Year", "selected_variable", id = "main_plot",
      dataview = "main_view", subto = c("main_map", "main_legend")
    )
  ),
  list(
    name = "Table",
    output_table()
  )
)

site_build(dir, open_after = TRUE, serve = TRUE)