【问题标题】:includeHTML in R Shiny Leaflet popupsR Shiny Leaflet 弹出窗口中的 includeHTML
【发布时间】:2020-09-29 18:46:45
【问题描述】:

我正在制作一个带有 Leaflet 的 R Shiny 应用程序,用于显示城市中的树木。我想将 svg 图像添加到标记弹出窗口中,这取决于单击标记的树种,但我无法让 includeHTML 工作。

具有两个标记的最小可重复示例

circle.svg:

<svg width = "20" height = "20"
  xmlns="http://www.w3.org/2000/svg">
  <circle cx="10" cy="10" r="10" style="fill:#000;"/>
</svg>

square.svg:

<svg width = "20" height = "20"
  xmlns="http://www.w3.org/2000/svg">
  <rect x="0" y="0" width = "20" height = "20" style="fill:#000;"/>
</svg>

当我运行下面的代码时,我得到一个错误:

library(shiny)
library(leaflet)
library(tidyverse)
library(magrittr)

df <- data.frame("type" = c("circle", "square"), 
                 "lon" = c(13.36, 13.37), 
                 "lat" = c(52.56, 52.57)
                 )

ui <- fluidPage(

  leafletOutput("map")

)

server <- function(input, output) {

  output$map <- renderLeaflet({
    leaflet(df) %>%
      addTiles() %>%
      addCircleMarkers(
        popup = ~ paste0(type, ": ", includeHTML(paste0(type, ".svg")))
        )
  })

}
shinyApp(ui = ui, server = server)
Warning: Error in file: invalid 'description' argument
  119: file
  118: readLines
  117: includeHTML
...

但如果删除 includeHTML,文件名会正确写入弹出窗口。

popup = ~ paste0(type, ": ", paste0(type, ".svg"))

如果我索引type,includeHTML 工作正常,期望它在两个弹出窗口中都显示圆圈,这当然不是我想要的:

popup = ~ paste0(type, ": ", includeHTML(paste0(type[1], ".svg")))

似乎 includeHTML 采用了整个 df$type 向量,而不仅仅是向量中与标记相关的一个元素。

发生了什么,我该如何解决?非常感谢您的帮助。

【问题讨论】:

    标签: r shiny leaflet popup


    【解决方案1】:

    您可以使用
    sapply(type, function(x) paste0(x, ": ", includeHTML(paste0(x, ".svg"))))
    生成弹出窗口 如下:

    ui <- fluidPage(
      leafletOutput("map")
    )
    
    server <- function(input, output) {
      output$map <- renderLeaflet({
        leaflet(df) %>%
          addTiles() %>%
          addCircleMarkers(
            popup=~sapply(type, function(x) paste0(x, ": ", includeHTML(paste0(x, ".svg"))))
            )
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2015-11-20
      • 1970-01-01
      • 2017-11-25
      • 2019-10-27
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多