问题描述
我最近开始使用
escape_contents
部分似乎没有什么区别.
我不确定我想要的是否可行,但我知道它在其他包中(例如,DT::datatable
).如果可能的话,我真的很想使用 huxtable,因为我喜欢包的设计和灵活性.
任何想法都会很棒.
我应该指定我希望它适用于 PDF.
根据
I recently started using the huxtable R package for tables and I'm really impressed with it. One thing I can't seem to figure out, however, is how to get line breaks within a cell. Here's what I've tried
library(tidyverse)
library(huxtable)
cars <- mtcars %>%
mutate(car = rownames(.),
car = str_replace(car, " ", "\n")) %>%
slice(1:5) %>%
select(car, cyl, hp)
cars
# A tibble: 5 x 3
car cyl hp
<chr> <dbl> <dbl>
1 "Mazda\nRX4" 6.00 110
2 "Mazda\nRX4 Wag" 6.00 110
3 "Datsun\n710" 4.00 93.0
4 "Hornet\n4 Drive" 6.00 110
5 "Hornet\nSportabout" 8.00 175
ht <- as_hux(cars, add_colnames = TRUE)
escape_contents(ht) <- TRUE
ht
But this ends up without the line break, as in the screenshot below
The escape_contents
part doesn't seem to make a difference.
I'm not sure if what I want is possible, but I know it is in other packages (e.g., DT::datatable
). I'd really like to use huxtable, if possible, however, because I like the design and flexibility of the package.
Any thoughts would be great.
EDIT: I should have specified I'm hoping to get this to work for PDF.
According to Escaping HTML or LaTeX,You should use escape_contents(ht) <- FALSE
and use <br>
tag instead of \n
library(tidyverse)
library(huxtable)
cars <- mtcars %>% mutate(car = rownames(.),
car = str_replace(car, " ", "<br>")) %>%
slice(1:5) %>% select(car, cyl, hp)
ht <- as_hux(cars, add_colnames = TRUE)
escape_contents(ht) <- FALSE
ht
note that the output is a Rmarkdown document and thanks for the package information. it looks good. following is the output of mine
这篇关于huxtable 表的单元格内的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!