opendatascot

🗝 R interface to datasets on opendata.scot

OTHER License

Stars
1
Committers
2

output: github_document

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

opendatascotland

opendatascotland is an R package to download and locally cache data from the amazing opendata.scot website. This helps to quickly start data analysis by providing a simple way to save, organise and import data in R.

Installation

You can install the development version of opendatascotland from GitHub with:

# install.packages("devtools")
devtools::install_github("fozy81/opendatascot")

Search

Search all available datasets by using the ods_search() function. Additionally, use the search argument to query datasets by title.

library(opendatascotland)
# View all available datasets and associated metadata
all_datasets <- ods_search()

# Search dataset titles containing matching terms (case insensitive)
single_query <- ods_search("Number of bikes")

# Search multiple terms
multi_query <- ods_search(c("Bins", "Number of bikes"))
head(multi_query, 3)

Note, search term is case-insensitive but word order must be correct (there is no 'fuzzy' matching).

Download

Currently, only datasets available in .csv, .json or .geojson can be downloaded. These formats cover the majority of data available. You will be warned if data can't be downloaded.

To download data, you can either download the metadata using ods_search(), then pass that data frame to ods_get()

query <- ods_search("Salt Bins")
data <- ods_get(query, ask = FALSE, refresh = TRUE)
query <- ods_search("Public Litter Bins")
data <- ods_get(query)

Or use the search argument in ods_get(search = "my search term") to search and download matching datasets in one step.

data <- ods_get(search = "Salt Bins", ask = FALSE, refresh = TRUE)
data <- ods_get(search = "Salt Bins")

By default, you will be asked if you want to save the data locally on the first download. Optionally, you can refresh the data or avoid being asked to save data.

data <- ods_get(search = "Number of bikes", refresh = TRUE, ask = FALSE)
data <- ods_get(
  search = c(
    "Salt Bins",
    "Recycling Points"
  ),
  ask = FALSE,
  refresh = TRUE
)

The ods_get() function returns a named list of data frames

data <- ods_get(search = c("Salt Bins", "Recycling Points"))
names(data)
[1] "Glass_and_textiles_recycling_points_Aberdeenshire_Council"
[2] "Recycling_Points_Aberdeen_City_Council"                   
[3] "Recycling_Points_Moray_Council"                           
[4] "Salt_Bins_Dumfries_and_Galloway_Council" 

Select the dataset by name.

recycling_points <- data$Recycling_Points_Aberdeen_City_Council

Or alternatively select data frame in the list by position number.

recycling_points <- data[[2]]

Geojson datasets are automating converted to simple feature 'sf' data. This helps make geospatial data easier to handle in R. As we can see in this example the data frame is classed as "sf" which means spatial / geometry coordinates are held in a geometry column.

class(recycling_points)

This allows the plot() function to automatically plot the coordinates in the geometry column.

plot(recycling_points[, "TEXTILE_YN"])