I have created a leaflet map with custom icons and have the icons in the www folder for publishing. However after I publish I am not able to see the icons. My code is as follows and it works fine in Rstudio.
oceanIcons <- iconList(
Rank1 = makeIcon("23-1.png", "ferry-18#2x.png", 36, 36),
Rank2= makeIcon("23-2.png", "danger-24#2x.png", 36, 36),
Rank3= makeIcon("23-3.png", "danger-24#2x.png", 36, 36),
Rank4= makeIcon("23-4.png", "danger-24#2x.png", 36, 36),
Rank5= makeIcon("23-5.png", "danger-24#2x.png", 36, 36),
Rank0= makeIcon("circle2.png", "danger-24#2x.png", 48, 48)
)
map = leaflet(map_df) %>% addTiles() %>% addMarkers(icon = ~oceanIcons[map_df$rank],
popup=sprintf("<strong> %s <br>Expected Volume of Bike Departures: %s </br> Distance in Miles: %s <br>
%s</br></strong>"
,map_df$st_name,map_df$outbound,map_df$distance, map_df$rank))%>% addProviderTiles(providers$CartoDB.Positron)
Is there a particular reason why this method will not work with leaflets or publishing.
You need to ensure that when you publish to shinyapps.io, that you send those files with, for example:
Related
Is it possible to make the range slider work with a scatter plot? The slider works if I change the scatter to a line plot but that does not work for me as I can not use the box select tool with line plots. The snippet below is simplified to demonstrate the issue. I suspect the embedded JavaScript is the issue but I may be wrong as it works just fine with a line plot. Thanks.
from bokeh.io import show
from bokeh.models import CustomJS, RangeSlider, Column, Row
from bokeh.plotting import figure
x = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
y = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
scatter_plot = figure(width=250, plot_height=600)
scatter_plot.scatter(x, y)
callback = CustomJS(args=dict(y_range=scatter_plot.y_range), code="""
var start = cb_obj.value
y_range.start = start[0]
y_range.end = start[1]
""")
depth_slider = RangeSlider(width=250, show_value=False, start=-20, end=120, value=(20, 80), step=20,
title="Y Scale")
depth_slider.js_on_change('value', callback)
layout = Column(Row(depth_slider), scatter_plot, )
show(layout)
There seems to be some race condition going on or a discrepancy between how Python and JS version of Bokeh work.
By default, all ranges are instances of DataRange1d class which recompute start and end when needed. In this case, it for some reason recomputes the values after you set them manually.
To fix it, specify the range manually from the get go:
scatter_plot = figure(..., y_range=(20, 80))
I want to create a R-plot that uses the font Latin Modern Roman and export it as pdf.
I've found the package extrafont that provides such additional fonts. At first, I had some issues with Ghostscript, which was not installed on my computer and after installation was not found. Providing R with the complete path to the .exe like this Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.52/bin/gswin64.exe"), however, fixed the problem. Similar to many examples I've found on the internet, I then wrote the following script*:
require("extrafont")
loadfonts(device = "win")
dat <- data.frame(x = seq(1, 20), y = c(
1, 2, 4, 5, 7, 8, 9, 11, 21, 43, 30, 91, 100, 200, 238, 490, 700, 900, 920, 1000
))
file <- paste(fig_dir, "/test.pdf", sep = "")
pdf(file = file, width = 5, height = 5*0.75)
par(family = "LM Roman 10")
plot(x~y, data = dat, main = "LaTeX Font Plot", ylab = "y-lab", xlab = "This is the x-label")
dev.off()
embed_fonts(file = file, outfile = str_replace(file, ".pdf", "_embed.pdf"))
Here, fig_dir is the directory to a folder containing the exported figures.
The first problem is a warning message telling me that the width of symbol 0x2d is unknown. Nevertheless, the .pdf is exported. The main issue, however, is the subsequent embed_fonts() command, because it simply runs forever. Unfortunately, I won't get any error messages, I just never get a result. Maybe someone has a hint where the problem could be or even faced a similar problem?
*the fonts were previously installed:
font_import(prompt = FALSE, pattern = "lmroman")
loadfonts(device = dev)
I have a data frame that contains a list of all sports venues in my state.
Here is a list of the column names in my data frame (pretty self explanatory):
[1] "City" "latitude" "longitude" "Rank" "Population" "County"
[7] "Desc"
I have created all icons below:
library(leaflet)
NHL <- makeIcon(
iconUrl = "https://www-league.nhlstatic.com/images/logos/league-dark/133-flat.svg",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorY = 16,
iconAnchorX = 31*215/230/2)
MLB <- makeIcon(
iconUrl = "https://www.mlbstatic.com/team-logos/league-on-dark/1.svg",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorY = 16,
iconAnchorX = 31*215/230/2)
MLS <-makeIcon(
iconUrl = "https://league-mp7static.mlsdigital.net/styles/non-retina_desktop_logo/s3/logo25-77x77_0.png?LzMdhn2DU4GXKEjKfJ2QYWMaQKQIk7VQ&itok=ZtYZ58tI",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorY = 16,
iconAnchorX = 31*215/230/2)
NBA <-makeIcon(
iconUrl = "https://seeklogo.net/wp-content/uploads/2014/09/NBA-logo.png",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorY = 16,
iconAnchorX = 31*215/230/2)
NFL <-makeIcon(
iconUrl = "https://static.nfl.com/static/content/public/static/wildcat/assets/img/application-shell/shield/default.svg",
iconWidth = 31*215/230,
iconHeight = 31,
iconAnchorY = 16,
iconAnchorX = 31*215/230/2)
I am trying to create a leaflet plot that highlights each sports team venue in my state by showing the relevant icon. The code below will only show a single icon (whichever is first in the icon = list)
df %>%
leaflet() %>%
addTiles() %>%
addMarkers(lat = df$Latitude, lng = df$Longitude, icon = c(MLB, NHL, NFL, MLS, NBA))
I've also created another data frame (df2; below) that contains each venue (Stadium Name) and the relevant league (NFL/NHL/etc) and I've tried to pass this to "icon = df2$League" but it does not recognize it as an object. Any ideas?
> colnames(df2)
[1] "Club" "Sport" "League" "Symbol" "Venue" "City" "Latitude"
[8] "Longitude"
You didn't provide reproducible data so I've made up some example data assuming your df has a column with the league name, which we can use to match the name of the icon:
library(leaflet)
library(sf)
# generate example data
set.seed(2020)
venues <- c('NHL', 'MLB', 'MLS', 'NBA', 'NFL')
nc <- st_read(system.file("shape/nc.shp", package="sf"))
df <- st_sample(nc, 5) %>%
st_coordinates() %>%
as.data.frame
df$league <- venues
df
#> X Y league
#> 1 -78.58785 35.94350 NHL
#> 2 -80.82830 35.88732 MLB
#> 3 -78.83967 36.11236 MLS
#> 4 -80.09532 35.01562 NBA
#> 5 -83.72636 35.33204 NFL
All we need to do is create a named iconList where the name of the icon matches the name in the "league" column of our dataframe. With ~iconSet[league] we can ensure the correct icon is being used for each point in the df.
# create iconSet
iconSet <- iconList(NHL= NHL,
MLB =MLB,
MLS = MLS,
NBA = NBA,
NFL = NFL)
# map
leaflet(df) %>%
addTiles() %>%
addMarkers(lng=~X, lat=~Y, icon = ~iconSet[league])
I would like to know if it is possible to add condition in the makeIcon() function
I have a table :
id ; lat ; long ; class1 ; class2 ; class3
And I want the icon to be different depending on these conditions :
if class1 == A, I want image1
else, if class2 == B, I want image2
else, if class3 == C, I want image 3
else, I want image4
Within makeIcon(), probably not. From the API documentation:
For the simple case of applying a single icon to a set of markers, use
makeIcon(). (link)
You probably want to use icon() which is a vector of icons that you can use to draw different icons for different data:
If you have several icons to apply that vary only by a couple of
parameters (i.e. they share the same size and anchor points but have
different URLs), use the icons() function.
Icons contains a vector with each icon image url (or any other icon property that differs among the data).
To do the logic, it might be easiest to nest an extra two ifelse statements into the icon function, something like:
iconUrl = ifelse(df$class1 == "a", "image1",
ifelse(df$class2 == "c", "image2",
ifelse(df$class3 =="x", "image3",
"some other url" #the else condition
)
)
),
Here's a minimal example that's just a slight addition to the example in the linked to api documentation:
library(leaflet)
lat<- c(57,65,60,61)
long<-c(-130,-125,-140,-135)
class1<-c("a","b","c","d")
class2<-c("b","c","d","e")
class3<-c("b","c","d","f")
df <- data.frame(lat,long,class1,class2,class3,stringsAsFactors=FALSE)
leafIcons <- icons(
iconUrl = ifelse(df$class1 == "a", "http://leafletjs.com/examples/custom-icons/leaf-green.png",
ifelse(df$class2 == "c", "http://leafletjs.com/examples/custom-icons/leaf-red.png",
ifelse(df$class3 == "d", "http://leafletjs.com/examples/custom-icons/leaf-orange.png",
"http://leafletjs.com/docs/images/logo.png"
)
)
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
leaflet(data = df) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons)
Sorry the image selection in this isn't fantastic. If you want to some other property vary depending on the data for each icon, icon size for example, you use the same process on iconWidth and/or iconHeight:
iconHeight = ifelse(df$class1 == "a", 100,
ifelse(df$class2 == "c", 200,
ifelse(df$class3 == "d", 300,
400
)
)
),
I am interested in making custom icons while using Leaflet in R.
The documentation has a nice example, but when experimenting with it, I realized that there is no way to allow the custom marker to resize when the map changes size.
Since size is one of the parameters and is fixed (iconWidth = 38, iconHeight = 95,), obviously the marker will stay consistently sized as zoom changes for map beneath it.
greenLeafIcon <- makeIcon(
iconUrl = "http://leafletjs.com/docs/images/leaf-green.png",
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "http://leafletjs.com/docs/images/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
Are there any hacks / ideas / protocols for having custom icons resize along with zoom?