From f466db6a9c2ce2bf9436f5368907025f8893c6d3 Mon Sep 17 00:00:00 2001 From: Rob Wiederstein Date: Mon, 9 Mar 2026 16:37:14 -0400 Subject: [PATCH] Add crosstalk linking between listings map and table --- app.R | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/app.R b/app.R index 27077d9..1095b66 100644 --- a/app.R +++ b/app.R @@ -6,10 +6,17 @@ library(sf) library(dplyr) library(leafpop) library(DT) +library(crosstalk) # load data ---- owners <- readRDS("./data/owners.rds") -listings <- readRDS("./data/listings.rds") +listings <- readRDS("./data/listings.rds") |> + arrange(price_per_sqft) |> + mutate( + price_fmt = scales::dollar(price), + ppsf_fmt = scales::dollar(price_per_sqft) + ) +shared_listings <- SharedData$new(listings, key = ~address) last_sale_date <- format(attr(owners, "last_sale_date"), "%Y-%m-%d") sbdvn <- sf::st_read("./data/plats/plats.shp") venice_bndry <- readRDS("./data/venice.rds") @@ -480,7 +487,7 @@ server <- function(input, output) { # listings map ---- output$listings_map <- renderLeaflet({ - leaflet() |> + leaflet(shared_listings) |> addProviderTiles("CartoDB.Voyager") |> addPolygons( data = sbdvn, @@ -491,16 +498,14 @@ server <- function(input, output) { label = ~sub_name ) |> addMarkers( - data = listings, - lat = ~latitude, - lng = ~longitude, - popup = ~paste0( + lat = ~latitude, + lng = ~longitude, + popup = ~paste0( "", address, "
", - "Price: ", scales::dollar(price), "
", + "Price: ", price_fmt, "
", "Sq Ft: ", sqft, "
", - "$/Sq Ft: ", scales::dollar(price_per_sqft) - ), - clusterOptions = markerClusterOptions() + "$/Sq Ft: ", ppsf_fmt + ) ) |> setView(lng = -82.362253, lat = 27.076199, zoom = 16) }) @@ -508,19 +513,17 @@ server <- function(input, output) { # listings table ---- output$listings_table <- renderDT({ datatable( - listings |> - select(listed_date, address, sqft, price, price_per_sqft) |> - arrange(price_per_sqft) |> - mutate( - price = scales::dollar(price), - price_per_sqft = scales::dollar(price_per_sqft) - ), - colnames = c("Listed", "Address", "Sq Ft", "Price", "$/Sq Ft"), + shared_listings, + colnames = c("Listed", "Address", "Sq Ft", "Price (raw)", "$/Sq Ft (raw)", + "Lat", "Lng", "Price", "$/Sq Ft"), rownames = FALSE, options = list( pageLength = 25, searching = FALSE, - dom = 't' + dom = 't', + columnDefs = list( + list(visible = FALSE, targets = c(3, 4, 5, 6)) + ) ) ) })