-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
296 lines (273 loc) · 9.78 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Carregar pacotes necessários
library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(DT)
# Carregar o dataset
trabalhadoresAPP_2020 <- read.csv("trabalhadoresAPP_2020.csv", header = FALSE)
# Remover a primeira linha e renomear colunas
trabalhadoresAPP_2020 <- trabalhadoresAPP_2020[-1, ] %>%
select(Year = V1,
Month = V2,
UF = V3,
Idade = V12,
Sexo = V13,
`Cor ou Raca` = V14,
Escolaridade = V15,
`Tipo de Ocupacao` = V16,
`Carteira Assinada` = V17,
Funcao = V18,
`Carga horaria semanal habitual` = V19,
`Rendimentos habituais` = V22,
`Contribui para o INSS` = V23) %>%
mutate(
Idade = as.numeric(Idade),
`Rendimentos habituais` = as.numeric(as.character(`Rendimentos habituais`)), # Garantir tipo numérico
`Faixa Etaria` = cut(
Idade,
breaks = c(-Inf, 17, 29, 39, 49, 59, 69, Inf),
labels = c("Menor que 18", "18-29", "30-39", "40-49", "50-59", "60-69", "70+"),
right = TRUE
)
)
# UI - Interface gráfica
# UI layout (modificado)
ui <- dashboardPage(
skin = "blue",
dashboardHeader(title = "Trabalhadores App 2020"),
dashboardSidebar(
sidebarMenu(
menuItem("Tabela", tabName = "table", icon = icon("table")),
menuItem("Gráficos", tabName = "graphs", icon = icon("chart-bar"))
),
hr(),
h4("Filtros", style = "padding-left: 15px;"),
selectInput("year", "Ano:", choices = c("Todos", unique(trabalhadoresAPP_2020$Year))),
selectInput("month", "Mês:", choices = c("Todos", 1:12)),
uiOutput("state_filter"),
uiOutput("occupation_filter"),
selectInput("age_group", "Faixa Etária:", choices = c("Todos", levels(trabalhadoresAPP_2020$`Faixa Etaria`))),
actionButton("reset", "Redefinir filtros", icon = icon("sync")),
downloadButton("downloadData", "Baixar Dados Filtrados", style = "margin-left: 15px; margin-top: 10px;")
),
dashboardBody(
tabItems(
# Table tab
tabItem(
tabName = "table",
fluidRow(
box(
title = "Tabela de Dados",
width = 12,
solidHeader = TRUE,
status = "primary",
DTOutput("data_table")
)
)
),
# Graphs tab (modificado)
tabItem(
tabName = "graphs",
fluidRow(
box(
title = "Distribuição por Faixa Etária e Tipo de Ocupação",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_chart")
),
box(
title = "Distribuição por Sexo",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_gender")
)
),
fluidRow(
box(
title = "Distribuição por Raça",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_race")
),
box(
title = "Distribuição por Escolaridade",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_education")
)
),
fluidRow(
box(
title = "Distribuição por Unidade da Federação",
width = 12,
solidHeader = TRUE,
status = "primary",
plotlyOutput("state_bar_chart")
)
),
fluidRow(
box(
title = "Distribuição por Rendimentos Habitual (Faixa Etária)",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_income_age")
),
box(
title = "Distribuição por Rendimentos Habitual (Sexo)",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_income_gender")
)
),
fluidRow(
box(
title = "Distribuição por Rendimentos Habitual (Raça)",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_income_race")
),
box(
title = "Distribuição por Rendimentos Habitual (Escolaridade)",
width = 6,
solidHeader = TRUE,
status = "primary",
plotlyOutput("bar_income_education")
)
)
)
)
)
)
# Server logic (modificado)
server <- function(input, output, session) {
output$state_filter <- renderUI({
states <- unique(trabalhadoresAPP_2020$UF)
selectInput("state", "Unidade da Federação (UF):", choices = c("Todos", states))
})
output$occupation_filter <- renderUI({
occupations <- unique(trabalhadoresAPP_2020$`Tipo de Ocupacao`)
selectInput("occupation", "Tipo de Ocupação:", choices = c("Todos", occupations))
})
filtered_data <- reactive({
data <- trabalhadoresAPP_2020
if (input$year != "Todos") data <- data %>% filter(Year == as.numeric(input$year))
if (input$month != "Todos") data <- data %>% filter(Month == as.numeric(input$month))
if (input$state != "Todos") data <- data %>% filter(UF == input$state)
if (input$occupation != "Todos") data <- data %>% filter(`Tipo de Ocupacao` == input$occupation)
if (input$age_group != "Todos") data <- data %>% filter(`Faixa Etaria` == input$age_group)
data
})
output$data_table <- renderDT({
datatable(filtered_data(), options = list(pageLength = 10), rownames = FALSE)
})
# Gráfico de Faixa Etária e Tipo de Ocupação
output$bar_chart <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = `Faixa Etaria`, fill = `Tipo de Ocupacao`)) +
geom_bar(position = "dodge") +
theme_minimal() +
labs(title = "Distribuição por Faixa Etária e Tipo de Ocupação", x = "Faixa Etária", y = "Contagem")
ggplotly(gg)
})
# Gráfico de distribuição por Sexo
output$bar_gender <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = Sexo, fill = Sexo)) +
geom_bar() +
theme_minimal() +
labs(title = "Distribuição por Sexo", x = "Sexo", y = "Contagem")
ggplotly(gg)
})
# Gráfico de distribuição por Raça
output$bar_race <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = `Cor ou Raca`, fill = `Cor ou Raca`)) +
geom_bar() +
theme_minimal() +
labs(title = "Distribuição por Raça", x = "Raça", y = "Contagem")
ggplotly(gg)
})
# Gráfico de distribuição por Escolaridade
output$bar_education <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = Escolaridade, fill = Escolaridade)) +
geom_bar() +
theme_minimal() +
labs(title = "Distribuição por Escolaridade", x = "Escolaridade", y = "Contagem")
ggplotly(gg)
})
# Gráfico de distribuição por Unidade da Federação
output$state_bar_chart <- renderPlotly({
data <- filtered_data() %>% group_by(UF) %>% summarise(Count = n())
gg <- ggplot(data, aes(x = reorder(UF, Count), y = Count)) +
geom_bar(stat = "identity", fill = "steelblue") +
coord_flip() +
theme_minimal() +
labs(title = "Distribuição por Unidade da Federação", x = "UF", y = "Contagem")
ggplotly(gg)
})
# Gráfico de distribuição por "Rendimentos Habitual" e Faixa Etária
output$bar_income_age <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = `Faixa Etaria`, y = `Rendimentos habituais`, fill = `Faixa Etaria`)) +
geom_bar(stat = "identity", position = "dodge") +
theme_minimal() +
labs(title = "Distribuição por Rendimentos Habitual e Faixa Etária", x = "Faixa Etária", y = "Rendimentos Habitual")
ggplotly(gg)
})
# Gráfico de distribuição por "Rendimentos Habitual" e Sexo
output$bar_income_gender <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = Sexo, y = `Rendimentos habituais`, fill = Sexo)) +
geom_bar(stat = "identity", position = "dodge") +
theme_minimal() +
labs(title = "Distribuição por Rendimentos Habitual e Sexo", x = "Sexo", y = "Rendimentos Habitual")
ggplotly(gg)
})
# Gráfico de distribuição por "Rendimentos Habitual" e Raça
output$bar_income_race <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = `Cor ou Raca`, y = `Rendimentos habituais`, fill = `Cor ou Raca`)) +
geom_bar(stat = "identity", position = "dodge") +
theme_minimal() +
labs(title = "Distribuição por Rendimentos Habitual e Raça", x = "Raça", y = "Rendimentos Habitual")
ggplotly(gg)
})
# Gráfico de distribuição por "Rendimentos Habitual" e Escolaridade
output$bar_income_education <- renderPlotly({
data <- filtered_data()
gg <- ggplot(data, aes(x = Escolaridade, y = `Rendimentos habituais`, fill = Escolaridade)) +
geom_bar(stat = "identity", position = "dodge") +
theme_minimal() +
labs(title = "Distribuição por Rendimentos Habitual e Escolaridade", x = "Escolaridade", y = "Rendimentos Habitual")
ggplotly(gg)
})
# Download de dados filtrados
output$downloadData <- downloadHandler(
filename = function() {
paste("dados_filtrados.csv")
},
content = function(file) {
write.csv(filtered_data(), file)
}
)
# Resetar filtros
observeEvent(input$reset, {
updateSelectInput(session, "year", selected = "Todos")
updateSelectInput(session, "month", selected = "Todos")
updateSelectInput(session, "state", selected = "Todos")
updateSelectInput(session, "occupation", selected = "Todos")
updateSelectInput(session, "age_group", selected = "Todos")
})
}
# Run the application
shinyApp(ui = ui, server = server)