library(shiny)
library(shinythemes)
library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------------------------------ tidyverse 1.3.0 --
## v ggplot2 3.3.0     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.5
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts --------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(leaflet)
library(tmap)
library(spdep)
## Loading required package: sp
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## Loading required package: sf
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(rgeos)
## rgeos version: 0.5-2, (SVN revision 621)
##  GEOS runtime version: 3.6.1-CAPI-1.10.1 
##  Linking to sp version: 1.4-1 
##  Polygon checking: TRUE
 library(sf)
 library(sp)
library(spdep)
library(rgdal)
## rgdal: version: 1.4-8, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/Administrator/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/Administrator/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.4-1
#jy
library(crosstalk)
## 
## Attaching package: 'crosstalk'
## The following object is masked from 'package:shiny':
## 
##     getDefaultReactiveDomain
library(ggExtra)
## 
## Attaching package: 'ggExtra'
## The following object is masked from 'package:shiny':
## 
##     runExample
library(ggplot2)
library(DT)
## 
## Attaching package: 'DT'
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
#jy
load("data/test_data.rda")
data_ward_df <- mpsz_ward_data %>% st_drop_geometry()

Shared data

shared_data <- SharedData$new(data_ward_df)

scatterplot with boxplot

p1 <- ggplot(shared_data, aes(data_ward_df$fat, data_ward_df$fibre, fill="black", group=data_ward_df$ward_name)) +
      geom_point(shape=22) +
      theme(legend.title = element_blank(), legend.position="none")

p <- ggMarginal(p1, type="boxplot")
p

Trying ggplotly

ggplotly(p1, tooltip=c("data_ward_df$ward_name"))
p

Trying ggplotly with tooltips

bscols(
  ggplotly(p1, tooltip=c("data_ward_df$ward_name"))
 )

Linking map to data table

shared_data <- SharedData$new(data_ward_df)

bscols(
  leaflet(shared_data, width="100%", height=300) %>%
    addTiles() %>%
    addMarkers(lng=data_ward_df$long, lat=data_ward_df$lat),
  datatable(shared_data, width="100%", class="compact", extensions="Scroller",
             options=list(deferRender=TRUE, scrollY=300, scroller=TRUE))
 )

Linking charts (scatterplot, boxplot, graph)

scatter1 <- plot_ly(shared_data, x=~fat, y=~fibre, tooltip = ~ward_name) %>%
        add_markers()

bp_y <- plot_ly(shared_data, y = ~fibre) %>%
      add_boxplot()

bp_x <- plot_ly(shared_data, x = ~fat) %>%
      add_boxplot()

subplot(nrows = 2,
  heights = c(.3, .7),
  widths = c(7/10, 3/10),
  bp_x, plotly_empty(),
  scatter1, bp_y
)
## Warning: No trace type specified and no positional attributes specified
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
## Warning: 'scatter' objects don't have these attributes: 'tooltip'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'stackgroup', 'orientation', 'groupnorm', 'stackgaps', 'text', 'texttemplate', 'hovertext', 'mode', 'hoveron', 'hovertemplate', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'texttemplatesrc', 'hovertextsrc', 'hovertemplatesrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: Can only have one: highlight
## Warning: 'scatter' objects don't have these attributes: 'tooltip'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'meta', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'stackgroup', 'orientation', 'groupnorm', 'stackgaps', 'text', 'texttemplate', 'hovertext', 'mode', 'hoveron', 'hovertemplate', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'metasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'texttemplatesrc', 'hovertextsrc', 'hovertemplatesrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

linking map and charts

scatter1 <- plot_ly(shared_data, x=~fat, y=~fibre, tooltip = ~ward_name) %>%
        add_markers()

bp_y <- plot_ly(shared_data, y = ~fibre, type="box", boxpoints="all") %>%
        layout(boxmode = "group")

bp_x <- plot_ly(shared_data, x = ~fat, type="box", boxpoints="all") %>%
        layout(boxmode = "group")

map <- leaflet(shared_data, width="100%", height=300) %>%
    addTiles() %>%
    addMarkers(lng=data_ward_df$long, lat=data_ward_df$lat)

filter_slider("popden", 
                            "Population Density",
                            shared_data,
                            ~people_per_sq_km,
                            width="100%")
bscols(
  map,
  subplot(nrows = 2,
    heights = c(.3, .7),
    widths = c(7/10, 3/10),
    bp_x, plotly_empty(),
    scatter1, bp_y
  )
)

0