kable()
is a method in R designed to generate a table against the given input. It is a part of the knitr
package, which should be installed in the R environment for the kable
method to run.
kable(data)
The input (data
) for the kable
function is an R object or a data frame.
kable
returns a table for a data object. If the input is a list of objects, it returns a table containing multiple tables.
Let’s look at an example of the kable
function.
library(knitr) #Importing the library for kable()my_basket = data.frame(#Column 01ITEM_GROUP = c("Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy"),#Column 02ITEM_NAME = c("Apple","Banana","Orange","Carrot","Potato","Brinjal","Milk","Cheese","Cream"),#Column 03Price = c(100.981, 80.643, 65.3,71.9, 62.2, 71.3,25.1, 62.9, 41.9))#Function callkable(my_basket)
Line 1: We include the package which is required by the kable
function in order to run.
Lines 3 to 17: We generate our data frame.
Line 20: We call the kable
function to generate the table for our data frame.