如何控制R中小叶功能弹出窗口的弹出窗口宽度?

cho*_*tom 5 r popup leaflet

我想通过R控制传单功能弹出窗口的宽度,我该怎么做?

  output$HomeMap <- renderLeaflet(leaflet() %>% 
                              addTiles() %>% 
                              setView(1.93, 41.48, zoom = 3) %>% 
                              addPolygons(data = subset(world, name %in%  datasourceInput()), 
                                          color = datasourceColor(), 
                                          weight = 1, 
                                          popup = datasourcePopups()
                              ))
Run Code Online (Sandbox Code Playgroud)

我不明白如何控制与弹出窗口相关的选项。

在此先感谢您在此问题上的帮助和最诚挚的问候!

sco*_*tus 9

我在创建我现在正在处理的一系列地图时遇到了类似的问题。我希望在 html/css/javascripted 页面上完成的任何格式都在引号内进行,我使用了双引号示例:"<br>"创建换行符并在下一行开始下一条信息。

为了给它排序,我使用了一组表格......我有 16 行文本,并希望将最后一行用于 png 图像。它起作用了,但是图像使文本从上方弹出窗口的一侧缩放……背景没有随之缩放。

所以我对代码进行了一些尝试,在网上搜索,然后在弹出变量的最顶部插入了这行代码:

myPopUp<- paste("<style> div.leaflet-popup-content {width:auto !important;}</style>", 
myvariable, "some copy to print", another variable)
Run Code Online (Sandbox Code Playgroud)

确保整个弹出窗口都在paste函数内部,现在弹出窗口会自动调整到内容的大小。这确实意味着您需要注意复制长度并适当调整弹出窗口中的任何图像的大小。

我的设置是从 292 个人口普查区之一中提取 16 个值和一个图表,并放入正确的数据和缓存的图像,然后重新缩放,它可以完美地工作。您可能想尝试一下。

这是神奇的代码: "<style> div.leaflet-popup-content {width:auto !important;}</style>"

这是它的样子相对于内容大小的弹出窗口

现在,如果我能在我一致认可的某个地方制作弹出式锚点就好了!


tim*_*lio 6

@bethanyP 的答案适用于所有情况。我认为一些额外的细节可能会有所帮助。我内联注释以解释每个步骤。

library(leaflet)

# example from https://rstudio.github.io/leaflet/popups.html
content <- paste(sep = "<br/>",
                 "<b><a href='http://www.samurainoodle.com'>Samurai Noodle</a></b>",
                 "606 5th Ave. S",
                 "Seattle, WA 98138"
)

leaflet() %>% addTiles() %>%
  addPopups(-122.327298, 47.597131, content,
            options = popupOptions(closeButton = FALSE)
  )

# according to ?popupOptions can specify min/maxWidth
#  set min and max width the to force a width
leaflet() %>% addTiles() %>%
  addPopups(
    -122.327298, 47.597131,
    content,
    options = popupOptions(
      closeButton = FALSE,
      minWidth = 300,
      maxWidth = 300
    )
  )
    
# on the other hand, it appears that we only have maxHeight
#  so height cannot be controlled in the same way
leaflet() %>% addTiles() %>%
  addPopups(
    -122.327298, 47.597131,
    content,
    options = popupOptions(
      closeButton = FALSE,
      maxHeight = 20
    )
  )

# let's extend the example to show how we can use
#   htmltools to add CSS to control our popups

# could also use the approach suggested in
#  the other answer, but I think this is more
#  fitting with typical HTML/JS convention

lf <- leaflet() %>% addTiles() %>%
  addPopups(
    -122.327298, 47.597131,
    content,
    options = popupOptions(
      closeButton = FALSE,
      # not necessary but adding a className
      #   can help in terms of specificity
      #   especially if you have multiple popups
      #   with different styling
      className = "myspecial-popup"
    )
  )

library(htmltools)
browsable(
  tagList(
    tags$head(
      #  more specific is better
      tags$style(
        'div.myspecial-popup div.leaflet-popup-content-wrapper {
          height: 400px;
          opacity: .5;
        }'
      )
    ),
    lf
  )
)
Run Code Online (Sandbox Code Playgroud)