如何强制kableExtra中的线路破坏逃脱= false的功能?

发布于 2025-02-12 14:47:16 字数 780 浏览 1 评论 0 原文

在kableExtra> = 0.8.0中, add_header_above 或 pack_rows 是直接添加 \ n

但是,这似乎不适用于 etaver = false 参数,如果文本还包含乳胶代码,则需要。

KableExtra中的一个强制线破坏如何使用逃脱= false

library(dplyr)
library(knitr)
library(kableExtra)

starwars %>%
  filter(species == 'Gungan' | species == 'Droid') %>%
  arrange(species) %>%
  select(name, eye_color) %>%
  kbl(booktabs = TRUE) %>%
  pack_rows(
    index = c(
      'The droids: everybody\'s favourite' = 6, 
      'The Gungans: only beloved of \nthose aged under $3^2$' = 3), 
    escape = FALSE)

In kableExtra >= 0.8.0, the canonical way to insert a linebreak into text piped into a table from a kableExtra function such as add_header_above or pack_rows is to add an \n directly.

However, this appears not to work with the escape = FALSE argument, which is required if the text also contains LaTeX code.

How can one force linebreaks in kableExtra functions with escape = FALSE?

library(dplyr)
library(knitr)
library(kableExtra)

starwars %>%
  filter(species == 'Gungan' | species == 'Droid') %>%
  arrange(species) %>%
  select(name, eye_color) %>%
  kbl(booktabs = TRUE) %>%
  pack_rows(
    index = c(
      'The droids: everybody\'s favourite' = 6, 
      'The Gungans: only beloved of \nthose aged under $3^2
 = 3), 
    escape = FALSE)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2025-02-19 14:47:16

问题

问题是您希望逃脱标题的一部分(即休息),而不是逃脱另一部分(即数学代码)。

进一步的并发症

此核心问题进一步复杂化了许多因素:

  1. 何时以及如何编程kableExtra来处理逃避
  2. 渴望拥有适用于HTML和乳胶输出的解决方案
  3. 何时以及如何评估代码的解决方案

的解决方案,

这是一个解决方案。这将适用于HTML和乳胶输出,但它不像您的原始代码那样干净,直截了当:

# a new version of `kableExtra::linebreak()` that takes into account what type 
# of output is desired as well as how much escaping is necessary
linebreak2 <- function(x, double_escape = TRUE, ...) {
  # if LaTeX insert text into a `\makecell[]{}` command and double escape
  if(knitr::is_latex_output())
    return(linebreak(x, double_escape = double_escape, ...))
  
  # if html output just replace `\n`s with `<br/>`s
  if(knitr::is_html_output())
    return(gsub("\n", "<br/>", x))
  
  # let x pass through for other types of output
  return(x)
}

# build the index named vector outside the pipe flow 
# in order to set the names using `linebreak2()`
index <- c(6, 3)
names(index) <- c(
  'The droids: everybody\'s favourite',
  linebreak2('The Gungans: only beloved of \nthose aged under $3^2

PDF输出

HTML输出

) ) # proceed as before starwars %>% filter(species == 'Gungan' | species == 'Droid') %>% arrange(species) %>% select(name, eye_color) %>% kbl(booktabs = TRUE) %>% pack_rows(index = index, escape = FALSE)

PDF输出

HTML输出

ISSUE

The issue at hand is that you wish to escape part of your header (i.e., the break) and not escape another part (i.e., the math code).

Further Complications

This core issue is further complicated by a number of factors:

  1. when and how kableExtra is programmed to deal with escaping
  2. a desire to have a solution that works for both html and LaTeX output
  3. when and how R evaluates code

A SOLUTION

Here is a solution that will work for both html and LaTeX output, but it is not as clean and straight forward as your original code:

# a new version of `kableExtra::linebreak()` that takes into account what type 
# of output is desired as well as how much escaping is necessary
linebreak2 <- function(x, double_escape = TRUE, ...) {
  # if LaTeX insert text into a `\makecell[]{}` command and double escape
  if(knitr::is_latex_output())
    return(linebreak(x, double_escape = double_escape, ...))
  
  # if html output just replace `\n`s with `<br/>`s
  if(knitr::is_html_output())
    return(gsub("\n", "<br/>", x))
  
  # let x pass through for other types of output
  return(x)
}

# build the index named vector outside the pipe flow 
# in order to set the names using `linebreak2()`
index <- c(6, 3)
names(index) <- c(
  'The droids: everybody\'s favourite',
  linebreak2('The Gungans: only beloved of \nthose aged under $3^2

PDF Output

enter image description here

HTML Output

enter image description here

) ) # proceed as before starwars %>% filter(species == 'Gungan' | species == 'Droid') %>% arrange(species) %>% select(name, eye_color) %>% kbl(booktabs = TRUE) %>% pack_rows(index = index, escape = FALSE)

PDF Output

enter image description here

HTML Output

enter image description here

司马昭之心 2025-02-19 14:47:16

您可以使用 html 线路断开标签&lt; br/&gt;

starwars %>%
  filter(species == 'Gungan' | species == 'Droid') %>%
  arrange(species) %>%
  select(name, eye_color) %>%
  kbl(booktabs = TRUE) %>%
  pack_rows(
    index = c(
      'The droids: everybody\'s favourite' = 6, 
      'The Gungans: only beloved of <br/> those aged under $3^2

”输入图像描述在这里”

= 3), escape = FALSE)

”输入图像描述在这里”

You could use html line break tag <br/>:

starwars %>%
  filter(species == 'Gungan' | species == 'Droid') %>%
  arrange(species) %>%
  select(name, eye_color) %>%
  kbl(booktabs = TRUE) %>%
  pack_rows(
    index = c(
      'The droids: everybody\'s favourite' = 6, 
      'The Gungans: only beloved of <br/> those aged under $3^2

enter image description here

= 3), escape = FALSE)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文