Bike shares in Toronto

Analysis of a bike sharing app in Toronto
Project
Shiny App
R
Author

Mark Edney

Published

February 27, 2022

Photo by Maarten van den Heuvel on Unsplash

This article is based on a project written on 01/14/2021

Bike Rental Shiny App

This application use the data collected from the Toronto Open Data to generate a histogram of the usage of rental bikes in Toronto during the month of June in 2020.

install.packages("opendatatoronto", 
                 repos = "https://cran.us.r-project.org",
                 dependencies = TRUE)
library(opendatatoronto)
library(tidyverse)
library(lubridate)
library(shiny)

UI

There are two user inputs on the UI side:

  • A slider that limits the maximum and minimum of the displayed values

  • A checkbox that excludes users with a annual bike pass

        sidebarPanel(
            sliderInput("dur",
                        "Trip Duration:",
                        min = 0,
                        max = 500,
                        value = c(0,500)),
            checkboxInput("freq",
                        "Exclude annual users:",
                        value = FALSE))

Server

The following code is used for the server side logic, this includes downloading the data from the ‘opendatatoronto’ library.

 # get package
    package <- show_package("7e876c24-177c-4605-9cef-e50dd74c617f")
    
    # get all resources for this package
    resources <- list_package_resources("7e876c24-177c-4605-9cef-e50dd74c617f")
    # identify datastore resources; by default, Toronto Open Data sets datastore resource format to CSV for non-geospatial and GeoJSON for geospatial resources
    datastore_resources <- filter(resources, tolower(format) %in% c('zip', 'geojson'))
    # load the first datastore resource as a sample
    data <- filter(datastore_resources, name == "Bike share ridership 2020") %>% get_resource()
    data2 <-  data$`2020-06.csv`
    data2[grepl("Time",names(data2))] <- 
        lapply(data2[grepl("Time",names(data2))], parse_date_time, orders = "mdy HM")
    data2$Dur <- as.numeric(data2$End.Time - data2$Start.Time,units="mins")

Application

The final application takes a while to load as the data needs to be downloaded and sorted through. In future iterations, I would save the data locally as an RDS file.