Load data and assets
#load world cities dataset
cities_data <- read.csv("worldcities.csv")
#20 random jokes
jokes <- c(
"Why don’t skeletons fight? They don’t have the guts!",
"My computer keeps sending me to the beach!",
"The math teacher’s afraid of negative numbers—he’ll stop at nothing!",
"Parallel lines have so much in common. It’s a shame they never meet.",
"I only know 25 letters… I don’t know Y.",
"My dog’s bark is worse than his byte!",
"I used to play piano by ear, now I use my hands.",
"Why don’t fish play basketball? They fear the net!",
"Told my wife her eyebrows were too high. She looked surprised!",
"Why did the scarecrow win? He was outstanding in his field!",
"I’m reading a book on anti-gravity—it’s impossible to put down!",
"Why not tell secrets on a farm? Potatoes have eyes, corn has ears.",
"What’s orange and sounds like a parrot? A carrot.",
"I’m on a seafood diet. I see food, I eat it!",
"Why don’t eggs tell jokes? They crack each other up.",
"Why was the math book sad? Too many problems.",
"I’d tell a chemistry joke, but no reaction.",
"What do you call fake spaghetti? An impasta!",
"The astronaut was claustrophobic. He needed space.",
"Why don’t oysters donate? They’re shellfish!"
)
# take emojis from related folder
laugh_emojis <- paste0("laugh_emojis", "/", 1:20, ".png")
# create a base icon list for markers
jokeIcons <- iconList(
makeIcon(iconUrl = "laugh_emojis/1.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/2.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/3.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/4.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/5.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/6.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/7.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/8.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/9.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/10.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/11.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/12.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/13.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/14.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/15.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/16.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/17.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/18.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/19.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16),
makeIcon(iconUrl = "laugh_emojis/20.png", iconWidth = 31*215/230, iconHeight = 31,iconAnchorX = 31*215/230/2, iconAnchorY = 16)
)
Data wrangling to find most laughed jokes and match icons with
jokes
# 20 random jokes in world-map
jokes_df <- data.frame(lat = runif(20, min = -60, max = 60),
lng = runif(20, min = -100, max = 100),
joke = jokes)
jokes_df$icon <- jokeIcons
# select 1000 random cities from cities_data
cities <- cities_data %>%
sample_n(1000) %>%
select(country, city, lat, lng)
# function to find the nearest joke from each city
find_nearest_joke <- function(lat, lng, jokes_df) {
distances <- distHaversine(cbind(jokes_df$lng, jokes_df$lat), c(lng, lat))
joke <- jokes[which.min(distances)]
icon <- jokeIcons[[which.min(distances)]]
return(list(joke = joke, icon = icon)) # Return as a named list
}
# add a new column to cities with the nearest joke
cities <- cities %>%
rowwise() %>%
mutate(joke_icon = list(find_nearest_joke(lat, lng, jokes_df)$icon),
joke_text = find_nearest_joke(lat, lng, jokes_df)$joke)
# populate icon list for city data
for(i in 1:nrow(cities)){
jokeIcons[[i]] <- cities$joke_icon[[i]]
}