带有闪亮的 R Leaflet 中的图标未加载(空图像)

Sam*_*Sam 3 icons r leaflet shiny

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

我正在使用 Shiny 制作在 R/Leaflet 中使用的图标,我得到的所有内容如下,但我不知道为什么:

在此输入图像描述

这是使用这里的玩具示例

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])
Run Code Online (Sandbox Code Playgroud)

以及以下,使用不同但相似的玩具数据时runApp(shinyApp(ui, server), launch.browser = TRUE)

在此输入图像描述

Flo*_*ian 5

请参阅makeIcon 的文档。作为第一个参数,它期望:

iconUrl: the URL or file path to the icon image
Run Code Online (Sandbox Code Playgroud)

因此,只有在工作目录中有png,更改路径以使其包含硬盘驱动器上图像的正确路径,或者可以使用 URL 时,您的代码才会起作用。所以一个有效的例子是:

  # 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])
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!