Making the Connection with Crosstalk

How-to
R
Shiny App
GGPlot
Rmarkdown
Leaflet
An investigation of the crosstalk library in R with Toronto Apartment data
Author

Mark Edney

Published

March 18, 2022

I recently wrote a post about creating dashboards in R which was based on the Flexdashboard library. My largest criticism was the lack of communication between visualizations on the same dashboard. This was before I learned about the Crosstalk package which adds this feature to html widgets, such as the Flexdashboard, to at least a limited degree.

Initialization

The Crosstalk package is available on CRAN and is loaded along with other important packages for this demonstration.

install.packages("crosstalk")
library(crosstalk)
library(tidyverse)
library(flexdashboard)
library(plotly)

I have decided to use a Toronto Open dataset about city audits for apartment buildings. I limited the features to only the ones that I feel will be interesting to look at. More information about the data set can be found here.

download.file("https://ckan0.cf.opendata.inter.prod-toronto.ca/dataset/4ef82789-e038-44ef-a478-a8f3590c3eb1/resource/979fb513-5186-41e9-bb23-7b5cc6b89915/download/Apartment%20Building%20Evaluation.csv", "data.csv")
df <- read_csv("data.csv") %>%
        select(lng = LONGITUDE, 
               lat = LATITUDE, 
               SCORE, 
               YEAR_BUILT, 
               SITE_ADDRESS, 
               PROPERTY_TYPE) %>% 
        slice_sample(n = 200)

The key to the crosstalk library is the SharedData functions. An object is created when a Data Frame is passed to the SharedData$new function. This is what enables communication between plots.

shared_df <- SharedData$new(df)

Dashboard Creation

The dashboard is created pretty much as previous mentioned in my dashboard post, with the exception that the shared Data Frame object is passed rather than the Data Frame.

The dashboard can include filters that are very similar to the Shiny Apt filters, with the filter_* family of functions.

filter_slider("Score", "SCORE", shared_df, ~SCORE, round = TRUE)
filter_checkbox("Property Type", "PROPERTY_TYPE", shared_df, ~PROPERTY_TYPE, inline = TRUE)

Conclusion

The Crosstalk package does add some significant connectivity to Flex Dashboards. It is relatively simple to use with some basic functions. It does have the issue of not working with aggregating data. The utility of finding the mean value of a selection is something Tableu and PowerBI are still superior at. I think that it is still a welcome improvement.

Final Dashboard

Photo by Jason Goodmanon Unsplash