-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEstablecimientos_salud_API_Shiny.R
More file actions
172 lines (146 loc) · 6.93 KB
/
Establecimientos_salud_API_Shiny.R
File metadata and controls
172 lines (146 loc) · 6.93 KB
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
library(shiny)
library(leaflet)
library(dplyr)
library(httr)
library(jsonlite)
library(tidyverse)
library(shinyWidgets)
library(plotly)
library(DT)
# Obtener datos de la API
url <- "https://datos.gob.cl/api/3/action/datastore_search?resource_id=2c44d782-3365-44e3-aefb-2c8b8363a1bc&limit=17000"
response <- GET(url)
content <- content(response, "text")
data <- fromJSON(content)
establecimientos_nacional <- data$result$records
options(digits = 10)
# Reemplazar las comas con puntos y eliminar los espacios en blanco
establecimientos_nacional$LatitudGlosa <- gsub(",", ".", establecimientos_nacional$LatitudGlosa)
establecimientos_nacional$LongitudGlosa <- gsub(",", ".", establecimientos_nacional$LongitudGlosa)
# Convertir las columnas de longitud y latitud a números
establecimientos_nacional$LatitudGlosa <- as.numeric(establecimientos_nacional$LatitudGlosa)
establecimientos_nacional$LongitudGlosa <- as.numeric(establecimientos_nacional$LongitudGlosa)
colnames(establecimientos_nacional)[colnames(establecimientos_nacional) == "Nombre Oficial"] <- "NombreOficial"
# Ordenar los datos por nombre de región, servicio de salud y tipo de establecimiento
establecimientos_nacional <- establecimientos_nacional %>%
arrange(RegionGlosa, SeremiSaludGlosa_ServicioDeSaludGlosa, TipoEstablecimientoGlosa)
# Definir paleta de colores
color_palette <- c("orange", "blue", "green", "red", "purple", "yellow", "cyan", "magenta", "brown", "gray")
# Definir UI
ui <- fluidPage(
titlePanel(div(style = "text-align: center; margin-top: 30px; margin-bottom: 30px;", tags$b("Establecimientos de Salud en Chile"))),
fluidRow(
style = "display: flex; justify-content: center;", # Centramos los elementos horizontalmente
column(3,
uiOutput("region_ui"),
style = "color: gray; margin-right: 10px;"), # Reducimos el ancho de la columna
column(3,
uiOutput("servicio_salud_ui"),
style = "color: gray; margin-right: 10px;"), # Reducimos el ancho de la columna
column(3,
uiOutput("tipo_establecimiento_ui"),
style = "color: gray;") # Mantenemos el ancho de la columna original
),
tags$style(HTML("
.spacer {
margin-bottom: 30px;
}
")),
fluidRow(class = "spacer"),
fluidRow(
column(4,
plotlyOutput("plot")),
column(4,
leafletOutput("mapa")),
column(4,
dataTableOutput("table"))
),
tags$style(HTML("
.dataTables_wrapper .dataTables_filter {
display: none;
}
.dataTables_wrapper .dataTables_length {
display: none;
}
"))
)
# Definir server
server <- function(input, output, session) {
# Filtro de regiones
output$region_ui <- renderUI({
if (input$servicio_salud != "Todas") {
regiones <- unique(establecimientos_nacional$RegionGlosa[establecimientos_nacional$SeremiSaludGlosa_ServicioDeSaludGlosa == input$servicio_salud])
} else {
regiones <- unique(establecimientos_nacional$RegionGlosa)
}
pickerInput("region", "Región:",
choices = c("Todas", sort(regiones)),
selected = "Todas")
})
# Filtro de servicio de salud según la región seleccionada
output$servicio_salud_ui <- renderUI({
servicios <- unique(establecimientos_nacional$SeremiSaludGlosa_ServicioDeSaludGlosa)
selected_servicio <- if ("Servicio de Salud Metropolitano Oriente" %in% servicios) {
"Servicio de Salud Metropolitano Oriente"
} else {
"Todas"
}
pickerInput("servicio_salud", "Servicio de Salud:",
choices = c("Todas", sort(servicios)),
selected = selected_servicio)
})
# Filtro de tipo de establecimiento según la región y/o servicio de salud seleccionados
output$tipo_establecimiento_ui <- renderUI({
tipos <- unique(establecimientos_nacional$TipoEstablecimientoGlosa[
if (input$region == "Todas" & input$servicio_salud == "Todas") TRUE else
(establecimientos_nacional$RegionGlosa == input$region | input$region == "Todas") &
(establecimientos_nacional$SeremiSaludGlosa_ServicioDeSaludGlosa == input$servicio_salud | input$servicio_salud == "Todas")])
pickerInput("tipo_establecimiento", "Tipo de Establecimiento:",
choices = c("Todas", sort(tipos)),
selected = "Todas")
})
outputOptions(output, "region_ui", suspendWhenHidden = FALSE)
outputOptions(output, "servicio_salud_ui", suspendWhenHidden = FALSE)
outputOptions(output, "tipo_establecimiento_ui", suspendWhenHidden = FALSE)
output$mapa <- renderLeaflet({
filtered_data <- establecimientos_nacional %>%
filter((input$region == "Todas" | RegionGlosa == input$region) &
(input$servicio_salud == "Todas" | SeremiSaludGlosa_ServicioDeSaludGlosa == input$servicio_salud) &
(input$tipo_establecimiento == "Todas" | TipoEstablecimientoGlosa == input$tipo_establecimiento))
# Asignar un color diferente a cada nivel de atención
colors <- color_palette[as.numeric(factor(filtered_data$NivelAtencionEstabglosa))]
leaflet(data = filtered_data) %>%
addTiles() %>%
addCircleMarkers(~LongitudGlosa, ~LatitudGlosa,
color = colors,
popup = ~paste("<b>Establecimiento:</b> ", `NombreOficial`, "<br>",
"<b>Dependencia:</b> ", SeremiSaludGlosa_ServicioDeSaludGlosa, "<br>",
"<b>Nivel de atención:</b> ", NivelAtencionEstabglosa, "<br>",
"<b>Comuna:</b> ", ComunaGlosa)
)
})
output$table <- renderDataTable({
filtered_data <- establecimientos_nacional %>%
filter((input$region == "Todas" | RegionGlosa == input$region) &
(input$servicio_salud == "Todas" | SeremiSaludGlosa_ServicioDeSaludGlosa == input$servicio_salud) &
(input$tipo_establecimiento == "Todas" | TipoEstablecimientoGlosa == input$tipo_establecimiento))
filtered_data %>%
group_by(TipoEstablecimientoGlosa) %>%
summarise(Cantidad = n()) %>%
arrange(TipoEstablecimientoGlosa) %>%
rename(`Tipo de establecimiento` = TipoEstablecimientoGlosa)
}, options = list(searching = FALSE, lengthMenu = c(8, 20, 30)))
output$plot <- renderPlotly({
filtered_data <- establecimientos_nacional %>%
filter((input$region == "Todas" | RegionGlosa == input$region) &
(input$servicio_salud == "Todas" | SeremiSaludGlosa_ServicioDeSaludGlosa == input$servicio_salud) &
(input$tipo_establecimiento == "Todas" | TipoEstablecimientoGlosa == input$tipo_establecimiento))
filtered_data %>%
group_by(NivelAtencionEstabglosa) %>%
summarise(count = n()) %>%
plot_ly(labels = ~NivelAtencionEstabglosa, values = ~count, type = 'pie', marker = list(colors = color_palette)) %>%
layout(title = "Distribución de Niveles de Atención")
})
}
# Ejecutar la aplicación
shinyApp(ui = ui, server = server)