R Markdown - Visualizing Leaflet map after knitting to HTML

Long story short after searching for many hours, I've finally figured out how to render my code and have successfully knitted this markdown file to html. That being said, although the map is visible within the RStudio IDEA, I can't successfully save it as an html or view it whatsoever. I've included all of my code for reproducibility. Anyway, am fairly new to R and would very much appreciate the help. Thanks!

If you follow the code, the line that produces the map is "denver_leaflet".


title: "Denver Crime Study"
author: "B.L"
date: "9 May 2022"
output:
html_document:
variant: gfm
preserve_yaml: yes
pdf_document: default
always_allow_html: yes
leafletmap: yes

chooseCRANmirror(graphics=FALSE, ind=1)

library(readr)
library(tidyverse)
library(lubridate)
library(DT)
library(leaflet)
library(htmltools)
library(sf)
library(sp)
library(ggrepel)
library(OpenStreetMap)
library(knitr)
library(leaflet.extras)
library(maptools)
library(htmlwidgets)
library(rgdal)
library(widgetframe)
library(mapview)
library(flexdashboard)
library(dplyr)
library(rnaturalearth)
library(ggmap)
library(rmarkdown)

remotes::install_github("ramnathv/htmlwidgets")
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, cache = TRUE, eval = FALSE)
Sys.setenv(PATH = paste(Sys.getenv("PATH"), "path_to_pandoc", sep=.Platform$path.sep))

denver_crime_dataset_2 <- read_csv("crime-2.csv")
denver_offense_codes <- read_csv("offense_codes.csv")

denver_crime_revised_2 <- as_tibble(denver_crime_dataset_2)
str(denver_crime_revised_2)

denver_offense_codes_revised <- as_tibble(denver_offense_codes)
str(denver_offense_codes_revised)

denver_crimes_codes_joined_2 <- inner_join(denver_crime_revised_2, y =  denver_offense_codes_revised, by = c("OFFENSE_CODE", "OFFENSE_CODE_EXTENSION", "OFFENSE_TYPE_ID","OFFENSE_CATEGORY_ID", "IS_CRIME", "IS_TRAFFIC"))

denver_crime_datetime_separate_2 <- denver_crimes_codes_joined_2 %>%
  mutate(FIRST_OCCURRENCE_DATE= mdy_hms(FIRST_OCCURRENCE_DATE),
         day = day(FIRST_OCCURRENCE_DATE),
         month = month(FIRST_OCCURRENCE_DATE),
         year = year(FIRST_OCCURRENCE_DATE),
         dayofweek = wday(FIRST_OCCURRENCE_DATE),
         minute = wday(FIRST_OCCURRENCE_DATE),
         second = second(FIRST_OCCURRENCE_DATE))
denver_crime_datetime_separate_2$date <- as.Date(denver_crime_datetime_separate_2$FIRST_OCCURRENCE_DATE) 

denver_crime_datetime_separate_2$time <- format(as.POSIXct(denver_crime_datetime_separate_2$FIRST_OCCURRENCE_DATE),    
                                                format = "%H:%M:%S")

first_500_denver_crime_datetime_separate_2 <- denver_crime_datetime_separate_2[1:500,]

first_500_denver_crime_datetime_separate_2$time <- as.character(first_500_denver_crime_datetime_separate_2$time) 
first_500_denver_crime_datetime_separate_2

datatable(first_500_denver_crime_datetime_separate_2, 
          options = list(pageLength = 25,scrollX='400px'))

denver_crime_datetime_separate_2 <- denver_crime_datetime_separate_2 %>%
  filter(GEO_LON >= -105.3218 & GEO_LON <= -104.6096839)

denver_cols_map_crime<- denver_crime_datetime_separate_2 %>%
  filter(IS_TRAFFIC == 0)
denver_cols_map_crime$summary_box <- paste("<b>Incident #: </b>", denver_cols_map_crime$incident_id,
                                           "<br>", "<b>Incident Address: </b>", denver_cols_map_crime$INCIDENT_ADDRESS,
                                           "<br>", "<b>Category: </b>", denver_cols_map_crime$OFFENSE_CATEGORY_ID,
                                           "<br>", "<b>Day of the week: </b>", denver_cols_map_crime$dayofweek,
                                           "<br>", "<b>Date: </b>", denver_cols_map_crime$date,
                                           "<br>", "<b>Time: </b>", denver_cols_map_crime$time,
                                           "<br>", "<b>Denver Neighborhood: </b>", denver_cols_map_crime$NEIGHBORHOOD_ID,
                                           "<br>", "<b>Denver Police district ID #: </b>", denver_cols_map_crime$DISTRICT_ID,
                                           "<br>", "<b>Longitude: </b>", denver_cols_map_crime$GEO_LON,
                                           "<br>", "<b>Latitude: </b>", denver_cols_map_crime$GEO_LAT)
library(leaflet)
denver_leaflet <- 
        leaflet(denver_cols_map_crime, width = "100%") %>% 
        addTiles() %>%
        addTiles(group = "OSM (default)") %>%
        addProviderTiles(provider = "Esri.WorldStreetMap",group = "World StreetMap") %>%
        addProviderTiles(provider = "Esri.WorldImagery",group = "World Imagery") %>%
        addMarkers(lng = ~GEO_LON, lat = ~GEO_LAT, popup = denver_cols_map_crime$summary_box, clusterOptions = markerClusterOptions()) %>%
        addLayersControl(
                baseGroups = c("OSM (default)","World StreetMap", "World Imagery"), options = layersControlOptions(collapsed = FALSE))   


denver_leaflet

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.