Steve Reno
Steve Reno

Reputation: 1384

aligning all knitr tables in R markdown

I have an rmarkdown document with several tables which are currently being printed with kable(tbl, align = 'c'), but I am wondering if it is possible to set an option to default print all of the tables in the document with center alignment.

Something like

knitr::opts_chunk$set(fig.align = 'center')

except for centering table output rather than figure alignment.

Upvotes: 6

Views: 4684

Answers (1)

Peter
Peter

Reputation: 7770

The knitr options such as fig.align control how the figure is displayed with respect to the document. A similar option for tables would control if the whole table was centered in the document or not.

To control the alignment of the contents within a table should be granular. You could make a wrapper function for kable that would provide the default options you want.

my_kable <- function(x, align = "c", ...) {
  knitr::kable(x, align = align, ...)
}

The my_kable function will use the the wanted align = 'c' as default and the use of the ... will let you pass any additional arguments needed for a specific table from my_kable to kntir::kable.

Upvotes: 3

Related Questions