Skip to content
Permalink
Browse files

finished basic dashboard

  • Loading branch information...
concannon committed Apr 10, 2019
1 parent 5611f02 commit 8ccc8332028be5bba4a5cb3665d236c9db8ff341
@@ -610,4 +610,21 @@ Changes not staged for commit:
modified: readme.rmd

no changes added to commit (use "git add" and/or "git commit -a")
]0;/home/connor/CookCo]0;connor@datasci-1: ~/CookCoconnor@datasci-1:~/CookCo$ git add .
]0;/home/connor/CookCo]0;connor@datasci-1: ~/CookCoconnor@datasci-1:~/CookCo$ git add .
]0;/home/connor/CookCo]0;connor@datasci-1: ~/CookCoconnor@datasci-1:~/CookCo$ git commit - m 'added sentence table'
[master 5611f02] added sentence table
14 files changed, 93 insertions(+), 64 deletions(-)
rewrite .Rproj.user/B8DACF1A/console06/INDEX001 (86%)
rewrite Output/Reporting/By_Screen_Year.pdf (84%)
mode change 100755 => 100644
]0;/home/connor/CookCo]0;connor@datasci-1: ~/CookCoconnor@datasci-1:~/CookCo$ git push origin master
Username for 'https://github.com': concannonc 
Password for 'https://concannon@github.com':
Counting objects: 29, done.
Compressing objects: 4% (1/24) Compressing objects: 8% (2/24) Compressing objects: 12% (3/24) Compressing objects: 16% (4/24) Compressing objects: 20% (5/24) Compressing objects: 25% (6/24) Compressing objects: 29% (7/24) Compressing objects: 33% (8/24) Compressing objects: 37% (9/24) Compressing objects: 41% (10/24) Compressing objects: 45% (11/24) Compressing objects: 50% (12/24) Compressing objects: 54% (13/24) Compressing objects: 58% (14/24) Compressing objects: 62% (15/24) Compressing objects: 66% (16/24) Compressing objects: 70% (17/24) Compressing objects: 75% (18/24) Compressing objects: 79% (19/24) Compressing objects: 83% (20/24) Compressing objects: 87% (21/24) Compressing objects: 91% (22/24) Compressing objects: 95% (23/24) Compressing objects: 100% (24/24) Compressing objects: 100% (24/24), done.
Writing objects: 3% (1/29) Writing objects: 6% (2/29) Writing objects: 10% (3/29) Writing objects: 13% (4/29) Writing objects: 17% (5/29) Writing objects: 20% (6/29) Writing objects: 24% (7/29) Writing objects: 27% (8/29) Writing objects: 31% (9/29) Writing objects: 34% (10/29) Writing objects: 37% (11/29) Writing objects: 41% (12/29) Writing objects: 44% (13/29) Writing objects: 48% (14/29) Writing objects: 51% (15/29) Writing objects: 55% (16/29) Writing objects: 58% (17/29) Writing objects: 62% (18/29) Writing objects: 65% (19/29) Writing objects: 68% (20/29) Writing objects: 72% (21/29) Writing objects: 75% (22/29) Writing objects: 79% (23/29) Writing objects: 82% (24/29) Writing objects: 86% (25/29) Writing objects: 89% (26/29) Writing objects: 93% (27/29) Writing objects: 96% (28/29) Writing objects: 100% (29/29) Writing objects: 100% (29/29), 122.44 KiB | 20.41 MiB/s, done.
Total 29 (delta 12), reused 0 (delta 0)
remote: Resolving deltas: 0% (0/12) remote: Resolving deltas: 8% (1/12) remote: Resolving deltas: 16% (2/12) remote: Resolving deltas: 25% (3/12) remote: Resolving deltas: 33% (4/12) remote: Resolving deltas: 41% (5/12) remote: Resolving deltas: 50% (6/12) remote: Resolving deltas: 58% (7/12) remote: Resolving deltas: 66% (8/12) remote: Resolving deltas: 75% (9/12) remote: Resolving deltas: 83% (10/12) remote: Resolving deltas: 91% (11/12) remote: Resolving deltas: 100% (12/12) remote: Resolving deltas: 100% (12/12), completed with 12 local objects.
To https://github.com/concannon/CookCo.git
4bbb687..5611f02 master -> master
]0;/home/connor/CookCo]0;connor@datasci-1: ~/CookCoconnor@datasci-1:~/CookCo$
@@ -15,17 +15,17 @@
"MAKE" : "make",
"OLDPWD" : "/home/connor",
"PAGER" : "/usr/bin/pager",
"PATH" : "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home:bin/msys-ssh-1000-18:/snap/bin:/home:bin/msys-ssh-1000-18",
"PATH" : "/home/connor/.local/bin:/home/connor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home:bin/msys-ssh-1000-18:/snap/bin:/home:bin/msys-ssh-1000-18:/home:bin/msys-ssh-1000-18",
"PROMPT_COMMAND" : "echo -ne \"\\033]0;${PWD/#${HOME}/~}\\007\"",
"PWD" : "/home/connor/Templates",
"PWD" : "/home/connor/CookCo",
"RMARKDOWN_MATHJAX_PATH" : "/usr/lib/rstudio-server/resources/mathjax-26",
"RSTUDIO" : "1",
"RSTUDIO_CONSOLE_COLOR" : "256",
"RSTUDIO_CONSOLE_WIDTH" : "100",
"RSTUDIO_HTTP_REFERER" : "http://178.128.232.146:8787/",
"RSTUDIO_PANDOC" : "/usr/lib/rstudio-server/bin/pandoc",
"RSTUDIO_PROJ_NAME" : "CookCo",
"RSTUDIO_SESSION_ID" : "a0d2746f",
"RSTUDIO_SESSION_ID" : "6bfc27cc",
"RSTUDIO_SESSION_STREAM" : "connor-d",
"RSTUDIO_TERM" : "37FAB866",
"RSTUDIO_USER_IDENTITY" : "connor",
@@ -52,7 +52,7 @@
"R_UNZIPCMD" : "/usr/bin/unzip",
"R_ZIPCMD" : "/usr/bin/zip",
"SED" : "/bin/sed",
"SHLVL" : "2",
"SHLVL" : "3",
"SSH_ASKPASS" : "rpostback-askpass",
"TAR" : "/bin/tar",
"TERM" : "xterm-256color",
@@ -1 +1 @@
[{"allow_restart":true,"alt_buffer":false,"autoclose":1,"buffered_output":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","caption":"Terminal 1","channel_id":"6146","channel_mode":1,"child_procs":false,"cols":119,"cwd":"~/CookCo","exit_code":null,"handle":"37FAB866","interaction_mode":2,"max_output_lines":1000,"restarted":true,"rows":17,"shell_type":7,"show_on_output":false,"terminal_sequence":1,"title":"/home/connor/CookCo","track_env":true,"zombie":false}]
[{"allow_restart":true,"alt_buffer":false,"autoclose":1,"buffered_output":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","caption":"Terminal 1","channel_id":"4448","channel_mode":1,"child_procs":false,"cols":119,"cwd":"~/CookCo","exit_code":null,"handle":"37FAB866","interaction_mode":2,"max_output_lines":1000,"restarted":true,"rows":17,"shell_type":7,"show_on_output":false,"terminal_sequence":1,"title":"connor@datasci-1: ~/CookCo","track_env":true,"zombie":false}]
@@ -1,3 +1,3 @@
{
"activeTab" : 1
"activeTab" : 0
}
@@ -8,7 +8,7 @@
"right" : {
"panelheight" : 823,
"splitterpos" : 407,
"topwindowstate" : "MINIMIZE",
"topwindowstate" : "NORMAL",
"windowheight" : 897
}
}
@@ -1,6 +1,6 @@
{
"TabSet1" : 0,
"TabSet2" : 4,
"TabSet2" : 0,
"TabZoom" : {
}
}
@@ -1,5 +1,5 @@
{
"chunk_output_type" : "console",
"cursorPosition" : "193,1",
"scrollLine" : "179"
"cursorPosition" : "56,47",
"scrollLine" : "46"
}
@@ -1,4 +1,5 @@
~%2FCookCo%2FOutput%2FDashboard%2Fapp.r="5C876691"
~%2FCookCo%2FOutput%2FDashboard%2Fapp_setup.r="EC2B2A7"
~%2FCookCo%2FOutput%2FReporting%2FBy%20Screen%20Year.rmd="37B80BDF"
~%2FCookCo%2FOutput%2FReporting%2FBy_Screen_Year.log="90978CE7"
~%2FCookCo%2Fdispositions.r="67170E67"
@@ -1,6 +1,6 @@
/home/connor/CookCo/Output/Dashboard/app.r="7A91E018"
/home/connor/CookCo/Output/Dashboard/app_setup.r="86934FFF"
/home/connor/CookCo/Output/Reporting/By Screen Year.rmd="412E6F4C"
/home/connor/CookCo/Output/Reporting/By_Screen_Year.log="6B68C352"
/home/connor/CookCo/exploration.r="E9B2763"
/home/connor/CookCo/readme.md="9916BF21"
/home/connor/CookCo/readme.rmd="4A79A435"
@@ -1,86 +1,205 @@
#

pkgs <- c("shinydashboard", "tidyr", "DT",
"tidyverse", "here", "DescTools", "skimr",
"lubridate", "sjmisc", "tictoc", "purrr",
"xtable", "ggalt", "knitr", "kableExtra", "janitor",
"scales", "Cairo", "dplyr", "feather")
source("app_setup.r")

options(scipen=1000)
#Report: https://www.cookcountystatesattorney.org/sites/default/files/files/documents/ccsao_2017_data_report_180220.pdf


lapply(pkgs, require, character.only = T)


file <- here::here("data", "dataset.feather")
df <- read_feather(file)


header <- dashboardHeader(title="Cook County State's Attorney",
header <- dashboardHeader(title="Cook County States Attorney",
titleWidth=300)

# Sidebar ---------------------------------------------------------------------
sidebar <- dashboardSidebar(
width=300,
sidebarMenu(
menuItem("Dashboard",tabName="dashboard", icon = icon("chart-line")),
menuItem("Filters",tabName="filters", icon = icon("users"), startExpanded = T,
sliderInput("filter_year", "Year", min=2012, max=2018, value = c(2012, 2018)),
selectizeInput("filter_event", "Case Type",choices = levels(df$EVENT), multiple = T)
)))



#DescTools::Desc(df$EVENT)
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Filters", tabName = "dashboard", icon = icon("filter"),
startExpanded = T,
sliderInput(inputId = "year", label = "Arrest Year",
min = 2012, max = 2018, value = c(2012,2018)),
selectizeInput(inputId = "charge", label = "Top Charge",
choices=c("All", sort(unique(df$`Offense_Category`))),
options = list(placeholder = "All"),
multiple = T)
)
))

# Body ------------------------------------------------------------------------
body <- dashboardBody(
tabItems(
tabItem(tabName="dashboard",
fluidRow(
column(6, plotOutput("plot1")),
column(6, plotOutput("plot2"))
),
fluidRow(
column(6, plotOutput("plot3")),
column(6, plotOutput("plot4"))
)



fluidRow(
valueBoxOutput("defendants_selected")
,valueBoxOutput("mean_age")
, valueBoxOutput("male")
),
tabBox(
title = "",
id = "tabset1", height = "100", width=12,
tabPanel("Plots", "",
fluidRow(
column(6, plotlyOutput("plot_defendants")),
column(6, plotlyOutput("plot_disp"))
),
fluidRow(
column(6,plotlyOutput("plot_chg")),
column(6,plotlyOutput("plot_sen"))
)
),
tabPanel("Table","",
fluidRow(
dataTableOutput("data_table")
)
)
)
)


# Server ----------------------------------------------------------------------
shinyApp(
ui = dashboardPage(header, sidebar, body, skin = "green"),
server = function(input, output,session) {

# Data

dataset <- reactive({ df })
# Dataset ---------------------------------------------------------------
dataset <- reactive({df})

dataset_filtered <- reactive({
dataset_filtered <- reactive({
dataset() %>%
filter(arrest_year >= input$filter_year[1],
arrest_year <= input$filter_year[2])})
{if (is.null(input$year)) .
else filter(.,`arrest_year` >= input$year[1],
`arrest_year` <= input$year[2])} %>%
{if (is.null(input$charge)) .
else filter(., Offense_Category %in% input$charge)}
})

observe({
if("All" %in% input$branch){
updateSelectizeInput(session = getDefaultReactiveDomain(),
"branch",
choices = c("All", sort(unique(df$`Home Branch`))),
options = list(placeholder = "All"))
}
})

# Value Boxes ----------------------------------------------------------

output$defendants_selected <- renderValueBox({

defendants <- nrow(dataset_filtered())
percent <- round((defendants/overall_n)*100,2)

valueBox(
prettyNum(defendants, big.mark = ","), paste0("Defendants Selected", " (", percent, "% of total )"),
icon = icon("percent"), color = "purple"
)
})

output$plot1 <- renderPlot({
qplot(mtcars$mpg)
output$mean_age <- renderValueBox({
valueBox(
round(mean(dataset_filtered()$`AGE_AT_INCIDENT`, na.rm = T),1), "Average Age",
icon = icon("users"), color = "green"
)
})

output$plot2 <- renderPlot({
qplot(mtcars$cyl)
output$male <- renderValueBox({
valueBox(
round(nrow( dataset_filtered()[!is.na(dataset_filtered()$`SENTENCE_TYPE`),])
/ nrow(dataset_filtered())*100,1)
, "Percent Sentenced",
icon = icon("line-chart"), color = "yellow"
)
})

# Plots -------------------------------------------------------------------

output$plot3 <- renderPlot({
dataset_filtered() %>%
# Defendant Count
output$plot_defendants <- renderPlotly({

p1 <- dataset_filtered() %>%
group_by(arrest_year) %>%
summarise(number = n()) %>%
ggplot(aes(arrest_year, number, group = 1))+geom_line()
summarise(Defendants = n()) %>%
ggplot(aes(arrest_year, Defendants))+
geom_bar(stat = "identity")+
theme_minimal()+
theme(legend.position = "none")+
scale_y_continuous()+
labs(title = "Defendants by Year",
y = "# of Defendants",
x = "Arrest Year")

ggplotly(p1, tooltip = c("y", "label"))
})

# Dispositions
output$plot_disp <- renderPlotly({

p2<- dataset_filtered() %>%
filter(disposed.x == 1) %>%
group_by(Disposition = disp) %>%
summarise(Defendants = n()) %>%
mutate(Percent = Defendants/sum(Defendants)) %>%
ggplot(aes(reorder(Disposition, Percent), Percent))+
geom_bar(stat = "identity")+
coord_flip()+
theme_minimal()+

theme(legend.position = "none")+
scale_y_continuous(labels = scales::percent)+
labs(title = "Disposition",
y = "Percent of Defendants",
x = "Disposition")

ggplotly(p2, tooltip = c("y", "label"))

})

# Common Charges
output$plot_chg <- renderPlotly({

p3 <- dataset_filtered() %>%
group_by(`Category` = Offense_Category) %>%
summarise(Defendants = n()) %>%
mutate(Percent = Defendants/sum(Defendants)) %>%
top_n(n = 15, wt = Percent) %>%
ggplot(aes(reorder(Category, Percent), Percent))+
geom_bar(stat = "identity")+
coord_flip()+
theme_minimal()+
theme(legend.position = "none")+
scale_y_continuous(labels = scales::percent)+
labs(title = "Most Common Charges",
y = "Percent of Defendants",
x = "Offense Category")

ggplotly(p3, tooltip = c("y", "label"))

})

# Sentences
output$plot_sen <- renderPlotly({

p4 <- dataset_filtered() %>%
filter(!is.na(SENTENCE_TYPE)) %>%
group_by(Sentence = SENTENCE_TYPE) %>%
summarise(Defendants = n()) %>%
mutate(Percent = Defendants/sum(Defendants)) %>%
ggplot(aes(reorder(Sentence, Percent), Percent))+
geom_bar(stat = "identity")+
coord_flip()+
theme_minimal()+

theme(legend.position = "none")+
scale_y_continuous(labels = scales::percent)+
labs(title = "Sentences",
y = "Percent of Defendants",
x = "Sentence")

ggplotly(p4, tooltip = c("y", "label"))

})

# Data Export ----------------------------------------------------------
output$data_table <- renderDataTable({
dataset_filtered() %>%
head(100)
},
#caption = "Selected Defendant List (n=100)",
options = list(dom = 'Bfrtip',pageLength=10,scrollX=TRUE,
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')))

})

0 comments on commit 8ccc833

Please sign in to comment.
You can’t perform that action at this time.