2015-03-31 40 views
4

我想通过R来控制传单功能弹出窗口的宽度,我该怎么做?如何控制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() 
          )) 

我不明白如何控制相关的弹出的选项..

提前感谢您在这个问题上和问候的帮助!

+0

您是否曾经为此找到过解决方案?我试图找出如何增加R中的传单地图的弹出框的宽度,但我还没有运气... – KMcL 2016-03-22 17:15:18

回答

2

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

为了下令,我使用了一组表格......我有16行的文本,并希望使用最后一行的PNG图像。它的工作原理,但图像使文本在上面的弹出窗口的一侧缩放......背景没有按比例放大。

所以我做了一些与代码玩耍,冲刷净,然后插入这行代码在我的弹出式变量的顶部:

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

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

我有我的设置来从292个人口普查区之一拉出16个值和图表,然后放入正确的数据和缓存的图像,然后重新缩放,并且工作完美无缺。您可能想尝试一下。

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

这是它的外观the popup sized relative to the content

现在,如果我能让弹出锚的地方,我是赞成的一致!

+0

伟大的解决方案。您还可以使用“不透明度”来添加一些透明度,以便在弹出窗口下不会完全丢失上下文。请参阅https://css-tricks.com/almanac/properties/o/opacity/。如果你愿意,很乐意演示。 – timelyportfolio 2016-05-05 12:46:49

+0

这解决了我的问题! – KMcL 2016-06-08 22:21:48

2

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