Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

knitr - Space after every five rows in kable output (with booktabs option) in R Markdown document

I am using knitr::kable() to render tables as part of an R Markdown document (that itself is part of a bookdown project). In particular, the booktabs option (through setting the booktabs argument to equal TRUE) renders the table in a nice-looking way. However, I'd like for there not to be a space after every five rows.

Here, for example, is the code and how the table in the bookdown demo appears when rendered as a PDF:

knitr::kable(
  head(iris, 20), caption = 'Here is a nice table!',
  booktabs = TRUE
)

iris table with booktabs

I'd like for the space that aappears after every five rows to not be included, but I cannot seem to find a setting in knitr::kable() that does this.

question from:https://stackoverflow.com/questions/49015578/space-after-every-five-rows-in-kable-output-with-booktabs-option-in-r-markdown

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason why the row height is not always equal is that by default,?kable?inserts a?addlinespace every 5 rows when booktabs is specified as TRUE, as is shown here:

linesep = if (booktabs) c('', '', '', '', '\addlinespace') else '\hline'

To alter this, add?linesep = "" as an argument to kable().

knitr::kable(
  head(iris, 20), caption = 'Here is a nice table!',
  booktabs = TRUE,
  linesep = ""
)

enter image description here

See?Get rid of addlinespace in kable?for more details.

It is also worth saying that you can play around with this option if you want to change the style. For example linesep = c("", "", "", "\hline") would add a horizontal line every four spaces.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...