-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
92 lines (86 loc) · 2.43 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
library(shiny)
library(shinydashboard)
library(dplyr)
library(ggplot2)
library(DT)
# data manipulation
df_use <- tibble::rownames_to_column(USArrests, "State")
df_use$urban_pop_interval <- floor(df_use$UrbanPop / 20) * 20
urban_pop_filter_df <- sort(
unique(df_use$urban_pop_interval), decreasing = FALSE
)
# UI
ui <- fluidPage(dashboardPage(
dashboardHeader(title = "Shiny Dashboard demo"),
dashboardSidebar(
sidebarMenu(
menuItem("Charts Demo", tabName = "Charts"),
menuItem("Data Tables Demo", tabName = "DataTables")
)
),
dashboardBody(
tabItems(
tabItem("Charts", h1("Charts Demo"),
fluidRow(
valueBoxOutput("Box_1"),
valueBoxOutput("Box_2"),
valueBoxOutput("Box_3")
),
# 2nd row showing state wise numbers in a bar chart
fluidRow(box(plotOutput("bar3"), width = 8),
box(selectInput(
"urban_pop_filter", "UrbanPopulation:", urban_pop_filter_df), width = 4)),
# Scatter plot demonstration
fluidRow(box(plotOutput("scatter1"), width = 8),
box(selectInput
("ArrestIndex_", "Arrest Index:", c("Murder", "Assault", "Rape"))
, width = 4))),
tabItem("DataTables", h1("Data Tables Demo"),
dataTableOutput("aggTable")
))
)
)
)
# SERVER
server <- function(input, output) {
# building valuebox for murder arrests
output$Box_1 <- renderValueBox({
valueBox(
mean(df_use[["Murder"]])
, "Avg. Statewise Murder Arrests Index in US (per million)"
, color = "navy"
)
})
# building 2nd box
output$Box_2 <- renderValueBox({
valueBox(
mean(df_use[["Assault"]])
, "Avg. Statewise Assault Arrests Index (per million)"
, color = "navy"
)
})
# building 3rd box box
output$Box_3 <- renderValueBox({
valueBox(
mean(df_use[["Rape"]])
, "Avg. Statewise Rape Arrests Index (per million)"
, color = "navy"
)
})
# filtering data for the bar chart
filtered_data <- reactive({
dplyr::filter(df_use, df_use$urban_pop_interval == input$urban_pop_filter)
})
# building a bar chart
output$bar3 <- renderPlot(
ggplot(data = filtered_data(), aes(x = State, y = Assault)) +
geom_bar(stat = "identity")
)
# building a scatter plot with values
output$scatter1 <- renderPlot(plot(
df_use$UrbanPop, df_use[[input$ArrestIndex_]], main = "Scatterplot Example",
xlab = "urban populations percentage ", ylab = "arrests per million"
))
output$aggTable <- renderDataTable(df_use)
}
shinyApp(ui, server)