Row

Interactive map

Row

Data Table

---
title: "Share bike in Portland"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
# https://github.com/simonpcouch/gbfs
library(flexdashboard)
library(gbfs)
library(tidyverse)
library(leaflet)
library(crosstalk)  # inter-widget interactivity
library(DT)  # interactive tables

# -------------- Data ------------------s
pdx_station_info <- 
  get_station_information("https://gbfs.biketownpdx.com/gbfs/gbfs.json")

pdx_station_status <- 
  get_station_status("https://gbfs.biketownpdx.com/gbfs/gbfs.json")

pdx_stations <- full_join(pdx_station_info, 
                          pdx_station_status, 
                          by = "station_id") %>%
  select(id = station_id, 
         lon, 
         lat, 
         num_bikes_available, 
         num_docks_available) %>%
  mutate(type = "docked")

pdx_free_bikes <- 
  get_free_bike_status("https://gbfs.biketownpdx.com/gbfs/gbfs.json", 
                       output = "return") %>%
  select(id = bike_id, lon, lat) %>%
  mutate(num_bikes_available = 1,
         num_docks_available = 0,
         type = "free")

pdx_full <- bind_rows(pdx_stations, pdx_free_bikes)

shared_data <- SharedData$new(pdx_full)

```


Sidebar {.sidebar data-width=300} 
-----------------------------------------------------------------------

### Filters

```{r filters}
  
filter_checkbox(
    id = "type",
    label = "Bikes type",
    sharedData = shared_data,
    group = ~type

)

filter_slider(
    id = "num_bikes_available",
    label = "Available bikes",
    sharedData = shared_data,
    column = ~num_bikes_available,
    step = 1,
    round = TRUE,
    sep = "",
    ticks = FALSE,
    max = max(pdx_full$num_bikes_available) + 1,
    min = min(pdx_full$num_bikes_available) - 1
)

filter_slider(
    id = "num_docks_available",
    label = "Available docks",
    sharedData = shared_data,
    column = ~num_docks_available,
    step = 1,
    round = TRUE,
    sep = "",
    ticks = FALSE,
    max = max(pdx_full$num_docks_available) + 1,
    min = min(pdx_full$num_docks_available) - 1
)


shared_data %>% 
  datatable(
    extensions=c(
      "Buttons",  # add download buttons, etc
      "Scroller"  # for scrolling down the rows rather than pagination
    ),
    style="bootstrap", 
    class="compact", 
    width="100%",
    options= list(
      dom = "Blrtip",  # specify content (search box, etc)
      deferRender = TRUE,
      scrollY = 300,
      scroller = TRUE,
      buttons = list(
        I("colvis"),  # turn columns on and off
        "csv",  # download as .csv
        "excel"  # download as .xlsx
      )
    ),
    colnames = c(
      "ID"="id",
      "availabe_bikes"="num_bikes_available",
      "availabe_dockss"="num_docks_available",
      "type" = "type"
    )
    
)


```



Row
-----------------------------------------------------------------------

### Interactive map

```{r}

shared_data %>% 
  leaflet() %>% 
  addProviderTiles(providers$OpenStreetMap) %>% 
  addCircleMarkers(lng=~pdx_full$lon,lat=~pdx_full$lat, radius = ~pdx_full$num_bikes_available, color="blue", weight=2) %>% 
  addCircleMarkers(lng=~pdx_full$lon,lat=~pdx_full$lat, radius = ~pdx_full$num_docks_available, color="red", weight=2) %>% 
  addAwesomeMarkers(
      icon = awesomeIcons(
        library = "ion",
        icon = ifelse(
          test = pdx_full$type == "free",
          yes = "ion-android-star-outline",
          no = ""
        ),
        iconColor = "white",
        markerColor = ifelse(
          test = pdx_full$type == "free", 
          yes = "orange",
          no = ""
        )
      )
    )


```


Row
-----------------------------------------------------------------------


### Data Table
  
```{r data_table}

shared_data %>% 
  datatable(
    extensions=c(
      "Buttons",  # add download buttons, etc
      "Scroller"  # for scrolling down the rows rather than pagination
    ),
    style="bootstrap", 
    class="compact", 
    width="100%",
    options= list(
      dom = "Blrtip",  # specify content (search box, etc)
      deferRender = TRUE,
      scrollY = 300,
      scroller = TRUE,
      buttons = list(
        I("colvis"),  # turn columns on and off
        "csv",  # download as .csv
        "excel"  # download as .xlsx
      )
    ),
    colnames = c(
      "ID"="id",
      "availabe_bikes"="num_bikes_available",
      "availabe_dockss"="num_docks_available",
      "type" = "type"
    )
    
)


```