Icons not loading (empty image) in R Leaflet with Shiny

余生颓废 提交于 2019-12-24 09:58:44

问题


[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:

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);


回答1:


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!



来源:https://stackoverflow.com/questions/48944127/icons-not-loading-empty-image-in-r-leaflet-with-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!