column_spec: Specify the look of the selected column

Description Usage Arguments Details Examples

View source: R/column_spec.R

Description

This function allows users to select a column and then specify its look.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
column_spec(
  kable_input,
  column,
  width = NULL,
  bold = FALSE,
  italic = FALSE,
  monospace = FALSE,
  underline = FALSE,
  strikeout = FALSE,
  color = NULL,
  background = NULL,
  border_left = FALSE,
  border_right = FALSE,
  width_min = NULL,
  width_max = NULL,
  extra_css = NULL,
  include_thead = FALSE,
  latex_column_spec = NULL,
  latex_valign = "p",
  link = NULL,
  new_tab = TRUE,
  tooltip = NULL,
  popover = NULL,
  image = NULL
)

Arguments

kable_input

Output of knitr::kable() with format specified

column

A numeric value or vector indicating which column(s) to be selected.

width

A character string telling HTML & LaTeX how wide the column needs to be, e.g. "10cm", "3in" or "30em".

bold

T/F value or vector to control whether the text of the selected column need to be bolded.

italic

T/F value or vector to control whether the text of the selected column need to be emphasized.

monospace

T/F value or vector to control whether the text of the selected column need to be monospaced (verbatim)

underline

T/F value or vector to control whether the text of the selected row need to be underlined

strikeout

T/F value or vector to control whether the text of the selected row need to be striked out.

color

A character string or vector for column text color. Here please pay attention to the differences in color codes between HTML and LaTeX.

background

A character string or vector for column background color. Here please pay attention to the differences in color codes between HTML and LaTeX.

border_left

A logical variable indicating whether there should be a border line on the left of the selected column. In HTML, you can also pass in a character string for the CSS of the border line

border_right

A logical variable indicating whether there should be a border line on the right of the selected column. In HTML, you can also pass in a character string for the CSS of the border line

width_min

Only for HTML table. Normal column width will automatically collapse when the window cannot hold enough contents. With this width_min, you can set up a column with a width that won't collapse even when the window is not wide enough.

width_max

Only for HTML table. width_max defines the maximum width of table columns.

extra_css

A vector of extra css text to be passed into the cells of the column.

include_thead

T/F. A HTML only feature to contoll whether the header row will be manipulated. Default is FALSE.

latex_column_spec

Only for LaTeX tables. Code to replace the column specification. If not NULL, will override all other arguments.

latex_valign

vertical alignment. Only works when you specified column width. Choose among p, m, b.

link

A vector of strings for url links.

new_tab

T/F for whether to open up the new link in new tab

tooltip

A vector of strings to be displayed as tooltip. Obviously, this feature is only available in HTML. Read the package vignette to see how to use bootstrap tooltip css to improve the loading speed and look.

popover

Similar with tooltip but can hold more contents. The best way to build a popover is through spec_popover(). If you only provide a text string, it will be used as content. Note that You have to enable this bootstrap module manually. Read the package vignette to see how.

image

Vector of image paths.

Details

Use latex_column_spec in a LaTeX table to change or customize the column specification. Because of the way it is handled internally, any backslashes must be escaped.

Examples

1
2
3
4
5
6
7
## Not run: 
x <- knitr::kable(head(mtcars), "html")
column_spec(x, 1:2, width = "20em", bold = TRUE, italic = TRUE)
x <- knitr::kable(head(mtcars), "latex", booktabs = TRUE)
column_spec(x, 1, latex_column_spec = ">{\\\\color{red}}c")

## End(Not run)

Example output

<table>
 <thead>
  <tr>
   <th style="text-align:left;">   </th>
   <th style="text-align:right;"> mpg </th>
   <th style="text-align:right;"> cyl </th>
   <th style="text-align:right;"> disp </th>
   <th style="text-align:right;"> hp </th>
   <th style="text-align:right;"> drat </th>
   <th style="text-align:right;"> wt </th>
   <th style="text-align:right;"> qsec </th>
   <th style="text-align:right;"> vs </th>
   <th style="text-align:right;"> am </th>
   <th style="text-align:right;"> gear </th>
   <th style="text-align:right;"> carb </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;width: 20em; font-weight: bold;font-style: italic;"> Mazda RX4 </td>
   <td style="text-align:right;width: 20em; font-weight: bold;font-style: italic;"> 21.0 </td>
   <td style="text-align:right;"> 6 </td>
   <td style="text-align:right;"> 160 </td>
   <td style="text-align:right;"> 110 </td>
   <td style="text-align:right;"> 3.90 </td>
   <td style="text-align:right;"> 2.620 </td>
   <td style="text-align:right;"> 16.46 </td>
   <td style="text-align:right;"> 0 </td>
   <td style="text-align:right;"> 1 </td>
   <td style="text-align:right;"> 4 </td>
   <td style="text-align:right;"> 4 </td>
  </tr>
  <tr>
   <td style="text-align:left;width: 20em; font-weight: bold;font-style: italic;"> Mazda RX4 Wag </td>
   <td style="text-align:right;width: 20em; font-weight: bold;font-style: italic;"> 21.0 </td>
   <td style="text-align:right;"> 6 </td>
   <td style="text-align:right;"> 160 </td>
   <td style="text-align:right;"> 110 </td>
   <td style="text-align:right;"> 3.90 </td>
   <td style="text-align:right;"> 2.875 </td>
   <td style="text-align:right;"> 17.02 </td>
   <td style="text-align:right;"> 0 </td>
   <td style="text-align:right;"> 1 </td>
   <td style="text-align:right;"> 4 </td>
   <td style="text-align:right;"> 4 </td>
  </tr>
  <tr>
   <td style="text-align:left;width: 20em; font-weight: bold;font-style: italic;"> Datsun 710 </td>
   <td style="text-align:right;width: 20em; font-weight: bold;font-style: italic;"> 22.8 </td>
   <td style="text-align:right;"> 4 </td>
   <td style="text-align:right;"> 108 </td>
   <td style="text-align:right;"> 93 </td>
   <td style="text-align:right;"> 3.85 </td>
   <td style="text-align:right;"> 2.320 </td>
   <td style="text-align:right;"> 18.61 </td>
   <td style="text-align:right;"> 1 </td>
   <td style="text-align:right;"> 1 </td>
   <td style="text-align:right;"> 4 </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:left;width: 20em; font-weight: bold;font-style: italic;"> Hornet 4 Drive </td>
   <td style="text-align:right;width: 20em; font-weight: bold;font-style: italic;"> 21.4 </td>
   <td style="text-align:right;"> 6 </td>
   <td style="text-align:right;"> 258 </td>
   <td style="text-align:right;"> 110 </td>
   <td style="text-align:right;"> 3.08 </td>
   <td style="text-align:right;"> 3.215 </td>
   <td style="text-align:right;"> 19.44 </td>
   <td style="text-align:right;"> 1 </td>
   <td style="text-align:right;"> 0 </td>
   <td style="text-align:right;"> 3 </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:left;width: 20em; font-weight: bold;font-style: italic;"> Hornet Sportabout </td>
   <td style="text-align:right;width: 20em; font-weight: bold;font-style: italic;"> 18.7 </td>
   <td style="text-align:right;"> 8 </td>
   <td style="text-align:right;"> 360 </td>
   <td style="text-align:right;"> 175 </td>
   <td style="text-align:right;"> 3.15 </td>
   <td style="text-align:right;"> 3.440 </td>
   <td style="text-align:right;"> 17.02 </td>
   <td style="text-align:right;"> 0 </td>
   <td style="text-align:right;"> 0 </td>
   <td style="text-align:right;"> 3 </td>
   <td style="text-align:right;"> 2 </td>
  </tr>
  <tr>
   <td style="text-align:left;width: 20em; font-weight: bold;font-style: italic;"> Valiant </td>
   <td style="text-align:right;width: 20em; font-weight: bold;font-style: italic;"> 18.1 </td>
   <td style="text-align:right;"> 6 </td>
   <td style="text-align:right;"> 225 </td>
   <td style="text-align:right;"> 105 </td>
   <td style="text-align:right;"> 2.76 </td>
   <td style="text-align:right;"> 3.460 </td>
   <td style="text-align:right;"> 20.22 </td>
   <td style="text-align:right;"> 1 </td>
   <td style="text-align:right;"> 0 </td>
   <td style="text-align:right;"> 3 </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
</tbody>
</table>
\begin{tabular}{>{\color{red}}crrrrrrrrrrr}
\toprule
  & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
\addlinespace
Valiant & 18.1 & 6 & 225 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
\bottomrule
\end{tabular}

kableExtra documentation built on Feb. 20, 2021, 9:10 a.m.