## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(laOpenData)
library(ggplot2)
library(dplyr)

## ----la-list-datasets---------------------------------------------------------
la_list_datasets() |> head()

## ----la-vacant-pull-----------------------------------------------------------
la_building_safety_vacant <- la_pull_dataset(
  dataset = "q3ak-s5hy", limit = 2, timeout_sec = 90)

la_building_safety_vacant <- la_pull_dataset(
  dataset = "building_and_safety_vacant_building_abatement", limit = 2, timeout_sec = 90)

## ----filter-location----------------------------------------------------------

la_businesses <- la_pull_dataset(dataset = "6rrh-rzua",limit = 3, timeout_sec = 90, filters = list(city = "LOS ANGELES"))
la_businesses

# Checking to see the filtering worked
la_businesses |>
  distinct(city)

## ----filter-la-eight----------------------------------------------------------
# Creating the dataset
la_businesses_8 <- la_pull_dataset(dataset = "6rrh-rzua", limit = 50, timeout_sec = 90, filters = list(city = "LOS ANGELES", council_district = 8))

# Calling head of our new dataset
la_businesses_8 |>
  slice_head(n = 6)

# Quick check to make sure our filtering worked
la_businesses_8 |>
  summarize(rows = n())

la_businesses_8 |>
  distinct(city)

la_businesses_8 |>
  distinct(council_district)

## ----complaint-type-graph, fig.alt="Bar chart showing the frequency of business types in LA in district 8.", fig.cap="Bar chart showing the frequency of business types in LA in district 8.", fig.height=5, fig.width=7----
# Visualizing the distribution, ordered by frequency
la_businesses_8 |>
  count(primary_naics_description) |>
  ggplot(aes(
    x = n,
    y = reorder(primary_naics_description, n)
  )) +
  geom_col(fill = "steelblue") +
  theme_minimal() +
  labs(
    title = "Top 50 Business Types in District 8 of LA",
    x = "Number of Businesses",
    y = "Business Type"
  )

