Icons not loading (empty image) in R Leaflet with

2019-08-04 19:56发布

[R-3.4.3 64-bit, RStudio, shinydashboard_0.6.1, shiny_1.0.5, leaflet.extras_0.2, Chrome]

I'm making icons to use in R/Leaflet with Shiny and all im getting is the below, but i've no idea why:

enter image description here

This is using the toy example from here:

oceanIcons <- iconList(
  ship = makeIcon("ferry-18.png", "ferry-18@2x.png", 18, 18),
  pirate = makeIcon("danger-24.png", "danger-24@2x.png", 24, 24)
)

# Some fake data
df <- sp::SpatialPointsDataFrame(
  cbind(
    (runif(20) - .5) * 10 - 90.620130,  # lng
    (runif(20) - .5) * 3.8 + 25.638077  # lat
  ),
  data.frame(type = factor(
    ifelse(runif(20) > 0.75, "pirate", "ship"),
    c("ship", "pirate")
  ))
)

leaflet(df) %>% addTiles() %>%
  # Select from oceanIcons based on df$type
  addMarkers(icon = ~oceanIcons[type])

And the following, with different but similar toy data, when using runApp(shinyApp(ui, server), launch.browser = TRUE);

enter image description here

1条回答
淡お忘
2楼-- · 2019-08-04 20:39

See the documentation for makeIcon. As the first argument it expects:

iconUrl: the URL or file path to the icon image

So your code will only work if you either have the png in your working directory, alter the path so it contains the correct path to the image on your hard drive, or you could use an URL. So a working example would be:

  # Make a list of icons. We'll index into it based on name.
    oceanIcons <- iconList(
      ship = makeIcon("http://globetrotterlife.org/blog/wp-content/uploads/leaflet-maps-marker-icons/ferry-18.png", 18, 18),
      pirate = makeIcon("http://globetrotterlife.org/blog/wp-content/uploads/leaflet-maps-marker-icons/danger-24.png", 24, 24)
    )

    # Some fake data
    df <- sp::SpatialPointsDataFrame(
      cbind(
        (runif(20) - .5) * 10 - 90.620130,  # lng
        (runif(20) - .5) * 3.8 + 25.638077  # lat
      ),
      data.frame(type = factor(
        ifelse(runif(20) > 0.75, "pirate", "ship"),
        c("ship", "pirate")
      ))
    )

    leaflet(df) %>% addTiles() %>%
      # Select from oceanIcons based on df$type
      addMarkers(icon = ~oceanIcons[type])

Hope this helps!

查看更多
登录 后发表回答