лучший способ контролировать ширину листовки этикетки - PullRequest
1 голос
/ 09 марта 2020

Как лучше всего наклеивать ярлыки с длинными листочками? Этот вопрос не о всплывающих окнах листовок.

Например:

library(leaflet)

# an exceedingly long text label
gettysburg.address <- "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this."

leaflet() %>%
  addTiles() %>%
  addMarkers(lng=-77.229797, lat=39.819410,
             # an exceedingly long text label
             label = htmltools::HTML(gettysburg.address))

Один из вариантов, конечно, заключается в том, чтобы вручную вставить разрыв строки HTML в желаемые интервалы. Это прекрасно работает, но это непрактично при генерации большого количества меток.

wrapped.gettysburg.address <- paste("Four score and seven years ago our fathers", "<br>", 
                            "brought forth on this continent, a new nation,", "<br>", 
                            "conceived in Liberty, and dedicated to the proposition", "<br>", 
                            "that all men are created equal. Now we are engaged in a", "<br>", 
                            "great civil war, testing whether that nation, or any", "<br>", 
                            "nation so conceived and so dedicated, can long endure.", "<br>", 
                            "We are met on a great battle-field of that war. We have", "<br>", 
                            "come to dedicate a portion of that field, as a final", "<br>", 
                            "resting place for those who here gave their lives that", "<br>", 
                            "that nation might live. It is altogether fitting and", "<br>", 
                            "proper that we should do this.")

leaflet() %>%
  addTiles() %>%
  addMarkers(lng=-77.229797, lat=39.819410,
             # an exceedingly long text label
             label = htmltools::HTML(wrapped.gettysburg.address))

Я могу представить себе несколько разных решений. 1030 * аргумент стиля, управляющий шириной поля метки всплывающей подсказки. Мне не удалось найти этот аргумент, если он существует.

Используйте функцию для вставки <br> через регулярные интервалы в текст метки, и затем оберните метку htmltools::HTML, как в примере выше.

Существует ли уже существующая функция, такая как stringr::str_wrap, которая вставляет вместо разрывов HTML? В качестве альтернативы, есть ли способ напрямую контролировать ширину коробки с этикеткой листовки?

1 Ответ

1 голос
/ 09 марта 2020
# an exceedingly long text label
gettysburg.address <- "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this."

leaf <- leaflet() %>%
  addTiles() %>%
  addMarkers(lng=-77.229797, lat=39.819410,
             # an exceedingly long text label
             label = htmltools::HTML(gettysburg.address))

использовать пользовательский css код

library(htmltools)
browsable(
  tagList(list(
    tags$head(
      tags$style(
        ".leaflet-tooltip{ width: 150px; white-space: normal; }"
      )
    ),
    leaf
  ))
)

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...