-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
136 lines (99 loc) · 4.09 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
library(ggplot2)
library(dplyr)
library(data.table)
library(reactable)
library(shiny)
library(shinydashboard)
library(shinyWidgets)
#browser()
data <- read.csv("./www/who_suicide_statistics.csv", header = TRUE, sep = ",")
# UI -----------------------
ui <- dashboardPage(
# Header of the app
dashboardHeader(title = "Demo appka"),
# Sidebar of the app
dashboardSidebar(
sidebarMenu(
menuItem("Suicide mortality", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Další dashboard", tabName = "next_one", icon = icon("widget"))
)
),
# Body of the app
dashboardBody(
# Add your own shiny style
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "style.css")
),
tabItems(
tabItem(tabName = "dashboard",
fluidRow(
column(6,
p(strong("Suicide mortality in the US"), style = "font-size: 20px; color: black"),
tags$div(class = "header", checked = NA,
tags$a(href = "https://www.kaggle.com/datasets/szamil/who-suicide-statistics?resource=download", "Link to the original dataset here."))),
column(2, offset = 4,
downloadButton("downloadData", label = "Download data"))
),
tags$br(),
fluidRow(
box(sliderInput("slider_years", label = "Range of years",
step = 1, sep = "", ticks = FALSE,
min = min(data$year), max = max(data$year),
value = c(min(data$year)+5, max(data$year)-5)), width = 4),
box(pickerInput("states", label = "States", choices = unique(data[[1]]), selected = data[1,1], multiple = TRUE,
options = list(
`actions-box` = TRUE,
size = 10,
`selected-text-format` = "count > 3")
), width = 4),
box(checkboxGroupInput("sex", label = "Sex", choices = unique(data[[3]]), selected = NULL), width = 4)
),
fluidRow(
box(plotOutput("distPlot", height = 350)),
box(reactableOutput("table"))
)
),
tabItem(tabName = "next_one",
h3("Nadpis další")
)
)
)
)
# SERVER -----------------------
server <- function(input, output) {
df_plot <- reactive({
df_plot <- data[data$year %in% input$slider_years[1]:input$slider_years[2] & data$country == input$states, ]
if(is.null(input$sex) == FALSE)
df_plot <- df_plot[df_plot$sex == input$sex, ]
#df_plot <- na.omit(df_plot)
#browser()
#setDT(df_plot)
df_plot <- as.data.frame(unclass(df_plot), stringsAsFactors = TRUE)
#browser()
df_plot <- df_plot %>%
group_by(year,sex) %>%
summarise(suicides_no = sum(suicides_no, na.rm = T))
})
output$distPlot <- renderPlot({
ggplot(df_plot(), aes(x = year, y = suicides_no, group = sex, color = sex))+
geom_line() +
geom_point()+
labs(title="Suicides by year and sex", x ="Year", y = "No of suicides")+
theme_minimal()
})
output$table <- renderReactable({
reactable(df_plot(),
sortable = TRUE, filterable = TRUE, height = 350, defaultPageSize = 5,
defaultColDef = colDef(align = "center")
)
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data.csv", sep = "")
},
content = function(file) {
write.csv(df_plot(), file, row.names = FALSE)
}
)
}
shinyApp(ui, server)