Site icon R-bloggers

Bike shares in Toronto

[This article was first published on The Data Sandbox, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Photo by Maarten van den Heuvel on Unsplash

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

< section id="bike-rental-shiny-app" class="level2">

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)
< section id="ui" class="level2">

UI

There are two user inputs on the UI side:

        sidebarPanel(
            sliderInput("dur",
                        "Trip Duration:",
                        min = 0,
                        max = 500,
                        value = c(0,500)),
            checkboxInput("freq",
                        "Exclude annual users:",
                        value = FALSE))
< section id="server" class="level2">

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")
< section id="application" class="level2">

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.

To leave a comment for the author, please follow the link and comment on their blog: The Data Sandbox.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.