## Warning: package 'flexdashboard' was built under R version 4.2.3
## Warning: package 'shiny' was built under R version 4.2.3
## Warning: package 'rsconnect' was built under R version 4.2.3
## 
## Attaching package: 'rsconnect'
## The following object is masked from 'package:shiny':
## 
##     serverInfo
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## Warning: package 'DT' was built under R version 4.2.3
## 
## Attaching package: 'DT'
## The following objects are masked from 'package:shiny':
## 
##     dataTableOutput, renderDataTable
## Warning: package 'pander' was built under R version 4.2.3
## 
## Attaching package: 'pander'
## The following object is masked from 'package:shiny':
## 
##     p
## Warning: package 'knitr' was built under R version 4.2.3
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
## Warning: package 'ggmap' was built under R version 4.2.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.2.3
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
## Warning: package 'leaflet' was built under R version 4.2.3
## Warning: package 'viridis' was built under R version 4.2.3
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.2.3
## Warning: package 'geojsonio' was built under R version 4.2.3
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
## 
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
## 
##     pretty
## Warning: package 'sp' was built under R version 4.2.3
## Warning: package 'sf' was built under R version 4.2.3
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
## Warning: package 'tmap' was built under R version 4.2.3
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
## Warning: package 'pals' was built under R version 4.2.3
## 
## Attaching package: 'pals'
## The following objects are masked from 'package:viridis':
## 
##     cividis, inferno, magma, plasma, turbo, viridis
## The following objects are masked from 'package:viridisLite':
## 
##     cividis, inferno, magma, plasma, turbo, viridis
## Warning: package 'rgdal' was built under R version 4.2.3
## Please note that rgdal will be retired during 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2022/04/12/evolution.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-6, (SVN revision 1201)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.5.2, released 2022/09/02
## Path to GDAL shared files: C:/Users/rjudd/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
## Path to PROJ shared files: C:/Users/rjudd/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.6-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.

Community Demographics

Row

Choropleth Map

renderPlot({

# split the selected variable into deciles 
  
get_data <- 
  reactive({
             phx.sf <- 
             phx.sf %>% 
             mutate( q = ntile( get(input$demographics), 10 ) )  
          })

  
ggplot( get_data() ) +
    geom_sf( aes( fill = q ), color=NA ) +
    coord_sf( datum=NA ) +
    labs( title = paste0( "Choropleth of Select Demographics: ", toupper(input$demographics) ),
          caption = "Source: Harmonized Census Files",
          fill = "Population Deciles" ) +
    scale_fill_gradientn( colours=rev(ocean.balance(10)), guide = "colourbar" ) + 
    xlim( xmin = -12519146, xmax = -12421368 ) + 
    ylim( ymin = 3899074, ymax = 3965924 )

})

Variable Distribution

renderPlot({

# extract vector x from the data frame 
x <-  d[ "pnhwht12" ] %>% unlist()

  
get_variable_x <- reactive({ d[ input$demographics ] })

x <- get_variable_x() %>% unlist()

cut.points <- quantile( x, seq( 0, 1, 0.1 ) )

hist( x, breaks=50, 
      col="gray", border="white", yaxt="n",
      main=paste0( "Histogram of variable ", toupper( input$demographics ) ),
      xlab="red lines represent decile cut points" )

abline( v=cut.points, col="darkred", lty=3, lwd=2 )


})

Neighborhoods

Clusters

# define the bounding box corners 
bb <- st_bbox( c( xmin = -12519146, xmax = -12421368, 
                  ymax = 3965924, ymin = 3899074 ), 
               crs = st_crs("+init=epsg:3395"))
## Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
## deprecated. It might return a CRS with a non-EPSG compliant axis order.
# ADD YOUR CUSTOM LABELS TO THE CLUSTERS 

phx2$cluster[ phx2$cluster == "1" ] <- "Baby Boomers"
phx2$cluster[ phx2$cluster == "2" ] <- "Hipsters"



renderTmap({

tmap_mode("view")
tm_basemap( "CartoDB.Positron" )
tm_shape( phx2, bbox=bb ) + 
  tm_polygons( col="cluster", palette="Accent",
               title="Community Types" ) 

})

NH Change 2000-2010

Row

Median Home Values

renderPlot({

# split the selected variable into deciles 
  
get_data <- 
  reactive({
             phx.sf <- 
             phx.sf %>% 
             mutate( q = ntile( get(input$home.value), 10 ) )  
          })

  
ggplot( get_data() ) +
    geom_sf( aes( fill = q ), color=NA ) +
    coord_sf( datum=NA ) +
    labs( title = paste0( "Spatial Distribution of Home Values: ", toupper(input$demographics) ),
          caption = "Source: Harmonized Census Files",
          fill = "Home Value Deciles" ) +
    scale_fill_gradientn( colours=rev(ocean.balance(10)), guide = "colourbar" ) + 
    xlim( xmin = -12519146, xmax = -12421368 ) + 
    ylim( ymin = 3899074, ymax = 3965924 )

})

Variable Distribution

renderPlot({

# extract vector x from the data frame 
# x <-  d[ "pnhwht12" ] %>% unlist()

  
get_variable_x <- reactive({ d[ input$home.value ] })

x <- get_variable_x() %>% unlist() %>% as.numeric()

cut.points <- quantile( x, seq( 0, 1, 0.1 ) )

hist( x, breaks=50, 
      col="gray", border="white", yaxt="n",
      main=paste0( "Histogram of ", toupper( input$home.value ) ),
      xlab="red lines represent decile cut points" )

abline( v=cut.points, col="darkred", lty=3, lwd=2 )


})

Drivers of Change

Row

Predicting Change

# RUNNING A REGRESSION WITH USER INPUTS
#
# create a formula object
# by constructing the formula from user selections 
# as a string then casting as a formula object 
x.s <- c("x1","x2","x3" )
formula.text <- paste( "y", " ~ ", paste0( x.s, collapse=" + ") )
formula.object <- as.formula( formula.text )
#
#lm( formula.object, data=d )
#
# make sure all variables are in d


  
# check boxes return vectors 
get_covariates <-  reactive({ input$covariates })


renderUI({
  
covariates <- get_covariates() 

formula.text <- paste0( "mhmval12", " ~ ", paste( covariates, collapse=" + " ) )
fo <- as.formula( formula.text )

m <- lm( fo, data=d )


# HTML( "<div style='width: 60%; margin: 0px auto;'>" )
HTML( 
  
  c("<br><br><br>",
  "<div type='regression' style='width: 60%; margin: 0px auto;'>",
  stargazer( m, type="html", omit.stat=c("rsq","f") ),
  "</div>",
  "<br><br><br>"
  )
  
  )


})
# HTML( reg.table )

Correlation Plots

pairs( iris )

```