Good morning, I hope that I can have some guideline, the code shows water quality data when a basin is selected, so far so good.
When I add shapefiles as bottom layers like rivers and mining areas, it happens that the legend is duplicated when you select a different basin, and if I continue selecting another basin it triples. Apparently there is a loop. Try changing the location of the shapefiles but the problem is the same, also try the command clearshpefiles (), and it also keeps duplicating. Any help or guidance, I thank you infinitely. Regards
library(shiny)
library(shinydashboard)
library(leaflet)
library(RColorBrewer)
library(formattable)
library(dplyr)
library(rgdal)
############################## DATA FRAME WATER QUALITY##############################
UH<-c("Antequera","Andamarca","Sora")
PH<-c(5.65,6.32,2.8)
latitude<-c(-18.484253,-18.484253,-18.484253)
longitude<-c(-66.868306,-66.868306,-66.868306)
TURB<-c(9,4.16,3.9)
CE<-c(3180,3105,3101)
sale_data<-data.frame(UH,PH,CE,TURB,latitude,longitude)
############################## SHAPEFILES###################################
UH_E<- readOGR('limite.shp')
PERTENENCIAS<- readOGR('concewgs.shp')
RIOS<-readOGR('riopro.shp')
############################## FUNCTION CIRCLES##############################
circle.scaler <- function(x){((x-min(x))/(max(x)-min(x)))*1500}
ui <- dashboardPage(
skin = "blue",
dashboardHeader(title = "Calidad Hidrica: Cuenca Poopo"),
dashboardSidebar(
selectInput("propertytype", "Unidad Hidrografica",choices = c("Andamarca","Antequera","Sora")),
selectInput("colorscalevar", "Parametro",choices = c("PH" = "PH","Conductividad" = "CE","Turbiedad" = "TURB")),
selectInput("circlesizevar", "Magnitud:",choices = c("Conductividad" = "CE","PH" = "PH","Turbiedad" = "TURB")),
uiOutput("PriceSlider")),
dashboardBody(fluidRow(box(width= 12, leafletOutput("map",height=800)))
)
)
server <- function(input, output, session) {
filteredData <- reactive({
sale_data[sale_data$PH >= input$priceslideroutput[1] &
sale_data$PH <= input$priceslideroutput[2] &
sale_data$UH == input$propertytype,]})
############################## COLORS PALETTE##############################
palBin <- colorBin("magma", domain = PERTENENCIAS$Area, bins = 8)
palRIOS<- colorFactor(palette = c('blue' ), domain =RIOS$Cobertura )
colorpal <- reactive({colorBin("viridis", sale_data[sale_data$UH ==input$propertytype,input$colorscalevar])})
############################## SLIDER PH##############################
output$PriceSlider <- renderUI({
sliderInput("priceslideroutput", "Rango de PH",
min(sale_data[sale_data$UH ==input$propertytype,"PH"]),
max(sale_data[sale_data$UH ==input$propertytype,"PH"]),
range(sale_data[sale_data$UH ==input$propertytype,"PH"]))})
############################## MAP##############################
output$map <- renderLeaflet({
leaflet(sale_data) %>%
addProviderTiles("Stamen.Terrain", group = "CartoDB") %>%
setView(lng=-67.093, lat=-18.770732, zoom=10)})
observe({
pal <- colorpal()
leafletProxy("map", data = filteredData()) %>% clearShapes() %>%
addPolygons(data = UH_E,weight=2,color="black",fillOpacity = 0.1,dashArray = "3",label = ~UH
,group = "Unidad_Hidrografica") %>%
addPolygons(data = RIOS,color = palRIOS(RIOS$Cobertura),weight=2,
fillColor = palRIOS(RIOS$Cobertura),fillOpacity = 0.5,
group = "Red_Drenaje",smoothFactor = 0.1)%>%
addCircles(radius = ~if(input$circlesizevar=="none"){80}
else{circle.scaler(eval(as.name(paste(input$circlesizevar))))},
weight = 0.5,
color = "#777777",
fillColor = ~pal(sale_data[sale_data$UH ==input$propertytype,input$colorscalevar]),
fillOpacity = 0.9,
popup = ~paste(case_when(input$colorscalevar == "PH" ~"PH: ",
input$colorscalevar == "CE" ~ "Conductividad: ",
input$colorscalevar == "TURB" ~ "Turbiedad: "),
if(input$colorscalevar== "PH"){currency(PH,digits=0)}
else {eval(as.name(paste(input$colorscalevar)))},
'<br>' , case_when(input$circlesizevar == "none" ~"PH: ",
input$circlesizevar == "PH" ~"PH:",
input$circlesizevar == "CE" ~ "Conductividad: "),
if(input$circlesizevar== "PH"){currency(PH,digits=0)}
else if (input$circlesizevar== "none"){CE}
else {eval(as.name(paste(input$circlesizevar)))}
)
)
})
observe({
proxy <- leafletProxy("map", data = sale_data)
proxy %>% clearControls()
pal <- colorpal()
proxy %>% leaflet::addLegend(position = "bottomleft", pal = pal,
values = ~sale_data[sale_data$UH ==input$propertytype,input$colorscalevar ],
title = case_when(input$colorscalevar == "PH" ~"PH",
input$colorscalevar == "CE" ~ "Conductividad",
input$colorscalevar == "TURB" ~ "Turbiedad: "))%>%
addPolygons(data=PERTENENCIAS,color = "#444444" ,group = "Concesiones_Mineras",
weight = 1,dashArray = "3", fillOpacity = 0.5,
fillColor = ~palBin(PERTENENCIAS$Area))%>%
############################## LEGEND##############################
leaflet::addLegend(position = "topright",group = "Concesiones_Mineras", pal = palBin, values = PERTENENCIAS$Area,title = "Concesiones Mineras(Ha)") %>%
leaflet::addLegend(position = "topright",group = "Red_Drenaje", pal = palRIOS, values = RIOS$Cobertura,title = "Red de Drenaje") %>%
addLayersControl(overlayGroups = c("Concesiones_Mineras","Red_Drenaje"),options = layersControlOptions(collapsed = FALSE), position = "topright")%>%
hideGroup(c("Red_Drenaje","Concesiones_Mineras"))
})
}
shinyApp(ui = ui, server = server)