-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
164 lines (142 loc) · 6.91 KB
/
server.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
shinyServer(function(input, output, session) {
output$selector_y <- renderText({ input$selector_y })
output$selector_x <- renderText({ input$selector_x })
#actualizar selectores ----
#pone la lista de variables en cada selector
updateSelectizeInput(session,
inputId = "selector_y",
#choices = variables,
choices = variables,
selected = sample(variables, 1)
)
updateSelectizeInput(session,
inputId = "selector_x",
choices = variables,
selected = sample(variables, 1)
)
updateSelectizeInput(session,
inputId = "selector_size",
choices = variables,
selected = sample(variables, 1)
)
#pone las comunas en el selector de comunas
updatePickerInput(session,
inputId = "selector_comunas",
choices = casen_datos %>% select(comuna) %>% distinct() %>% pull() %>% as.character(),
selected = c("La Florida", "Puente Alto", "La Cisterna", "Cerrillos", "Ñuñoa", "Vitacura", "Providencia")
)
#actualiza los selectores de tipo para poner "no aplica" en las variables que no tienen mediana (las de conteo)
observeEvent(input$selector_x, {
if(input$selector_x %in% variables_conteo) {
updateSelectInput(session,
inputId = "tipo_elegido_x",
choices = c("No aplica" = "promedio"),
selected = c("No aplica" = "promedio"))
}
})
observeEvent(input$selector_y, {
if(input$selector_y %in% variables_conteo) {
updateSelectInput(session,
inputId = "tipo_elegido_y",
choices = c("No aplica" = "promedio"),
selected = c("No aplica" = "promedio"))
}
})
observeEvent(input$selector_size, {
if(input$selector_size %in% variables_conteo) {
updateSelectInput(session,
inputId = "tipo_elegido_size",
choices = c("No aplica" = "promedio"),
selected = c("No aplica" = "promedio"))
}
})
#gráfico ----
output$grafico <- renderPlot({
req(casen_datos)
req(input$selector_y)
#variables del gráfico ----
variable_y = input$selector_y #"ytotcorh"
variable_x = input$selector_x #"numper"
variable_col = "comuna"
variable_size = input$selector_size #"s4"
comunas_elegidas <- input$selector_comunas#c("La Florida", "Puente Alto", "Vitacura")
tipo_elegido_x = input$tipo_elegido_x #"promedio"
tipo_elegido_y = input$tipo_elegido_y #"promedio"
tipo_elegido_size = input$tipo_elegido_size #"mediana"
#estética ----
aes_mapping <- aes_string(
x = variable_x,
y = variable_y,
col = variable_col,
size = variable_size
)
#ggplot ----
p <- casen_datos %>%
#crear columnas de promedio/mediana
tidyr::pivot_wider(names_from = tipo, values_from = 2:22) %>%
#seleccionar variables y tipos elegidos
select(comuna,
paste(variable_x, tipo_elegido_x, sep="_"),
paste(variable_y, tipo_elegido_y, sep="_"),
all_of(variable_col),
paste(variable_size, tipo_elegido_size, sep="_")) %>%
rename_all(list(~ stringr::str_remove(., "_promedio"))) %>%
rename_all(list(~ stringr::str_remove(., "_mediana"))) %>%
#filtrar comunas
filter(comuna %in% comunas_elegidas) %>%
#graficar
ggplot(mapping = aes_mapping) +
geom_point(col= "white") +
geom_point(alpha=0.7) +
ggrepel::geom_text_repel(aes(label = comuna),
size = 5, point.padding = 1,
min.segment.length = 2,
show.legend = FALSE) +
#escalas
scale_size(range = c(6, 20),
labels = function(x) format(x, big.mark = ".", decimal.mark = ",")) +
scale_y_continuous(labels = function(x) format(x, big.mark = ".", decimal.mark = ",")) +
scale_x_continuous(labels = function(x) format(x, big.mark = ".", decimal.mark = ",")) +
#viridis::scale_color_viridis(discrete = TRUE, option = "magma") +
#fishualize::scale_color_fish(option = "Oncorhynchus_tshawytscha", discrete = TRUE) +
rcartocolor::scale_color_carto_d(type = "qualitative",
#palette = "Sunset"
#palette = "Vivid"
palette = "Pastel"
) +
coord_cartesian(clip = "off") +
#tema
theme_light(base_size = 18) +
theme(legend.position = "right") +
theme(axis.line = element_line(size = 1, color = "gray40", lineend = "round"),
axis.ticks = element_blank(),
panel.grid.major = element_line(size=0.5, color="gray80"),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.text = element_text(color= "gray60", size=13),
legend.text = element_text(color= "gray60", size = 13),
legend.title = element_text(color= "gray60", face = "bold", size = 14),
axis.title = element_text(color= "gray60", face = "bold", size = 14)) +
guides(size = guide_legend(override.aes = list(color = "gray60")),
col = guide_legend(override.aes = list(size = 6))) +
#etiquetas
labs(y = names(variables)[variables == variable_y], #obtener del vector nombrado
x = names(variables)[variables == variable_x],
col = "Comunas",
size = names(variables)[variables == variable_size]
)
return(p)
})
# output$alerta_repetidas <- renderText({
# if(input$selector_x == input$selector_y) {
# alerta <- "Las variables seleccionadas no pueden repetirse"
# } else if (input$selector_x == input$selector_size) {
# alerta <- "Las variables seleccionadas no pueden repetirse"
# } else if (input$selector_y == input$selector_size) {
# alerta <- "Las variables seleccionadas no pueden repetirse"
# } else {
# alerta <- ""
# }
# return(alerta)
# })
}) #fin