View source: R/filterDTColumn.R
filterDTColumn | R Documentation |
Filter a data.frame based on the DT datatable
widget column search string.
filterDTColumn(x, search)
filterDT(df, column, global)
x |
A numeric or character vector, usually representing a column of a data.frame. |
search |
A string specifying the search filter to apply to |
df |
A data.frame that was used in the |
column |
A character vector of per-column search strings to apply to |
global |
String containing a regular expression to search for across all columns in |
For character x
, search
is treated as a regular expression.
For numeric x
, search
should have the form LOWER ... UPPER
where all elements in [LOWER, UPPER] are retained.
For factor x
, search
should have the form ["choice_1", "choice_2", etc.]
.
This is also the case for logical x
, albeit with the only choices being "true"
or "false"
.
filterDT
will retain all rows where (i) any value in any column (after coercion to a string) matches global
,
and (ii) the value in each column satisfies the filter specified in the corresponding entry of column
.
Setting global
to an empty string will skip requirement (i) while
setting any entry of column
to an empty string will skip requirement (ii) for the affected column.
Ideally, ncol(df)
and length(searches)
would be the same, but if not,
filterDT
will simply filter on the first N entries where N is the smaller of the two.
Any NA
element in x
will be treated as a no-match.
The same applies for each column of df
that has non-empty column
.
Note that a no-match in one column does not preclude a successful match in another column by global
.
A logical vector indicating which entries of x
or rows of df
are to be retained.
Aaron Lun
datatable
and associated documentation for more details about column searches.
# Regular expression:
filterDTColumn(LETTERS, "A|B|C")
# Range query:
filterDTColumn(runif(20), "0.1 ... 0.5")
# Factor query:
filterDTColumn(factor(letters), "['a', 'b', 'c']")
# Works on DataFrames:
X <- data.frame(row.names=LETTERS, thing=runif(26),
stuff=sample(letters[1:3], 26, replace=TRUE),
stringsAsFactors=FALSE)
filterDT(X, c("0 ... 0.5", "a|b"), global="")
filterDT(X, "", global="A")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.