JavaRad-R is a shiny application showing the distribution of delicious coffee in several major American cities compiled from their public Open Data initiatives:
It is based on the latest release of datasets at the time of data normalization.
# Libraries Used
library(readr)
library(shiny)
library(stringr)
library(shinyWidgets)
library(leaflet)
# Data
cafecitos_df <- read_csv("javarad-R.csv")
West Coast being the Best Coast (for Open Data!), the robust availability of regularly maintained datasets in sunny California necessitated the amalgamation of the first three cities into their shared metropolitan region. This allowed for a simplified final selection list for JavaRad-R:
The simplified list was next used to populate the choices defined in the ui.R file of the shiny application:
# Define UI for application that draws cafecitos
bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(top = 10, right = 10,
pickerInput("ciudades", label = "Select a City:",
choices = list("No thanks, all the cafecitos...",
`Los Angeles` = 1,
`San Francisco` = 2,
`New York City` = 3,
`Chicago` = 4,
`Washington DC` = 5,
`Cambridge` = 6,
`Detroit` = 7),
options = list(`live-search` = TRUE)
)
)
)
Data was then imported into the server.R file for manipulation and sanitized before populating new variables into the dataframe and defining the server + observer logic for delicious coffee between cities:
# Clean variables
cafecitos_df$dba_name <- str_to_title( cafecitos_df$dba_name )
cafecitos_df$address <- str_to_title( str_replace_all(cafecitos_df$address,"\\+"," ") )
cafecitos_df$uri <- str_replace_all( paste0(cafecitos_df$dba_name,"+",cafecitos_df$address), " ", "+" )
# Pop-Up
cafecitos_df$popup_text <- paste0("<center>", #Setting up popup info
"<b>", cafecitos_df$dba_name, "</b>",
"</br>", cafecitos_df$address,
"</br><a href='", 'https://google.com/search?q=', cafecitos_df$uri," ","+",
"' target='_blank'>More info...</a></center>")
# Define server logic required to draw all the coffee
shinyServer(function(input, output, session) {
# Filter coffee by city
filteredData <- reactive({
if (input[["ciudades"]] == "No thanks, all the cafecitos...") {
cafecitos_df
} else {
print(names(cafecitos_df))
subset(cafecitos_df, city_id == input[["ciudades"]])
}
})
# Create icons
icons <- awesomeIcons(
icon = "fa-coffee",
library = "fa",
iconColor = "#FFFFFF",
markerColor = "blue"
)
# Add map to output
output$"map" <- renderLeaflet({
leaflet(filteredData()) %>%
addProviderTiles(providers$Esri.WorldTopoMap) %>%
addAwesomeMarkers(~long, ~lat,
icon = icons,
label = ~dba_name,
labelOptions = labelOptions(textsize = "14px"),
popup = ~popup_text,
clusterOptions = markerClusterOptions())
})
# Create observer
observe({
leafletProxy("map", data = filteredData()) %>%
clearShapes() %>%
addAwesomeMarkers(~long, ~lat,
icon = icons,
label = ~dba_name,
labelOptions = labelOptions(textsize = "14px"),
popup = ~popup_text,
clusterOptions = markerClusterOptions())
})
})
Total delicious coffee of the 9 cities used is 3481 cafecitos and there were 127 records ignored during cafecito marker generation due to my inability to maintain patience with my Google API control panel. The city with the most delicious coffee in JavaRad-R is Los Angeles (1205) and the city with the fewest is Detroit (82).
Fully-deployed shinyapps.io application can be found here: JavaRad-R