30 min read

Creating a DC Museum Map Using Crosstalk in R

Early in 2017, a redditor created a comprehensive list of museums in the Washington DC area. Crosstalk, a new HTML Widget in R seemed like a great tool to explore these visually.

After cleaning this spreadsheet up and adding the addresses in, I was able to create the crosstalk example below using only a few lines of R code. Be sure to install the devtools version of DT below, the CRAN package does not currently work with HTML widgets.

This map is also available as a flexdashboard website with code.

R Code


##load in museum data locally once addresses are added
df <- read.csv('df.csv', colClasses = "character")
lonlat <- geocode(df$Address)
df1<-cbind(df, lonlat)
names(df1) <- gsub("\\.", "", names(df1))

##Add Hyperlinks
df1$URL <- paste0("<a href='",df1$URL,"' target='_blank'>",df1$Name,"</a>")
df1$Cost <- as.numeric(gsub("[\\$,]", "", df1$Cost))
sd <- SharedData$new(df1)

##Add Filter/Slider Bar
  filter_checkbox("Organization", "Organization", sd, ~Organization, inline = TRUE),
  filter_slider("Cost", "Cost", sd, column=~Cost)

##Map and Table
leaflet(sd, width="100%", height=300) %>% 
addTiles() %>% 
addProviderTiles(providers$Esri.NatGeoWorldMap)  %>% 
  addMarkers(popup=paste("<b>Name:</b> ", df1$URL, "<br>", 
                        "<b>Address:</b>", df1$Address, "<br>", 
                        "<b>Cost:</b>", df1$Cost, "<br>", 
                        "<b>Summary:</b>", df1$SummaryWebsite)),

datatable(sd, width="100%", height=300, extensions="Scroller", style="bootstrap", class="compact",
options=list(initComplete = JS(
    "function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#F8F8F8', 'color': '#000'});",
"}"), deferRender=TRUE, scrollY=300, scroller=TRUE, columnDefs = list(list(visible=FALSE, targets=c(1,3,4,5,9,10,11)))))%>%