I am trying to add some markers to a leaflet map in R based on conditions that I select from an selectInput
command. The selectInput
command lets the user choose three different numbers (1, 2 or 4). Based on this number the respective marker should be added to the leaflet map. In the end I am attempting to only add one marker based on the selected number of selectInput
.
My problem: I am trying to use ifelse()
statements, but I get an error that the argument could not be interpreted as a logical condition. I am assuming that this might be linked to the piping of my leaflet command? My code with some sample data is below.
library(shiny)
# create some sample data containing lat/lon coordinates
data1 <- as.data.frame(cbind(c(46.5728, 46.5729, 46.5730, 46.5731),c(6.9385,6.9386,6.9387, 6.9388)))
colnames(data1) <- c("Lat", "Lon")
data2 <- as.data.frame(cbind(c(46.4728, 46.4729, 46.4730, 46.4731),c(6.8385,6.8386,6.8387, 6.8388)))
colnames(data2) <- c("Lat", "Lon")
data4 <- as.data.frame(cbind(c(46.3728, 46.3729, 46.3730, 46.3731),c(6.7385,6.7386,6.7387, 6.7388)))
colnames(data2) <- c("Lat", "Lon")
ui <- fluidPage(
tabsetPanel(tabPanel("TEST PANEL",
fluidRow(leafletOutput("Map", height = 500),
wellPanel(fluidRow(selectInput("Number", "Select number:", c(1,2,4))))))))
server <- function(input,output, session){
output$Map <- renderLeaflet({
m <- leaflet() %>%
addProviderTiles('Esri.WorldImagery', options = providerTileOptions(maxNativeZoom=19,maxZoom=100)) %>%
if(input$map == "1"){
addCircleMarkers(data = data1,
lat = data1$Lat,
lng = data1$Lon,
radius = 4, col = rgb(215,48,39, max = 255))
}
if(input$map == "2"){
addCircleMarkers(data = data2,
lat = data2$Lat,
lng = data2$Lon,
radius = 4, col = rgb(233,25,78, max = 255))
}
if(input$map == "4"){
addCircleMarkers(data = data4,
lat = data4$Lat,
lng = data4$Lon,
radius = 4, col = rgb(23,25,78, max = 255))
}
addScaleBar(position = "bottomleft", options = scaleBarOptions()) %>%
leaflet::addLegend("topright",
colors =c(rgb(215,48,39, max = 255), rgb(233,25,78, max = 255), rgb(23,25,78, max = 255)),
labels= c("No 1", "No 2","No 4"),
title= "Number",
opacity = 1) %>%
addMeasure(primaryLengthUnit = "meters", decPoint = ".")
m
})
}
shinyApp(ui, server)