What is the kable() method in R?

Overview

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.

Syntax

kable(data)

Parameters

The input (data) for the kable function is an R object or a data frame.

Return value

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

Example

Let’s look at an example of the kable function.

library(knitr) #Importing the library for kable()
my_basket = data.frame(
#Column 01
ITEM_GROUP = c("Fruit","Fruit","Fruit",
"Vegetable","Vegetable","Vegetable",
"Dairy","Dairy","Dairy"),
#Column 02
ITEM_NAME = c("Apple","Banana","Orange",
"Carrot","Potato","Brinjal",
"Milk","Cheese","Cream"),
#Column 03
Price = c(100.981, 80.643, 65.3,
71.9, 62.2, 71.3,
25.1, 62.9, 41.9))
#Function call
kable(my_basket)

Explanation

  • 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.

Copyright ©2024 Educative, Inc. All rights reserved