line break within cell for huxtable table

点点圈 提交于 2020-12-05 07:47:53

问题


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.


回答1:


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




回答2:


Okay, so combining insights from this LaTeX StackExchange post and this classic XKCD got me this:

library(tidyverse)
library(huxtable)
cars <- mtcars %>% 
  mutate(car = rownames(.),
         car = str_replace(car, "(\\S*)\\s(.*)",
                           "\\\\vtop{\\\\hbox{\\\\strut \\1}\\\\hbox{\\\\strut \\2}}")) %>% 
  slice(1:5) %>% 
  select(car, cyl, hp)

ht <- as_hux(cars, add_colnames = TRUE) %>% 
    set_escape_contents(1:6, 1, FALSE)
theme_article(ht)

which gives the following PDF output:

Of course, you won't need quite as many escapes if you're building your own cells by hand rather than using str_replace. Note that putting set_escape_contents to FALSE for the cells with line breaks is crucial.




回答3:


Try this, which minimizes the amount of backslashes:

library(tidyverse)
library(huxtable)

cars <- mtcars %>% mutate(car = rownames(.))
cars$car <- str_replace(cars$car, " ", "\\\\newline ")
cars %>% 
      as_hux(add_colnames = TRUE) %>% 
      set_wrap(TRUE) %>% 
      set_escape_contents(everywhere, "car",  FALSE) %>% 
      quick_pdf()

The set_wrap() call is necessary for LaTeX tables to accept newlines, I believe.

If you want to escape different parts of the cells containing newlines, you can do that manually, e.g. with xtable::sanitize().



来源:https://stackoverflow.com/questions/49398097/line-break-within-cell-for-huxtable-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!