Basic Usage
To create a data table, usereactable() on a data frameor matrix. The table will be sortable and paginated by default:
Column definitions
Columns can be customized by providing a named list of columndefinitions created bycolDef() tocolumns:
reactable(iris[1:5,], columns=list( Sepal.Length=colDef(name="Sepal Length"), Sepal.Width=colDef(name="Sepal Width"), Species=colDef(align="center")))For convenience, you can also specify a defaultcolDef()to use for all columns indefaultColDef:
reactable(iris[1:5,], defaultColDef=colDef( header=function(value)gsub("."," ",value, fixed=TRUE), cell=function(value)format(value, nsmall=1), align="center", minWidth=70, headerStyle=list(background="#f7f7f8")), columns=list( Species=colDef(minWidth=140)# overrides the default), bordered=TRUE, highlight=TRUE)Sorting
Tables are sortable by default. You can sort a column by clicking onits header, or sort multiple columns by holding the shift key whilesorting.
Sorting toggles between ascending and descending order by default. Toclear the sort, hold the shift key while sorting, and the sorting willadditionally toggle between ascending, descending, and unsortedorder.
Note: Ascending order means the lowest, first, orearliest values will appear first. Descending order means the largest,last, or latest values will appear first.
Default sorted columns
You can set the default sorted columns by providing a vector ofcolumn names todefaultSorted:
You can also provide a named list to customize the default sortorders. Use"asc" for ascending order, or"desc" for descending order:
Default sort order
Columns are sorted in ascending order first by default. To change thedefault sort order for all columns in the table, setdefaultSortOrder inreactable() to"asc" for ascending order, or"desc" fordescending order.
To change the sort order of an individual column, setdefaultSortOrder in itscolDef() to"asc" or"desc". The default sort order of thecolumn takes precedence over the table.
reactable(iris[48:52,], defaultSortOrder="desc", columns=list( Species=colDef(defaultSortOrder="asc")), defaultSorted=c("Species","Petal.Length"))Sort missing values last
You can ignore missing values when sorting by settingsortNALast on a column:
reactable(data.frame( n=c(1,2,3,-Inf,Inf), x=c(2,3,1,NA,NaN), y=c("aa","cc","bb",NA,NA)), defaultColDef=colDef(sortNALast=TRUE), defaultSorted="x")No sorting
You can disable sorting by settingsortable toFALSE on the table or column. When only some columns aresortable, it can help to indicate sortable columns usingshowSortable:
reactable(iris[1:5,], sortable=FALSE, showSortable=TRUE, columns=list( Petal.Width=colDef(sortable=TRUE), Petal.Length=colDef(sortable=TRUE)))Hide sort icons
You can hide sort icons by settingshowSortIcon toFALSE. This is only recommended when you want to use acustom sortindicator.
reactable(iris[1:5,], showSortIcon=FALSE)Filtering
You can make columns filterable by settingfilterable = TRUE inreactable():
data<-MASS::Cars93[1:20,c("Manufacturer","Model","Type","AirBags","Price")]reactable(data, filterable=TRUE, minRows=10)To make specific columns filterable (or not), setfilterable toTRUE orFALSE incolDef():
Custom filtering
Column filtering can be customized using thefilterMethod andfilterInput arguments incolDef(). See theCustomFiltering guide for more details and examples.
This example shows basic usage of a custom filter method, changingfiltering on theManufacturer column to be case-sensitiverather than case-insensitive. (Try filtering for “bmw” and then“BMW”).
data<-MASS::Cars93[,c("Manufacturer","Model","Type","Price")]reactable(data, columns=list( Manufacturer=colDef( filterable=TRUE,# Filter by case-sensitive text match filterMethod=JS("function(rows, columnId, filterValue) { return rows.filter(function(row) { return row.values[columnId].indexOf(filterValue) !== -1 }) }"))), defaultPageSize=5)Searching
You can make the entire table searchable by settingsearchable = TRUE inreactable():
data<-MASS::Cars93[1:20,c("Manufacturer","Model","Type","AirBags","Price")]reactable(data, searchable=TRUE, minRows=10)Custom searching
The table search method can be customized using thesearchMethod argument inreactable(). See theCustom Filtering guide for detailsand examples.
Pagination
You can change the default page size by configuringdefaultPageSize:
reactable(iris[1:6,], defaultPageSize=4)You can also set the minimum rows per page usingminRows. This may be useful when rows don’t completely fillthe page, or if the table has filtering:
reactable(iris[1:6,], defaultPageSize=4, minRows=4, searchable=TRUE)Page size options
You can show a dropdown of page sizes for users to choose from usingshowPageSizeOptions. The page size options can becustomized throughpageSizeOptions:
Alternative pagination types
You can use an alternative pagination type by settingpaginationType to:
"jump"to show a page jump"simple"to show previous/next buttons only
Page jump
reactable(iris[1:50,], paginationType="jump", defaultPageSize=4)Simple
reactable(iris[1:50,], paginationType="simple", defaultPageSize=4)Hide page info
You can hide page info by settingshowPageInfo toFALSE:
reactable(iris[1:12,], showPageInfo=FALSE, defaultPageSize=4)reactable(iris[1:12,], showPageInfo=FALSE, showPageSizeOptions=TRUE, defaultPageSize=4)Always show pagination
By default, pagination is hidden if the table only has one page. Tokeep the pagination shown, setshowPagination toTRUE. This is especially useful if you want to keep thepage info showing the number of rows in the table.
reactable(iris[1:5,], showPagination=TRUE)No pagination
Tables are paginated by default, but you can disable pagination bysettingpagination = FALSE:
reactable(iris[1:20,], pagination=FALSE, highlight=TRUE, height=250)If you want to keep the row count and pagination controls visibleeven when pagination is disabled, setshowPagination = TRUE:
reactable(iris[1:20,], pagination=FALSE, showPagination=TRUE, highlight=TRUE, height=250)Tip: Disabling pagination is not recommended forlarge tables with many interactive elements (such as links, expandbuttons, or selection checkboxes), as that can make it difficult forkeyboard users to navigate the page.
Grouping and Aggregation
You can group rows in a table by specifying one or more columns ingroupBy:
data<-MASS::Cars93[10:22,c("Manufacturer","Model","Type","Price","MPG.city")]reactable(data, groupBy="Manufacturer")When rows are grouped, you can aggregate data in a column using anaggregate function:
data<-MASS::Cars93[14:38,c("Type","Price","MPG.city","DriveTrain","Man.trans.avail")]reactable(data, groupBy="Type", columns=list( Price=colDef(aggregate="max"), MPG.city=colDef(aggregate="mean", format=colFormat(digits=1)), DriveTrain=colDef(aggregate="unique"), Man.trans.avail=colDef(aggregate="frequency")))You can use one of the built-in aggregate functions:
colDef(aggregate="sum")# Sum of numberscolDef(aggregate="mean")# Mean of numberscolDef(aggregate="max")# Maximum of numberscolDef(aggregate="min")# Minimum of numberscolDef(aggregate="median")# Median of numberscolDef(aggregate="count")# Count of valuescolDef(aggregate="unique")# Comma-separated list of unique valuescolDef(aggregate="frequency")# Comma-separated counts of unique valuesOr a custom aggregate function in #"../reference/colDef.html">colDef( aggregate=JS(" function(values, rows) { // input: // - values: an array of all values in the group // - rows: an array of row data values for all rows in the group (optional) // // output: // - an aggregated value, e.g. a comma-separated list return values.join(', ') } "))
Multiple groups
data<-data.frame( State=state.name, Region=state.region, Division=state.division, Area=state.area)reactable(data, groupBy=c("Region","Division"), columns=list( Division=colDef(aggregate="unique"), Area=colDef(aggregate="sum", format=colFormat(separators=TRUE))), bordered=TRUE)Custom aggregate function
Custom aggregate functions are useful when none of the built-inaggregate functions apply, or when you want to aggregate values frommultiple columns. For example, when calculating aggregate averages orpercentages.
Within a custom aggregate function, you can access the values in thecolumn using thevalues argument, and the values in othercolumns using therows argument:
columns=list( Price=colDef( aggregate=JS("function(values, rows) { values// [46.8, 27.6, 57] rows// [// { "Model": "Dynasty", "Manufacturer": "Dodge", "Price": 46.8, "Units": 2 },// { "Model": "Colt", "Manufacturer": "Dodge", "Price": 27.6, "Units": 5 },// { "Model": "Caravan", "Manufacturer": "Dodge", "Price": 57, "Units": 5 }// ] }") ))Here’s an example that calculates an aggregate average price bydividing the the sum of two columns,Price andUnits:
library(dplyr)set.seed(10)data<-sample_n(MASS::Cars93[23:40,],30, replace=TRUE)%>%mutate(Price=Price*3, Units=sample(1:5,30, replace=TRUE))%>%mutate(Avg.Price=Price/Units)%>%select(Model,Manufacturer,Price,Units,Avg.Price)reactable(data, groupBy="Manufacturer", columns=list( Price=colDef(aggregate="sum", format=colFormat(currency="USD")), Units=colDef(aggregate="sum"), Avg.Price=colDef(# Calculate the aggregate Avg.Price as `sum(Price) / sum(Units)` aggregate=JS("function(values, rows) { let totalPrice = 0 let totalUnits = 0 rows.forEach(function(row) { totalPrice += row['Price'] totalUnits += row['Units'] }) return totalPrice / totalUnits }"), format=colFormat(currency="USD"))))Include sub rows in pagination
By default, sub rows are excluded from pagination and always shown onthe same page when expanded. To include sub rows in pagination, you cansetpaginateSubRows toTRUE. This isrecommended for grouped tables with a large number of rows whereexpanded rows may not all fit on one page.
data<-MASS::Cars93[,c("Manufacturer","Model","Type","Price","MPG.city")]reactable(data, groupBy="Type", paginateSubRows=TRUE)