-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHINYMPLAN_V2.R
81 lines (72 loc) · 2.24 KB
/
SHINYMPLAN_V2.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
library(shiny)
library(shinythemes)
library(ggplot2)
library(plotly)
library(leaflet)
library(DT)
distritos$id <- seq.int(nrow(distritos))
# UI ----
ui <- fluidPage(
titlePanel("Proyectos Estratégicos"),
sidebarPanel(
selectInput("estrategia",
"Proyecto:",
unique(distritos$estrategia)),
selectInput("subsistema",
"subsistema:",
unique(c(proyectos$SUBSISTEMA)),
selected = 'Sistema Municipal de Áreas de Valor Ambiental (SMAVA)')),
mainPanel(leafletOutput('map'),
dataTableOutput('tabla'))
)
# SERVER ----
server <- function(input,output){
qSub <- reactive({
subset <- subset(distritos, proyectos$SUBSISTEMA>=input$subsistema)
})
# table
output$tabla <- renderDataTable({
DT::datatable(qSub(), options=list(stateSave = TRUE))
})
# to keep track of previously selected row
prev_row <- reactiveVal()
observeEvent(input$tabla_rows_selected, {
row_selected = qSub()[input$tabla_rows_selected,]
proxy <- leafletProxy('map')
print(row_selected)
proxy %>%
addPolygons(data = distritos[input$tabla_rows_selected,],
color = 'red',
layerId = as.character(prev_row()$id))
if(!is.null(prev_row()))
{
proxy %>%
addMarkers(data = prev_row() %>%
layerId = as.character(prev_row()$id))
}
prev_row(row_selected)
})
# map
output$map <- renderLeaflet({
qMap <- leaflet() %>%
addProviderTiles("Esri.WorldImagery") %>%
addPolygons(data = distritos,
fillColor = "aliceblue",
color = "grey",
layerId = ~nombre,
highlight = highlightOptions(
weight = 5,
color = "#666",
fillOpacity = 0.7,
bringToFront = TRUE))
qMap
})
observeEvent(input$map_shape_click, {
print(input$map_shape_click)
clickId <- input$map_shape_click$id
dataTableProxy("tabla") %>%
selectRows(which(qSub()$nombre == clickId)) %>%
selectPage(which(input$tabla_rows_all == clickId) %/% input$tabla_state$length + 1)
})
}
shinyApp(ui = ui, server = server)