Introduction to opendatatoronto

opendatatoronto is an R interface to the City of Toronto Open Data Portal. The goal of the package is to help read data directly into R without needing to manually download it via the portal.

In the portal, datasets are called packages. You can see a list of available packages by using list_packages(). This will show metadata about the package, including what topics (i.e. tags) the package covers, a description of it, any civic issues it addresses, how many resources there are (and their formats), how often it is is refreshed and when it was last refreshed.

library(opendatatoronto)

packages <- list_packages(limit = 10)

packages
#> # A tibble: 10 x 10
#>    title id    topics civic_issues excerpt dataset_category num_resources
#>    <chr> <chr> <chr>  <chr>        <chr>   <chr>                    <int>
#>  1 3D M… 387b… Devel… <NA>         This i… Document                     4
#>  2 Body… c405… City … <NA>         This d… Table                        2
#>  3 Stre… 1db3… City … Mobility     Transi… Map                          1
#>  4 Stre… 74f6… City … <NA>         Public… Map                          1
#>  5 Stre… 821f… City … <NA>         Public… Map                          1
#>  6 Stre… ccfd… City … <NA>         Poster… Map                          1
#>  7 Stre… cf70… City … <NA>         Poster… Map                          1
#>  8 Stre… 99b1… City … <NA>         Inform… Map                          1
#>  9 Stre… 71e6… Trans… <NA>         "Bike … Map                          1
#> 10 Stre… 0c4e… City … <NA>         Bench … Map                          1
#> # … with 3 more variables: formats <chr>, refresh_rate <chr>,
#> #   last_refreshed <date>

Or, you can search packages by title using search_packages():

apartment_packages <- search_packages("Apartment")

apartment_packages
#> # A tibble: 2 x 10
#>   title id    topics civic_issues excerpt dataset_category num_resources
#>   <chr> <chr> <chr>  <chr>        <chr>   <chr>                    <int>
#> 1 Apar… 2b98… Busin… Affordable … "This … Table                        1
#> 2 Apar… 4ef8… Locat… Affordable … This d… Table                        1
#> # … with 3 more variables: formats <chr>, refresh_rate <chr>,
#> #   last_refreshed <date>

You can also see metadata for one specific package using show_package():

show_package("996cfe8d-fb35-40ce-b569-698d51fc683b")
#> # A tibble: 1 x 10
#>   title id    topics civic_issues excerpt dataset_category num_resources
#>   <chr> <chr> <chr>  <chr>        <chr>   <chr>                    <int>
#> 1 TTC … 996c… Trans… Mobility     TTC Su… Document                    33
#> # … with 3 more variables: formats <chr>, refresh_rate <chr>,
#> #   last_refreshed <date>

Within a package, there are a number of resources - e.g. CSV, XSLX, JSON, SHP files, and more. Resources are the actual “data”.

For a given package, you can get a list of resources using list_package_resources(), either by using a package found via search_packages() or list_packages():

apartment_building_registration_package <- search_packages("Apartment Building Registration")

apartment_building_registration_resources <- apartment_building_registration_package %>%
  list_package_resources()

apartment_building_registration_resources
#> # A tibble: 1 x 4
#>   name                       id                        format last_modified
#>   <chr>                      <chr>                     <chr>  <date>       
#> 1 Apartment Building Regist… 3ad76a8c-0518-4df2-b94e-… CSV    2019-11-06

or by passing the package’s portal URL directly:

list_package_resources("https://open.toronto.ca/dataset/apartment-building-registration/")
#> # A tibble: 1 x 4
#>   name                       id                        format last_modified
#>   <chr>                      <chr>                     <chr>  <date>       
#> 1 Apartment Building Regist… 3ad76a8c-0518-4df2-b94e-… CSV    2019-11-06

Finally (and most usefully!), you can download the resource (i.e., the actual data) directly into R using get_resource():

apartment_building_registration_data <- apartment_building_registration_resources %>%
  get_resource()

apartment_building_registration_data
#> # A tibble: 3,450 x 70
#>    `_id` AIR_CONDITIONIN… AMENITIES_AVAIL… BALCONIES BARRIER_FREE_AC…
#>    <int> <chr>            <chr>            <chr>     <chr>           
#>  1  4797 NONE             <NA>             YES       YES             
#>  2  4798 NONE             <NA>             YES       YES             
#>  3  4799 NONE             <NA>             YES       YES             
#>  4  4800 NONE             <NA>             YES       NO              
#>  5  4801 NONE             <NA>             YES       NO              
#>  6  4802 NONE             <NA>             YES       NO              
#>  7  4803 INDIVIDUAL UNITS <NA>             NO        NO              
#>  8  4804 NONE             <NA>             NO        NO              
#>  9  4805 NONE             <NA>             YES       NO              
#> 10  4806 NONE             <NA>             YES       NO              
#> # … with 3,440 more rows, and 65 more variables: BIKE_PARKING <chr>,
#> #   EXTERIOR_FIRE_ESCAPE <chr>, `FACILITIES_AVAILABLE?` <lgl>,
#> #   FIRE_ALARM <chr>, GARBAGE_CHUTES <chr>, HEATING_TYPE <chr>,
#> #   INTERCOM <chr>, `IS_THERE_A_COOLING_ROOM?` <lgl>,
#> #   `IS_THERE_EMERGENCY_POWER?` <lgl>, LAUNDRY_ROOM <chr>,
#> #   LOCKER_OR_STORAGE_ROOM <chr>, NO_BARRIERFREE_ACCESSBLE_UNITS <lgl>,
#> #   NO_OF_ACCESSIBLEPARKING_SPACES <lgl>, NO_OF_ELEVATORS <chr>,
#> #   NO_OF_STOREYS <lgl>, NO_OF_UNITS <lgl>, `NON-SMOKING_BUILDING` <lgl>,
#> #   PARKING_TYPE <chr>, PETS_ALLOWED <chr>,
#> #   PROP_MANAGEMENT_COMPANY_NAME <chr>, PROPERTY_TYPE <chr>, RSN <int>,
#> #   SEPARATE_GAS_METERS_EACH_UNIT <chr>,
#> #   SEPARATE_HYDRO_METER_EACH_UNIT <chr>,
#> #   SEPARATE_WATER_METERS_EA_UNIT <chr>, SITE_ADDRESS <chr>,
#> #   SPRINKLER_SYSTEM <chr>, VISITOR_PARKING <chr>, WARD <chr>,
#> #   WINDOW_TYPE <chr>, YEAR_BUILT <chr>, YEAR_REGISTERED <chr>,
#> #   HEATING_EQUIPMENT_STATUS <chr>, DATE_OF_LAST_INSPECTION_BY_TSSA <chr>,
#> #   NON_SMOKING_BUILDING <chr>, FACILITIES_AVAILABLE <chr>, PCODE <chr>,
#> #   SPRINKLER_SYSTEM_TEST_RECORD <chr>, IS_THERE_A_COOLING_ROOM <chr>,
#> #   ELEVATOR_STATUS <chr>, ELEVATOR_PARTS_REPLACED <chr>,
#> #   DESCRIPTION_OF_INDOOR_EXERCISE_ROOM <chr>,
#> #   SPRINKLER_SYSTEM_YEAR_INSTALLED <int>, CONFIRMED_UNITS <int>,
#> #   YEAR_OF_REPLACEMENT <chr>, OUTDOOR_GARBAGE_STORAGE_AREA <chr>,
#> #   NO_OF_LAUNDRY_ROOM_MACHINES <int>,
#> #   ANNUAL_FIRE_PUMP_FLOW_TEST_RECORDS <chr>,
#> #   IS_THERE_EMERGENCY_POWER <chr>, ANNUAL_FIRE_ALARM_TEST_RECORDS <chr>,
#> #   HEATING_EQUIPMENT_YEAR_INSTALLED <int>,
#> #   APPROVED_FIRE_SAFETY_PLAN <chr>, CONFIRMED_STOREYS <int>,
#> #   LAUNDRY_ROOM_LOCATION <chr>, LAUNDRY_ROOM_HOURS_OF_OPERATION <chr>,
#> #   EMERG_POWER_SUPPLY_TEST_RECORDS <chr>, TSSA_TEST_RECORDS <chr>,
#> #   DESCRIPTION_OF_OUTDOOR_REC_FACILITIES <chr>, GREEN_BIN_LOCATION <chr>,
#> #   PET_RESTRICTIONS <chr>, DESCRIPTION_OF_CHILD_PLAY_AREA <chr>,
#> #   INDOOR_GARBAGE_STORAGE_AREA <chr>, RECYCLING_BINS_LOCATION <chr>,
#> #   NO_OF_ACCESSIBLE_PARKING_SPACES <int>,
#> #   NO_BARRIER_FREE_ACCESSBLE_UNITS <int>

The opendatatoronto package can currently handle the download of CSV, XLS/XLSX, XML, JSON, SHP, and GeoJSON resources, as well as ZIP resources that contain multiple files. For more information, see the following vignettes: