What is the kable() Function in R

The kable() function in R is a part of the knitr package “used to generate a table against the provided value”.

Syntax

kable(data)

Parameters

data: It is an R object or data frame. 

Return Value

The kable() function returns a table for a data object. If the input is a list of objects, it returns a table containing multiple tables.

Example 1: Simple use of the kable() function in R

Let’s take the ChickWeight dataset in this example and create a kable using the knitr::kable() function. The ChickWeight is a built-in R dataset that you can use anywhere in your program.

data <- head(ChickWeight[, 1:4])

Pass this data to the knitr::kable() function.

data <- head(ChickWeight[, 1:4])

print(knitr::kable(data, "pipe"))

Output

kable in R

Example 2: Generate a simple table using the kable() function in R

To generate a simple table using the kable() function, pass the “simple” argument.

data <- head(ChickWeight[, 1:4])

print(knitr::kable(data, "simple"))

Output

kable() function in R

Example 3: Create an HTML table using the kable() function in R

To create an HTML table using the kable() function, pass the “html” as an argument.

data <- head(ChickWeight[, 1:4])

print(knitr::kable(data, "html"))

Output

<table>
 <thead>
  <tr>
    <th style="text-align:right;"> weight </th>
    <th style="text-align:right;"> Time </th>
    <th style="text-align:left;"> Chick </th>
    <th style="text-align:left;"> Diet </th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td style="text-align:right;"> 42 </td>
   <td style="text-align:right;"> 0 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> 1 </td>
  </tr>
  <tr>
    <td style="text-align:right;"> 51 </td>
    <td style="text-align:right;"> 2 </td>
    <td style="text-align:left;"> 1 </td>
    <td style="text-align:left;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:right;"> 59 </td>
   <td style="text-align:right;"> 4 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:right;"> 64 </td>
   <td style="text-align:right;"> 6 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:right;"> 76 </td>
   <td style="text-align:right;"> 8 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:right;"> 93 </td>
   <td style="text-align:right;"> 10 </td>
   <td style="text-align:left;"> 1 </td>
   <td style="text-align:left;"> 1 </td>
  </tr>
 </tbody>
</table>

Example 4: Create LaTex using the kable() function in R

To create a LaTex using the kable() function, pass the “latex” argument.

data <- head(ChickWeight[, 1:4])

print(knitr::kable(data, "latex"))

Output

\begin{tabular}{r|r|l|l}
\hline
weight & Time & Chick & Diet\\
\hline
42 & 0 & 1 & 1\\
\hline
51 & 2 & 1 & 1\\
\hline
59 & 4 & 1 & 1\\
\hline
64 & 6 & 1 & 1\\
\hline
76 & 8 & 1 & 1\\
\hline
93 & 10 & 1 & 1\\
\hline
\end{tabular}

Example 5: Generate rst using the kable() function in R

To create restructured data using the kable() function, pass the “rst” argument.

data <- head(ChickWeight[, 1:4])

print(knitr::kable(data, "rst"))

Output

====== ==== ===== ====
weight Time Chick Diet
====== ==== ===== ====
 42     0    1     1
 51     2    1     1
 59     4    1     1
 64     6    1     1
 76     8    1     1
 93    10    1     1
====== ==== ===== ====

That’s it.

Leave a Comment