datatable editor DT package, In this tutorial, we are going to discuss DT package from R. DT stands for data tables and datatable() is the main function of DT package.
datatable() is completely different from data.table() function
DT package is very easy to use and based on this package can filter, search, and export data into different formats easily.
DT package outputs can easily render into R console, R Shiny, and R Markdown.
First, we need to install the package, if you are not installed run below command
install.packages("DT") library(DT) datatable(iris)
Here we are going to make use of the iris package for demonstration purposes. Now in the RStudio viewer pane, you can see the HTML results. You can sort the variables and able to change the number of entries in the Show button.
If you don’t want to display the row numbers in the data frame simply run the below code.
datatable(iris,rownames = FALSE)
In search box, you can type the number and can search the particular number from the dataset.
Suppose if you want to search the number based on column-wise, you can pass one more argument that is filter.
datatable(iris,rownames = FALSE,filter = "top")
If you don’t want a display search bar at the top and change display entries into some other number can easily do that.
datatable(iris,options = list(searching=FALSE, pageLength=20))
different styling fetures are available in DT package.
So you can make use of different CSS styles.
datatable(iris,rownames = FALSE,clas='hover cell-border stripe')
Data entry and correction are very important in the data cleaning part. Suppose if you want to correct the entry you can make use of datatable editor option.
datatable(iris,rownames = FALSE, editable = 'cell')
Now you can edit the cell you want. This feature will be more helpful when you are working on shiny because you can enable this option on the server side and the user side also.
If you want to change the column names then simply add the argument colnames.
datatable(iris,rownames = FALSE,colnames = c("A","B","C","D","E"))
If you want to change the particular column names then
datatable(iris,rownames = FALSE,colnames = c("X"="Sepal.Length","Y"="Petal.Length"))
If you want to change the title then make use of the caption.
You can add the buttons into the data table and make it more interactive also.
datatable(iris,extensions = 'Buttons', options = list(dom='Bfrtip', buttons=c('copy', 'csv', 'excel', 'print', 'pdf')))
Buttons can be more helpful feature if you are using shiny.