Installing and loading DT package
This is a short post giving steps on how to actually install R packages. Let’s suppose you want to install the DT package. Well, nothing could be easier. We just fire up an R shell and type:
> insatll.packages("DT") > library(DT)
The main function in this package is
datatable(). It creates an HTML widget to display R data objects with DataTables.
datatable(data, options = list(), class = "display", callback = JS("return table;"), rownames, colnames, container, caption = NULL, filter = c("none", "bottom", "top"), escape = TRUE, style = "default", width = NULL, height = NULL, elementId = NULL, fillContainer = getOption("DT.fillContainer", NULL), autoHideNavigation = getOption("DT.autoHideNavigation", NULL), selection = c("multiple", "single", "none"), extensions = list(), plugins = NULL, editable = FALSE)
|data||a data object (either a matrix or a data frame)|
|options||a list of initialization options (see https://datatables.net/reference/option/); the character options wrapped in |
|class||the CSS class(es) of the table|
|colnames||if missing, the column names of the data; otherwise it can be an unnamed character vector of names you want to show in the table header instead of the default data column names|
|container||a sketch of the HTML table to be filled with data cells; by default, it is generated from |
|caption||the table caption; a character vector or a tag object generated from |
|filter||whether/where to use column filters; |
How to convert data.frame to data.table
You can convert any `data.frame` into `datatable` using
the above output will be shown in a browser by default.
Table CSS Classes
class argument specifies the CSS classes of the table. The possible values can be found on the page of default styling options. The default value
display basically enables row striping, row highlighting on mouse over, row borders, and highlighting ordered columns. You can choose a different combination of CSS classes, such as
> datatable(head(iris), class = 'cell-border stripe')
You can enable table editing using the argument
?DT::datatable for its possible values). Then you will be able to double-click a cell to edit its value. It works in both client-side and server-side processing modes. Below are two client-side examples:
> DT::datatable(head(iris), editable = 'cell')
Display Row Names
If the data object has row names, they will be displayed as the first column of the table by default. You can suppress row names via the argument
rownames = FALSE, and you can also change row names by providing a different character vector to
> datatable(head(mtcars), rownames = FALSE) # no row names
You can add a table caption via the
caption argument. It can be either a character vector, or a tag object created from
> datatable( + head(iris), + caption = 'Table 1: This is a simple caption for the table.' + )
Hence, we saw what is DT package in R, how to install and load DT package how to use DT package to make tables and to make them more informative with each example.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!