I'm attempting to add markers to a map, however for some reason the marker icon no longer renders:
Here's a reproducible example shiny app:
library(shiny)
library(tidyverse)
library(leaflet)
ui <- fluidPage(
# Application title
titlePanel("Leaflet example"),
leafletOutput("example")
)
server <- function(input, output) {
output$example <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Esri.WorldGrayCanvas) %>%
setView(lng = -71.0589, lat = 42.3601, zoom = 15) %>%
addMarkers(
lng = -71.0589,
lat = 42.3601
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Related
I'm currently using addSearchOSM() from the leaflet.extras package to search addresses:
How can I change the colour of the circle marker? Will accept CSS solutions as well - I attempted to manually update the .leaflet-interactive{} css, but that changes all interactive elements, including polygons.
Reproducible example here:
library(shiny)
library(leaflet)
library(tidyverse)
library(leaflet.extras)
ui <- fluidPage(
fluidRow(
column(
width = 12,
leafletOutput("map")
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(
lng = -73.9888,
lat = 40.72905,
zoom = 12
) %>%
addSearchOSM()
})
}
# Run the application
shinyApp(ui = ui, server = server)
I can't find any documentation on how to add a tooltip with addPulseMarkers above the Layer Control (using leaflet.extras). See below for an example of what I'd aiming to do.
library(shiny)
library(leaflet)
library(leaflet.extras)
ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet(quakes) %>%
addPulseMarkers(lng = ~long, lat = ~lat,
icon = makePulseIcon(color = "blue", heartbeat = 2),
group = "I want a tooltip on hover above this that says, 'Nice'") %>%
addLayersControl(
overlayGroups = c("I want a tooltip on hover above this that says, 'Nice'"),
options = layersControlOptions(collapsed = FALSE)
)
})
}
shinyApp(ui, server)
Do you mean like this?
library(shiny)
library(leaflet)
library(leaflet.extras)
ui <- fluidPage(
leafletOutput("map")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet(quakes) %>%
addPulseMarkers(lng = ~long, lat = ~lat,
icon = makePulseIcon(color = "blue", heartbeat = 2),
group = "I want a tooltip on hover above this that says, 'Nice'") %>%
addLayersControl(
overlayGroups = c("I want a tooltip on hover above this that says, 'Nice'"),
options = layersControlOptions(collapsed = FALSE)
) %>%
htmlwidgets::onRender("
function() {
$('.leaflet-control-layers-overlays').prepend('<label style=\"text-align:center\">Nice</label>');
}
")
})
}
shinyApp(ui, server)
It borrows from: Add title to layers control box in Leaflet using R
I want to display my marker labels based on zoom level.
Based on (https://rstudio.github.io/leaflet/shiny.html) I tried to use "input$MAPID_zoom". In my example, labels stored in location_name should be displayed when zoom level (mapscale) is lower to 6.
What I tried :
library(shiny)
library(leaflet)
# my data
df <- data.frame(
location_name = c('S1', 'S2'),
lng = c(-1.554136, -2.10401),
lat = c(47.218637, 47.218637),
stringsAsFactors = FALSE)
# UI
ui <- shinyUI(fluidPage(
leafletOutput('map')
))
# server
server <- shinyServer(function(input, output, session) {
mapscale <- observe({
input$map_zoom # get zoom level
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(data=df, lng = ~lng, lat = ~lat,
label =~if(mapscale<6, location_name))
})
})
shinyApp(ui = ui, server = server)
A few remarks on your code if you like.
If you wrap the zoom in a reactive function, reference it like mapscale(). Use the normal if statement in R and the ~ in front of the variable. Then you should be fine.
Reproducible example:
library(shiny)
library(leaflet)
df <- data.frame(
location_name = c('S1', 'S2'),
lng = c(-1.554136, -2.10401),
lat = c(47.218637, 47.218637),
stringsAsFactors = FALSE
)
ui <- shinyUI(
fluidPage(
leafletOutput(outputId = 'map')
)
)
server <- shinyServer(function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles()
})
observeEvent(
eventExpr = input$map_zoom, {
print(input$map_zoom) # Display zoom level in the console
leafletProxy(
mapId = "map",
session = session
) %>%
clearMarkers() %>%
addMarkers(
data = df,
lng = ~lng,
lat = ~lat,
label = if(input$map_zoom < 6) ~location_name
)
}
)
})
shinyApp(
ui = ui,
server = server
)
i am trying to use shiny dashboard with leaflet package. I tried using "SelectInput" function in the dashboard to create reactive map based on the input selected(geoArea).However, i am not able to make the leaflet and the SelectInput connect with each other.
I also wanted to distinguish two groups in my dataset and plot it in leaflet/shiny (column name "up.and.down" has positive and negative values).In Base R i could do it using filter option from the tidyverse package and give distinct colour to each but however i am not sure how this works in Shinydashboard. Any help in regards will be much appreciated.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(leaflet)
library(tidyverse)
datafile<- read.csv("/Users/prabeshkc/Desktop/stackoverflow data.csv")
`ui<- dashboardPage(
skin = "blue",
dashboardHeader(title = "Cluster Map"),
dashboardSidebar(
selectInput("Area",label = "Geo Area",
choices = datafile$geoArea)
),
dashboardBody(
fluidRow(box(width = 12,leafletOutput(outputId = "mymap")))
))`
`server <- function(input, output) {
data_input<-reactive({
datafile %>%
leaflet() %>%
addTiles() %>%
addMarkers(lng = datafile$lng,lat = datafile$lat)
})
data_input_ordered<-reactive({
data_input()[order(match(data_input)$geoArea)]
})`
`labels<- reactive({
paste("<p>", datafile$goals,"</p>"),
paste("<p>", datafile$achieved,"</p>")
})`
`output$mymap<- renderLeaflet(
datafile %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = datafile$lng,lat = datafile$lat)
)
}
shinyApp(ui = ui, server = server)`
Try this one:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(leaflet)
library(tidyverse)
datafile<-read.csv("/Users/prabeshkc/Desktop/stackoverflow data.csv") %>%
mutate(color=ifelse(up.and.down<0,"red","blue"))
ui<- dashboardPage(
skin = "blue",
dashboardHeader(title = "Cluster Map"),
dashboardSidebar(
selectInput("Area",label = "Geo Area",
choices = datafile$geoArea)
),
dashboardBody(
fluidRow(box(width = 12,leafletOutput(outputId = "mymap")))
))
server <- function(input, output) {
output$mymap<- renderLeaflet({
validate(need(datafile,"Add file"))
validate(need(input$Area,"Select Area"))
datafile %>%
filter(geoArea %in% input$Area) %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(lng = ~lng,lat = ~lat,color=~color,
popup = ~paste(paste0("Goals - ",goals),paste0("Achieved - ",achieved), sep="<br>"))
})
}
shinyApp(ui = ui, server = server)
I am building a shiny app where I would like to get the coordinates of a polygon from a leaflet map. Specifically, the shape is drawn using the Drawtoolbar from the leaflet.extras package. A simple example app is below.
My question is, how can I get the coordinates from the shape drawn on the map by the user? Thank you in advance.
library(shiny)
library(leaflet)
library(leaflet.extras)
# Define UI
ui <- fluidPage(
leafletOutput("mymap",height=800)
)
# Define server logic
server <- function(input, output) {
output$mymap <- renderLeaflet(
leaflet() %>%
addProviderTiles("Esri.OceanBasemap",group = "Ocean Basemap") %>%
setView(lng = -166, lat = 58.0, zoom = 5) %>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addLayersControl(overlayGroups = c('draw'), options =
layersControlOptions(collapsed=FALSE))
)
observeEvent(input$mymap_shape_click,{
print(input$mymap_shape_click)
})
observeEvent(input$mymap_click,{
print(input$mymap_click)
})
}
# Run the application
shinyApp(ui = ui, server = server)
You need to observe the _draw_new_feature function
library(leaflet.extras)
# Define UI
ui <- fluidPage(
leafletOutput("mymap",height=800)
)
# Define server logic
server <- function(input, output) {
output$mymap <- renderLeaflet(
leaflet() %>%
addProviderTiles("Esri.OceanBasemap",group = "Ocean Basemap") %>%
setView(lng = -166, lat = 58.0, zoom = 5) %>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
addLayersControl(overlayGroups = c('draw'), options =
layersControlOptions(collapsed=FALSE))
)
observeEvent(input$mymap_draw_new_feature,{
feature <- input$mymap_draw_new_feature
print(feature)
})
}
# Run the application
shinyApp(ui = ui, server = server)