Map deer-vehicle colisions with {shiny}

A roe deer in profile.

Oh dear, roe deer (Marek Szczepanek, CC BY-SA 4.0)

Open data

Deer roam Scotland. So do humans. It’s a problem when they meet at high speed.

The National Deer-Vehicle Collisions Project, administered by The Deer Initiative, has been monitoring data on deer-vehicle collisions in the UK.

The data are open. I found the data set when skimming through (a classic weekend activity for all the family). It links to the SNH Natural Spaces site where you can download the data as shapefile, GML or KML under the Open Government Licence.

I couldn’t find an interactive visualisation of these data and I want to spend more time working with the {shiny} package,1 so why not.

A simple Shiny app

Here’s a preview of the app in action:

A gif showing selections of some years, months and locations; hovering over points on the map; and showing the interactive datatable.

Along with {shiny}, the app was built primarily with the R packages:

The interface is a simple dashboard in three parts: a sidebar, a main panel and three value boxes.

The sidebar contains three collapsible sections with an ‘about’ section, a ‘how to’ section and a section with interactive filters. You can type or select collisions from a specified time (year and/or month) and place (the local authority in which it happened).

The main panel has two tabs. One is an interactive map that indicates the collisions with clickable markers. The second tab contains an interactive table. The data shown in these views is a result of the filters that have been applied via the sidebar. The interactive table also allows for further data filtering and you can download the results.

The value boxes each show one statistic related to the filtered data: the number of collisions; total collisions for the given local authorities and years; and the total collisions in the selected years. These update as you change your selections.

Run the app

The code for the app is available on GitHub. You can also download the cleaned data in CSV or RDS format.

You can run the app from an R session by installing {shiny} from CRAN and then executing the following lines:

  repo = "scot-deer-collisions", 
  username = "matt-dray"


I used to host the app for free on, but it’s since made way for other projects. That means you’ll have to download it to run it. The code will continue to exist on GitHub though, where you can leave an issue or fork it to make it better.

In terms of app design, I limited the filters to year, month and local authority. I could have included deer species and road, but the data are sparse and the formatting wasn’t great. I cleaned these variables up a bit and they’re available as filterable columns in the interactive table.

There’s also a large number of data points and it doesn’t make sense to show them all at once because it slows the app down. There are methods for improving this, but I assume most people will be looking at specific local authorities or roads rather than the country as a whole.

Session info
## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 4.0.2 (2020-06-22)
##  os       macOS  10.16                
##  system   x86_64, darwin17.0          
##  ui       X11                         
##  language (EN)                        
##  collate  en_GB.UTF-8                 
##  ctype    en_GB.UTF-8                 
##  tz       Europe/London               
##  date     2021-03-01                  
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version    date       lib source                            
##  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.0)                    
##  blogdown      0.21       2020-10-11 [1] CRAN (R 4.0.2)                    
##  bookdown      0.21       2020-10-13 [1] CRAN (R 4.0.2)                    
##  cli           2.3.0      2021-01-31 [1] CRAN (R 4.0.2)                    
##  digest        0.6.27     2020-10-24 [1] CRAN (R 4.0.2)                    
##  evaluate      0.14       2019-05-28 [1] CRAN (R 4.0.0)                    
##  glue          1.4.2      2020-08-27 [1] CRAN (R 4.0.2)                    
##  htmltools 2021-01-17 [1] Github (rstudio/htmltools@11cfbf3)
##  knitr         1.31       2021-01-27 [1] CRAN (R 4.0.2)                    
##  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.0.2)                    
##  rlang         0.4.10     2020-12-30 [1] CRAN (R 4.0.2)                    
##  rmarkdown     2.6        2020-12-14 [1] CRAN (R 4.0.2)                    
##  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.0.0)                    
##  stringi       1.5.3      2020-09-09 [1] CRAN (R 4.0.2)                    
##  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.0.0)                    
##  withr         2.4.1      2021-01-26 [1] CRAN (R 4.0.2)                    
##  xfun          0.21       2021-02-10 [1] CRAN (R 4.0.2)                    
##  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.0.0)                    
## [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

  1. But see this post about a Shiny app I made that that one colleague described as ‘very depressing’.↩︎