R markdown 中 kableExtra table 的 popover/tooltip 中的换行符

Line break in popover/tooltip of kableExtra table in R markdown

我希望在将鼠标悬停在 table 的单元格上时在弹出窗口中显示文本。这行得通,但是,我没有设法在该文本中换行。我的例子改编自这里:https://haozhu233.github.io/kableExtra/awesome_table_in_html.html

---
title: "Line break in popover"
output: html_document
---

<script>
$(document).ready(function(){
    $('[data-toggle="popover"]').popover(); 
});

</script>

```{r echo = FALSE}

knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)

popover_dt <- data.frame(
  position = c("top", "bottom", "right", "left"),
  stringsAsFactors = FALSE
)
popover_dt$`Hover over these items` <- cell_spec(
  paste("Message on", popover_dt$position), # Cell texts
  popover = spec_popover(content = c("line\nbreak", "line<br/>break", "line&#013;break", "line&#10;break")))

kbl(popover_dt, escape = FALSE) %>%
  kable_paper("striped", full_width = FALSE)

```

但是,我没有开始工作的是在弹出窗口中的 line 和 break 之间进行换行。我尝试了 \n<br>&#013;&#10;。似乎没有任何尝试。知道如何解决这个问题吗?

您可以向文档添加自定义 css 样式,请参阅 :

---
title: "Line break in popover"
output: html_document
---

<script>
$(document).ready(function(){
    $('[data-toggle="popover"]').popover(); 
});
</script>

```{css, echo=FALSE}
div {
  white-space: pre-wrap;
}
```

```{r echo = FALSE}

knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)



popover_dt <- data.frame(
  position = c("top", "bottom", "right", "left"),
  stringsAsFactors = FALSE
)
popover_dt$`Hover over these items` <- cell_spec(
  paste("Message on", popover_dt$position), # Cell texts
  popover = spec_popover(content = c("line\nbreak", "line<br>break", "line&#013;break", "line&#10;break")))

kbl(popover_dt, escape = FALSE) %>%
  kable_paper("striped", full_width = FALSE)

```

html: true 添加到您的 javascript:

<script>
$(document).ready(function(){
    $('[data-toggle="popover"]').popover({html: true}); 
});
</script>

然后 <br/> 应该提供一个换行符。