lecy / neighborhood_change_phx

https://lecy.github.io/neighborhood_change_phx/
1 stars 3 forks source link

Shiny Widget Bar Chart Attempt #18

Open kendellebrown opened 5 years ago

kendellebrown commented 5 years ago

Hi Dr. Lecy,

I'm running into an issue with rendering our bar chart reactive with the error 'argument "height" is missing, with no default'. When I attempted to add parameters for height it shot back another error saying it needed to be a vector or a matrix.

If/when we fix the height issue, is my reactive code correct?


title: "Bar Chart Shiny Attempt" author: "Kendelle Brown, Abby Henderson" date: "April 28, 2019" output: flexdashboard::flex_dashboard runtime: shiny

Inputs {.sidebar}


selectInput( 
             inputId='year', 
             label='Check Year', 
             choices=c("1990" = 1990, "2000" = 2000, "2010" = 2010, "2015" = 2015),
             selected="1990"
           )

Column

Chart 1

# Data

H <- c(983403,1321045,1445632,1563025)
M <- c("1990","2000","2010","2015")
# Plot the bar chart 

renderPlot({

barplot(
  label=input$year,
  names.arg=M, 
  ylim=range(900000:1600000), 
  xpd=FALSE, 
  ylab="Population Growth",
  col="darksalmon", xlab="Year",
  main="Population Growth by Year",
  border="darksalmon")

})

Chart 2


renderPlot({

    plot.new()
    plot.window( xlim=c(0,10), ylim=c(0,10) )
    box()

    text( 5, 5, label=input$year, cex=4 )

    title( main="Year", line=-2 )

})
aehende1 commented 5 years ago

I've made some progress- we now have a reactive bar plot (yay!) but it only works as its own flexdashboard. When I input the code (below) into our overall dashboard, I get the attached error (see image). Dr. Lecy- do you have any suggestions of how to fix this?

This is the code of the tab as a whole

Total{data-orientation=columns} =====================================

URL <- "https://raw.githubusercontent.com/lecy/neighborhood_change_phx/master/data/total.dat.csv"

total.dat <- read.csv( URL, stringsAsFactors=FALSE )

Inputs {.sidebar}


# shiny inputs defined here
# Reactive function
selectInput("clusterNum", label = "Check Year", choices = list("1990" = 1990, "2000" = 2000, "2010" = 2010, "2015" = 2015), selected = "1990" )

Column {data-width=500}

Total Population Count

# renderPlot

H <- c(983403,1321045,1445632,1563025)

M <- c("1990","2000","2010","2015")

barplot(H, names.arg=M, ylim=range(900000:1600000), xpd=FALSE, ylab="Population Growth",col="darksalmon", xlab="Year",

main="Population Growth by Year",border="darksalmon")

Total Population [Count]([url](url

Screen Shot 2019-04-28 at 5 25 07 PM ))

selectedData <- reactive({
  total.dat[population, input$clusterNum]
  })

renderPlot({
 ggplot(selectedData(), aes_string(x=input$clusterNum,
                            y=total.dat$population)) +
    geom_bar(stat="identity", fill="salmon",
             position=position_dodge()) +
    xlim(1980, 2025) +
    theme_minimal()
})
lecy commented 5 years ago

Can you share the full RMD file with this code? I would need a little more information to try and identify the problem.

aehende1 commented 5 years ago

The full RMD file is in this Github repository at Capstone Dashboard/dashboard_draft.Rmd. I am no longer receiving the height error- so that part seems to be fixed. Now the error is that the newly created reactive function "selectedData()" does not read into the renderPlot() function.

lecy commented 5 years ago

You had a couple of problems:

  1. Your selectInput() function was setting years as characters, but your data frame had them as numbers.
  2. Your subset function was using arguments incorrectly (you need a logical statement to select rows, and you were referencing columns by their values and not their name).

Try this:



---
title: "PEDA Capstone"
output:
   flexdashboard::flex_dashboard:
    theme: spacelab
    social: menu
    source_code: embed
    smart: false
runtime: shiny
---

```{r setup, chunk-1, include=FALSE, cache=FALSE}

knitr::opts_chunk$set( echo=FALSE, message=F, warning=F )

library( flexdashboard )
library( dplyr )
library( geojsonio )
library( sp )
library( shiny )
library( ggplot2 )

```

```{r, include=FALSE, cache=FALSE}
# Import Shapefile
# download.file( "https://github.com/lecy/neighborhood_change_phx/raw/master/shapefiles/tl_2010_04013_tract10.zip", 
# "maricopa-az-census_tract10.zip" )
# unzip( "maricopa-az-census_tract10.zip" )
# file.remove( "maricopa-az-census_tract10.zip" )
# dir()
# 
# library( rgdal )
# phx <- readOGR("tl_2010_04013_tract10.shp")
# head( phx@data, 10)
# plot( phx )
# 
# phx <- spTransform( phx, CRS("+proj=longlat +datum=WGS84") )
```

```{r shapefile, cache=TRUE}
url <- "https://raw.githubusercontent.com/lecy/neighborhood_change_phx/master/shapefiles/phx.2010.tracts.geojson"
phx <- geojson_read( url, method="web", what="sp" )
phx$geoid2 <- paste0( "G", phx$STATEFP10, 
                     "0", phx$COUNTYFP10, "0", phx$TRACTCE10 )
```

Narrative {data-orientation=columns}
=====================================  

Column {data-width=400}
-------------------------------------

### Maricopa County Census Tracts

```{r}
par( mar=c(0,0,4,0) )
plot( phx, main="Phoenix" )
```

### Phoenix Census Tracts

```{r}
par( mar=c(0,0,0,0) )
plot( phx, col="gray", border="white", bg="gray",
      xlim=c(-112.5, -111.5), ylim=c(33.2, 33.75) )
```

Column {data-width=600}
-------------------------------------
### Project Narrative Overview
This project is an analysis of Phoenix demographic data changes from 1990-2015 indicating potential neighborhood change. It was done as the Capstone Project for students in the Master of Program Evaluation and Data Analytics Program through the Watts College of Public Service and Community Solutions at ASU.

Although the data pulled is for all of Maricopa County, this dashboard has a specific focus on the City of Phoenix. All maps have been set to look closely at Phoenix, where the population density is greater and therefore the census tracts are smaller.

The 1990, 2000, and select 2010 data is pulled from NHGIS time-series tables available through IPUMS. Select 2010 variables and all 2015 data was collected using the censusapi() package in R. All of the source code for this project can be found on the Github page, and the final two tabs of this dashboard contain the data used in this dashboard and the original variable names associated with that data. Any questions or inquires about this project can be directed to the students responsible for its creation, with contact information available on the About Me page of the project website.

This dashboard identifies four demographic changes to consider:

* Total Phoenix Population from 1990-2015

  + Total population of Phoenix was considered because of the way the city and county have experienced growth in this time period. Specifically, the city of Phoenix grew from less than 990,000 residents in 1990 to over 1.5 million in 2015. Obviously this impacts the analysis, as the number of individuals living in the area of analysis grew by over 60% in this time period. As the city and county continue to grow, further analysis may be necessary to determine how the city changing as it grows.

* Race from 1990 - 2015

  + Race was selected for this analysis because of the changes in this time period. Specifically, there is a significant increase in the Asian population from 1990 to 2015 that can be seen with both the dot plots and the Asian Population histogram.

* Ethnicity from 1990 - 2015

  + Ethnicity was also selected for changes in this time period. As can be seen in the choropleth and histogram, the Hispanic population of Phoenix increased greatly in this time period. An interesting further analysis could include looking at the ancestry of the increasing Hispanic population to determine if that changed as the immigration policies along the Arizona/Mexico border changed and as humanitarian crises increased in South and Central America.

* Homes for Sale from 1990-2015

  + The amount of homes for sale was selected due to The Great Recession and the housing bubble bursting in the late 2000's. Arizona saw a large increase in housing developments during the housing bubble, and outlying census tracts in Maricopa County were especially impacted by the Recession. The visuals in this dashboard focus on 2000 and 2010, to see how the housing market changed amidst that crisis.

* Income from 2010-2015

  + Income was selected because of the impact it has on individuals livelihood. Specifically, this dashboard looks at the disparity between those living below the poverty line and those in the highest income bracket. As the dot plots show, the delineation between highest and lowest income brackets is not based on geography, but they appear to be comingled throughout Phoenix. As the choropleth maps showing the percentage of individuals living under the poverty line indicate, there was an increase in individuals living under the poverty line between 2010 and 2015. Finally, a donut plot shows the split of income brackets in 2015, focusing on below the poverty line, the highest income bracket, and those living in between.

Total{data-orientation=columns}
=====================================   

```{r global}
# URL <- "https://raw.githubusercontent.com/lecy/neighborhood_change_phx/master/data/total.dat.csv"
# total.dat <- read.csv( URL, stringsAsFactors=FALSE )
H <- c(983403,1321045,1445632,1563025)
M <- c("1990","2000","2010","2015")
total.dat <- data.frame( year=M, population=H, stringsAsFactors=F )
```

Inputs {.sidebar}
------------------------------
```{r}
# shiny inputs defined here
# Reactive function
selectInput( "clusterNum", 
             label = "Check Year", 
             choices = list( "1990"="1990", 
                             "2000"="2000", 
                             "2010"="2010", 
                             "2015"="2015" ), 
             selected = "1990" )
```

Column {data-width=500}
------------------------------

### Total Population Count

```{r}
setColor <- reactive({
  bar.color <- ifelse( M == input$clusterNum, "steelblue", "darksalmon" )
})

renderPlot({

  selected.color <- setColor()

  barplot( H, names.arg=M, 
           col=selected.color, 
           ylim=range(900000:1600000), 
           xpd=FALSE, 
           ylab="Population",
           xlab="Year",
           main="Population by Year",
           border="darksalmon" )

})
```

### Total Population Count

This is where the interactive shiny renderPlot will go.

```{r}
selectedData <- reactive({
  total.dat[ total.dat$year == input$clusterNum , ]
  # FOR TESTING:
  # total.dat <- total.dat[ total.dat$year == 1990 , ]
  })

renderPlot({

 dat.for.plot <- selectedData()

 ggplot( dat.for.plot,
         aes_string( x=dat.for.plot$year,
                     y=dat.for.plot$population) ) +
    geom_bar( stat="identity", 
              fill="salmon",
             position=position_dodge() ) +
    xlim( 1980, 2025 ) + theme_minimal()

})
```
aehende1 commented 5 years ago

Thank you for that- I hadn't realized those were issues. They did make it look better, but it still will only run if I copy and paste this code into a completely new dashboard. If I include the changes in our dashboard_draft.Rmd, I get the attached errors (the second is the same error we were getting yesterday). I've checked and don't see any packages missing or differences in format, so I'm not sure what could be generating these errors. I'll keep investigating. If you have any tips, let me know. error

lecy commented 5 years ago

Is your current version on GitHub? If not, attach it here as a file or send via email and I will take a look.

lecy commented 5 years ago

If I include the changes in our dashboard_draft.Rmd, I get the attached errors (the second is the same error we were getting yesterday).

To your comment on posting questions on Stack Overflow with reproducible code, it's hard to guess what updates you made to your current dashboard ;-)

aehende1 commented 5 years ago

Yes- the current version is on GitHub. The changes I made are to the "Total" tab. See below:


Total{data-orientation=columns}
=====================================   

```{r global}
# URL <- "https://raw.githubusercontent.com/lecy/neighborhood_change_phx/master/data/total.dat.csv"
# total.dat <- read.csv( URL, stringsAsFactors=FALSE )
H <- c(983403,1321045,1445632,1563025)
M <- c("1990","2000","2010","2015")
total.dat <- data.frame( year=M, population=H, stringsAsFactors=F )
```

Inputs {.sidebar}
------------------------------
```{r}
# shiny inputs defined here
# Reactive function
selectInput( "clusterNum", 
             label = "Check Year", 
             choices = list( "1990"="1990", 
                             "2000"="2000", 
                             "2010"="2010", 
                             "2015"="2015" ), 
             selected = "1990" )
```

Column {data-width=500}
------------------------------

### Total Population Count

```{r}
setColor <- reactive({
  bar.color <- ifelse( M == input$clusterNum, "steelblue", "darksalmon" )
})

renderPlot({

  selected.color <- setColor()

  barplot( H, names.arg=M, 
           col=selected.color, 
           ylim=range(900000:1600000), 
           xpd=FALSE, 
           ylab="Population",
           xlab="Year",
           main="Population by Year",
           border="darksalmon" )

})
```

### Total Population Count

This is where the interactive shiny renderPlot will go.

```{r}
selectedData <- reactive({
  total.dat[ total.dat$year == input$clusterNum , ]
  })

renderPlot({

 dat.for.plot <- selectedData()

 ggplot( dat.for.plot,
         aes_string( x=dat.for.plot$year,
                     y=dat.for.plot$population) ) +
    geom_bar( stat="identity", 
              fill="salmon",
             position=position_dodge() ) +
    xlim( 1980, 2025 ) + theme_minimal()

})
```
lecy commented 5 years ago

The updated data renamed H as population, and M as year. So the variables needed to be renamed in the code:


```{r global}
H <- c(983403,1321045,1445632,1563025)
M <- c("1990","2000","2010","2015")
total.dat <- data.frame( year=M, population=H, stringsAsFactors=F )
```

```{r}
setColor <- reactive({
  bar.color <- ifelse( total.dat$year == input$clusterNum, "steelblue", "darksalmon" )
})

renderPlot({

  selected.color <- setColor()

  barplot( total.dat$population, 
           names.arg=total.dat$year, 
           col=selected.color, 
           ylim=range(900000:1600000), 
           xpd=FALSE, 
           ylab="Population",
           xlab="Year",
           main="Population by Year",
           border="darksalmon" )
})
```
aehende1 commented 5 years ago

I fixed that error-thank you. I also edited our dataset so that the years are numeric.

The new version is uploaded to Github under the same name. However, I'm still getting the same error when I run the Dashboard. I've looked into the issue pages and it seems that it could be a package dependency problem, but changing, removing, or re-installing the packages didn't fix the issue. I also tried re-formatting the reactive functions to better match some of the examples I found but that was also unsuccessful. I'm confused as to why it will run successfully if I copy and paste the code into a new Markdown document, but I receive errors if I try to run it within the dashboard_draft Markdown. This leads me to believe it is an error in the code preceding this tab. I moved this tab ahead of the narrative to see if that would fix the issue, but no such luck.

lecy commented 5 years ago

It's a good question. Your dashboard is quite large, so unfortunately I think the easiest way to identify the problem might be to start with a new dashboard and add tabs one at a time until you break it.

Make sure all of the packages you need are loaded up front. I see a lot of library calls halfway through the file, and some of them loading packages for a second time.

I am not sure why are you including this code intermittently throughout the document:

remove(list=ls())

It will delete all of the active R objects - any data you have currently loaded as well as objects associated with shiny. I suspect that might be the problem. Why are you clearing out the environment at those points?

Also, try to load all of the data up-front, and only load it once.

For shapefiles, it should be easier to just read the geojson version from GitHub than to download and unzip the shapefiles.

url <- "https://raw.githubusercontent.com/lecy/neighborhood_change_phx/master/shapefiles/phx.2010.tracts.geojson"
phx <- geojson_read( url, method="web", what="sp" )
phx$geoid2 <- paste0( "G", phx$STATEFP10, 
                     "0", phx$COUNTYFP10, "0", phx$TRACTCE10 )

Instead of:

# Import Shapefile
# download.file( "https://github.com/lecy/neighborhood_change_phx/raw/master/shapefiles/tl_2010_04013_tract10.zip", 
# "maricopa-az-census_tract10.zip" )
# unzip( "maricopa-az-census_tract10.zip" )
# file.remove( "maricopa-az-census_tract10.zip" )
# dir()
# 
# library( rgdal )
# phx <- readOGR("tl_2010_04013_tract10.shp")
# head( phx@data, 10)
# plot( phx )
# 
# phx <- spTransform( phx, CRS("+proj=longlat +datum=WGS84") )
lecy commented 5 years ago

Last thought - are you using all of these packages? That's a lot of packages to load. If you can remove some, it will minimize dependency conflicts.

kendellebrown commented 5 years ago

I went through and removed all the "remove(list=ls())" chunks, as well as cleaned up our packages and put them in the beginning and the dashboard now loads the widget!!

However, now when it publishes to shinyio, it changes our chart dimensions under the 'Total' tab, as well as messes up our layout under the 'Race' tab. The updated version is uploaded to Github under 'dashboard_draft.Rmd'.

lecy commented 5 years ago

It was a parsing issue. You need to include spaces after hashes in your section dividers, and space between section headers and code.

Inputs {.sidebar}
------------------------------
```{r}
# some code here...
```

###White
```{r}
# some code here...
```

###Black
```{r}
# some code here...
```

Change to:

Inputs {.sidebar}
------------------------------

```{r}
# some code here...
```

### White

```{r}
# some code here...
```

###  Black

```{r}
# some code here...
```

dashboard.txt

aehende1 commented 5 years ago

Wow. Another lesson learned the hard way- thank you for clearing that up. I hope I don't make that mistake again.

I originally included the remove() function because I thought you had said the dashboard would run more quickly if we removed the datasets for each tab so that every tab was a fresh start. However, it doesn't seem to impact the speed of the dashboard. The dashboard is now fully functioning, formatting fixed, uploaded in its own folder and updated in the GitHub Pages site.