Introduction to cqcr

library(cqcr)

Using cqcr

In the example below, get all care homes in Waltham Forest, Hackney and Tower Hamlets.

loc1 <- cqc_locations_search(care_home = TRUE, 
                             local_authority = c("Waltham Forest",
                                                 "Hackney", "Tower Hamlets"))

Given the list of care home IDs, we can retrieve more detailed information on each of these locations, and with some functions from purrr create a data frame with some basic information.

care_home_list <- lapply(loc1$location_id, cqc_location)

care_home_df <- map_dfr(care_home_list, `[`, c("location_id", "name", "number_of_beds",
                                    "onspd_longitude", "onspd_latitude"))

care_home_df <- care_home_df %>% 
  mutate(rating = care_home_list %>%
           map(c("current_ratings", "overall", "rating")) %>% as.character(),
         rating = recode(rating, "NULL" = "No published rating"),
         rating = factor(rating, levels = c("Outstanding", "Good",
                                            "Requires improvement", 
                                            "Inadequate",
                                            "No published rating")),
         report_date = care_home_list %>%
           map(c("current_ratings", "reportDate")) %>%
           as.character(),
         report_date = ifelse(report_date == "NULL", NA, report_date) )

care_home_df

Then, we can map it out with leaflet. In this case the colours indicate the most recent inspection rating, and the size corresponds to a scaled count of beds.

library(leaflet)

pal <- colorFactor(c("blue", "green", "orange", "red", "gray27"),
                   domain = care_home_df$rating)

labels <- paste0(
  "<strong>Name:</strong> ", care_home_df$name,"</br>",
  "<strong>Number of Beds:</strong> ", care_home_df$number_of_beds, "</br>",
  "<strong>Rating:</strong> ", care_home_df$rating, "</br>",
  "<strong>Report Date:</strong> ", format(as.Date(care_home_df$report_date), "%e %B %Y")
) %>% lapply(htmltools::HTML)

map <- leaflet(care_home_df) %>%
  addTiles() %>%
    addCircleMarkers(lng = ~onspd_longitude,
             lat = ~onspd_latitude,
             label = labels,
             color = ~pal(rating), 
             radius = ~scales::rescale(care_home_df$number_of_beds, to = c(5, 15)))


map