R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tmap)
library(sf)
## Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(leaflet)
library(shiny)
## Warning: package 'shiny' was built under R version 4.4.3
library(rsconnect)
## 
## Attaching package: 'rsconnect'
## 
## The following object is masked from 'package:shiny':
## 
##     serverInfo
q <- opq(bbox = getbb("Ann Arbor, US")) %>%
  add_osm_features(features = c(
    "amenity" = "restaurant",
    "amenity" = "bar",
    "amenity" = "biergarden",
    "amenity" = "fast_food",
    "amenity" = "food_court",
    "amenity" = "cafe",
    "amenity" = "pub",
    "amenity" = "ice_cream"
  )) %>% 
  osmdata_sf() 

amenity_point <- q$osm_points %>% 
  select(osm_id, name, amenity, geometry) %>% 
  drop_na(name, amenity)
get_amenity_icon <- function(amenity_type) { 
  icon_urls <- c(
    "pub" = "pub.png", 
    "cafe" = "cafe.png", 
    "bar" = "bar.png", 
    "restaurant" = "restaurant.png", 
    "fast_food" = "fastfood.png", 
    "ice_cream" = "icecream.png")
  makeIcon(
    iconUrl = icon_urls[amenity_type], 
    iconWidth = 28, iconHeight = 28, iconAnchorX = 16, iconAnchorY = 28, popupAnchorX = 0, popupAnchorY = -28)} 

leaflet(data = amenity_point) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addMarkers(
    lng = st_coordinates(amenity_point)[, 1], 
    lat = st_coordinates(amenity_point)[, 2], 
    popup = ~paste0("<b>", name, "</b><br>", amenity),
    icon = ~get_amenity_icon(amenity)  
  ) %>%
  addLegend(
    "bottomright", 
    colors = c("red", "blue", "darkgreen", "orange", "brown", "purple"),
    labels = c("Pub", "Cafe", "Bar", "Restaurant", "Fast Food", "Ice Cream"),
    title = "Amenity Type",  
    opacity = 1
  ) %>%
  setView(lng = -83.7430, lat = 42.2808, zoom = 12)