Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

252 check errors #253

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: stats19
Title: Work with Open Road Traffic Casualty Data from Great Britain
Version: 3.1.0.9000
Version: 3.2.0
Authors@R: c(
person("Robin", "Lovelace", email = "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-5679-6536")),
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# stats19 (development version)
# stats19 3.2.0 2024-10

* Updates so package functions fail gracefully when input data is not as expected, e.g. due to URL changes (#252)

# stats19 3.1.0 2024-07

Expand Down
15 changes: 12 additions & 3 deletions R/dl.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,21 @@ dl_stats19 = function(year = NULL,
stop("Stopping as requested", call. = FALSE)
}
}
# Save to tempfile first, to avoid partial downloads
tmp_file = tempfile()
# Check to see if zip_url is a valid URL with the curl package:
if (!curl::has_internet()) {
message("No internet connection detected. Please check your connection and try again.")
return(NULL)
}
if (isFALSE(silent)) {
message("Attempt downloading from: ", zip_url)
}
# Save to tempfile first, to avoid partial downloads
tmp_file = tempfile()
utils::download.file(zip_url, tmp_file, quiet = silent)
res = curl::curl_fetch_disk(zip_url, tmp_file)
if (res$status != 200) {
message("Failed to download file: ", zip_url)
return(NULL)
}
file.rename(tmp_file, destfile)
if (isFALSE(silent)) {
message("Data saved at ", destfile)
Expand Down
4 changes: 4 additions & 0 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ read_collisions = function(year = NULL,
message(path)
}
# read the data in
if (is.null(path)) {
message("File not found.")
return(NULL)
}
suppressWarnings({
ac = readr::read_csv(path, col_types = col_spec())
})
Expand Down
1 change: 0 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ locate_one_file = function(filename = NULL,
year = NULL,
type = NULL) {
# see if locate_files can pin it down
# browser()
path = locate_files(data_dir = data_dir,
type = type,
years = year,
Expand Down
4 changes: 3 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ Note that cycling manifests distinct morning and afternoon peaks [see @lovelace_

Examples of how the package can been used for policy making include:

- Use of the package in a web app created by the library service of the UK Parliament. See [commonslibrary.parliament.uk](https://commonslibrary.parliament.uk/constituency-data-traffic-accidents/), screenshots of which from December 2019 are shown below, for details.
- Use of the package in a web app created by the library service of the UK Parliament. See commonslibrary.parliament.uk^[
https://commonslibrary.parliament.uk/constituency-data-traffic-accidents/
] screenshots of which from December 2019 are shown below, for details.

![](https://user-images.githubusercontent.com/1825120/70164249-bf730080-16b8-11ea-96d8-ec92c0b5cc69.png)

Expand Down
123 changes: 54 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

<!-- badges: start -->

<!-- [![Travis build status](https://travis-ci.org/ropensci/stats19.svg?branch=master)](https://travis-ci.org/ropensci/stats19) -->

[![](http://www.r-pkg.org/badges/version/stats19)](https://www.r-pkg.org/pkg/stats19)
Expand All @@ -15,9 +14,7 @@ cycle](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://l
<!-- badges: end -->

<!-- [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.2540781.svg)](https://doi.org/10.5281/zenodo.2540781) -->

<!-- [![Gitter chat](https://badges.gitter.im/ITSLeeds/stats19.png)](https://gitter.im/stats19/Lobby?source=orgpage) -->

<!-- README.md is generated from README.Rmd. Please edit that file -->

# stats19 <a href='https://docs.ropensci.org/stats19/'><img src='https://raw.githubusercontent.com/ropensci/stats19/master/man/figures/logo.png' align="right" height=215/></a>
Expand Down Expand Up @@ -57,8 +54,6 @@ remotes::install_github("ropensci/stats19")

``` r
library(stats19)
#> Data provided under OGL v3.0. Cite the source and link to:
#> www.nationalarchives.gov.uk/doc/open-government-licence/version/3/
```

You can install the released version of stats19 from
Expand All @@ -68,7 +63,7 @@ You can install the released version of stats19 from
install.packages("stats19")
```

## get\_stats19()
## get_stats19()

`get_stats19()` requires `year` and `type` parameters, mirroring the
provision of STATS19 data files, which are categorised by year (from
Expand All @@ -86,20 +81,20 @@ crashes = get_stats19(year = 2022, type = "collision")
#> https://data.dft.gov.uk/road-accidents-safety-data/dft-road-casualty-statistics-collision-2022.csv
#> Data already exists in data_dir, not downloading
#> Reading in:
#> ~/data/bronze/stats19/dft-road-casualty-statistics-collision-2022.csv
#> ~/data/stats19/dft-road-casualty-statistics-collision-2022.csv
#> date and time columns present, creating formatted datetime column
#> Warning in format_stats19(x, type = "Accident"): NAs introduced by coercion
#> Warning in format_stats19(x, type = "Accident"): NAs introduced by coercion
#> Warning in format_stats19(x, type = "Accident"): NAs introduced by coercion
#> Warning: NAs introduced by coercion
#> Warning: NAs introduced by coercion
#> Warning: NAs introduced by coercion
```

What just happened? For the `year` 2022 we read-in crash-level (`type =
"collision"`) data on all road crashes recorded by the police across
Great Britain. The dataset contains 37 columns (variables) for 106,004
crashes. We were not asked to download the file (by default you are
asked to confirm the file that will be downloaded). The contents of this
dataset, and other datasets provided by **stats19**, are outlined below
and described in more detail in the [stats19
What just happened? For the `year` 2022 we read-in crash-level
(`type = "collision"`) data on all road crashes recorded by the police
across Great Britain. The dataset contains 37 columns (variables) for
106,004 crashes. We were not asked to download the file (by default you
are asked to confirm the file that will be downloaded). The contents of
this dataset, and other datasets provided by **stats19**, are outlined
below and described in more detail in the [stats19
vignette](https://itsleeds.github.io/stats19/articles/stats19.html).

We will see below how the function also works to get the corresponding
Expand All @@ -121,24 +116,24 @@ dl_stats19(year = 2022, data_dir = tempdir())
```

Multiple matches. Which do you want to download?

1: dft-road-casualty-statistics-casualty-2022.csv
2: dft-road-casualty-statistics-vehicle-2022.csv
3: dft-road-casualty-statistics-collision-2022.csv

Selection:
Enter an item from the menu, or 0 to exit

## Using the data

STATS19 data consists of 3 main tables:

- Collisions, the main table which contains information on the crash
time, location and other variables (37 columns in total)
- Casualties, containing data on people hurt or killed in each crash
(19 columns in total)
- Vehicles, containing data on vehicles involved in or causing each
crash (28 columns in total)
- Collisions, the main table which contains information on the crash
time, location and other variables (37 columns in total)
- Casualties, containing data on people hurt or killed in each crash (19
columns in total)
- Vehicles, containing data on vehicles involved in or causing each
crash (28 columns in total)

The contents of each is outlined below.

Expand Down Expand Up @@ -192,8 +187,7 @@ formatted as follows:
casualties = get_stats19(year = 2022, type = "casualty", ask = FALSE, format = TRUE)
#> Files identified: dft-road-casualty-statistics-casualty-2022.csv
#> https://data.dft.gov.uk/road-accidents-safety-data/dft-road-casualty-statistics-casualty-2022.csv
#> Attempt downloading from: https://data.dft.gov.uk/road-accidents-safety-data/dft-road-casualty-statistics-casualty-2022.csv
#> Data saved at ~/data/bronze/stats19/dft-road-casualty-statistics-casualty-2022.csv
#> Data already exists in data_dir, not downloading
#> Warning: The following named parsers don't match the column names:
#> accident_severity, carriageway_hazards, date, day_of_week,
#> did_police_officer_attend_scene_of_accident, first_road_class,
Expand Down Expand Up @@ -267,8 +261,7 @@ and formatted as follows:
vehicles = get_stats19(year = 2022, type = "vehicle", ask = FALSE, format = TRUE)
#> Files identified: dft-road-casualty-statistics-vehicle-2022.csv
#> https://data.dft.gov.uk/road-accidents-safety-data/dft-road-casualty-statistics-vehicle-2022.csv
#> Attempt downloading from: https://data.dft.gov.uk/road-accidents-safety-data/dft-road-casualty-statistics-vehicle-2022.csv
#> Data saved at ~/data/bronze/stats19/dft-road-casualty-statistics-vehicle-2022.csv
#> Data already exists in data_dir, not downloading
#> Warning: The following named parsers don't match the column names:
#> accident_severity, carriageway_hazards, date, day_of_week,
#> did_police_officer_attend_scene_of_accident, first_road_class,
Expand Down Expand Up @@ -337,7 +330,7 @@ names(vehicles)
## Creating geographic crash data

An important feature of STATS19 data is that the collision table
contains geographic coordinates. These are provided at \~10m resolution
contains geographic coordinates. These are provided at ~10m resolution
in the UK’s official coordinate reference system (the Ordnance Survey
National Grid, EPSG code 27700). **stats19** converts the non-geographic
tables created by `format_collisions()` into the geographic data form of
Expand All @@ -361,16 +354,7 @@ Wales).

``` r
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
wy = filter(police_boundaries, pfa16nm == "West Yorkshire")
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
crashes_wy = crashes_sf[wy, ]
Expand Down Expand Up @@ -546,29 +530,27 @@ Lovelace, Roberts, and Kellar 2016 for more on this).

Examples of how the package can been used for policy making include:

- Use of the package in a web app created by the library service of
the UK Parliament. See
[commonslibrary.parliament.uk](https://commonslibrary.parliament.uk/constituency-data-traffic-accidents/),
screenshots of which from December 2019 are shown below, for
details.
- Use of the package in a web app created by the library service of the
UK Parliament. See commonslibrary.parliament.uk[^1] screenshots of
which from December 2019 are shown below, for details.

![](https://user-images.githubusercontent.com/1825120/70164249-bf730080-16b8-11ea-96d8-ec92c0b5cc69.png)

- Use of methods taught in the
[stats19-training](https://docs.ropensci.org/stats19/articles/stats19-training.html)
vignette by road safety analysts at Essex Highways and the Safer
Essex Roads Partnership ([SERP](https://saferessexroads.org/)) to
inform the deployment of proactive front-line police enforcement in
the region (credit: Will Cubbin).

- Mention of road crash data analysis based on the package in an
[article](https://www.theguardian.com/cities/2019/oct/07/a-deadly-problem-should-we-ban-suvs-from-our-cities)
on urban SUVs. The question of how vehicle size and type relates to
road safety is an important area of future research. A starting
point for researching this topic can be found in the
[`stats19-vehicles`](https://docs.ropensci.org/stats19/articles/stats19-vehicles.html)
vignette, representing a possible next step in terms of how the data
can be used.
- Use of methods taught in the
[stats19-training](https://docs.ropensci.org/stats19/articles/stats19-training.html)
vignette by road safety analysts at Essex Highways and the Safer Essex
Roads Partnership ([SERP](https://saferessexroads.org/)) to inform the
deployment of proactive front-line police enforcement in the region
(credit: Will Cubbin).

- Mention of road crash data analysis based on the package in an
[article](https://www.theguardian.com/cities/2019/oct/07/a-deadly-problem-should-we-ban-suvs-from-our-cities)
on urban SUVs. The question of how vehicle size and type relates to
road safety is an important area of future research. A starting point
for researching this topic can be found in the
[`stats19-vehicles`](https://docs.ropensci.org/stats19/articles/stats19-vehicles.html)
vignette, representing a possible next step in terms of how the data
can be used.

## Next steps

Expand All @@ -588,20 +570,20 @@ save lives. For more on these next steps, see the package’s introductory

The **stats19** package builds on previous work, including:

- code in the [bikeR](https://github.com/Robinlovelace/bikeR) repo
underlying an academic paper on collisions involving cyclists
- functions in [**stplanr**](https://docs.ropensci.org/stplanr/) for
downloading Stats19 data
- updated functions related to the
[CyIPT](https://github.com/cyipt/stats19) project
- code in the [bikeR](https://github.com/Robinlovelace/bikeR) repo
underlying an academic paper on collisions involving cyclists
- functions in [**stplanr**](https://docs.ropensci.org/stplanr/) for
downloading Stats19 data
- updated functions related to the
[CyIPT](https://github.com/cyipt/stats19) project

[![ropensci\_footer](https://ropensci.org/public_images/ropensci_footer.png)](https://ropensci.org)
[![ropensci_footer](https://ropensci.org/public_images/ropensci_footer.png)](https://ropensci.org)

## References

<div id="refs" class="references hanging-indent">
<div id="refs" class="references csl-bib-body hanging-indent">

<div id="ref-lovelace_stats19_2019">
<div id="ref-lovelace_stats19_2019" class="csl-entry">

Lovelace, Robin, Malcolm Morgan, Layik Hama, Mark Padgham, and M
Padgham. 2019. “Stats19 A Package for Working with Open Road Crash
Expand All @@ -610,7 +592,7 @@ Data.” *Journal of Open Source Software* 4 (33): 1181.

</div>

<div id="ref-lovelace_who_2016">
<div id="ref-lovelace_who_2016" class="csl-entry">

Lovelace, Robin, Hannah Roberts, and Ian Kellar. 2016. “Who, Where,
When: The Demographic and Geographic Distribution of Bicycle Crashes in
Expand All @@ -621,3 +603,6 @@ Behaviour*, Bicycling and bicycle safety, 41, Part B.
</div>

</div>

[^1]:
<https://commonslibrary.parliament.uk/constituency-data-traffic-accidents/>
8 changes: 1 addition & 7 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
Errant README.html file removed and correct doi: text added.

This is a minor bug fix update.

I have added some references to the DESCRIPTION file.

These generated an automated note from CRAN that I have responded to.
Updates so package functions fail gracefully when input data is not as expected, e.g. due to URL changes.

## R CMD check results

Expand Down
7 changes: 0 additions & 7 deletions tests/skip-download.R

This file was deleted.

4 changes: 0 additions & 4 deletions tests/testthat.R

This file was deleted.

24 changes: 0 additions & 24 deletions tests/testthat/test-data_dir.R

This file was deleted.

22 changes: 0 additions & 22 deletions tests/testthat/test-dl_stats19.R

This file was deleted.

Loading
Loading