data.table-condition-classes {data.table} | R Documentation |
Condition Handling with Classed Conditions
Description
data.table
provides specific condition classes for common operations, making it easier to handle conditions programmatically. This is particularly useful when writing robust code or packages that use data.table
. Relying on the exact text of condition messages is fragile (it is not uncommon to change the wording slightly, or for the user's session not to be in English); prefer using the signal class where possible.
Details
Available Condition Classes
data.table
provides the following specific condition classes:
Error Classes:
-
dt_missing_column_error
: When referencing columns that don't exist -
dt_invalid_input_error
: When providing invalid input types or empty required arguments -
dt_unsortable_type_error
: When trying to sort/key unsupported types -
dt_join_type_mismatch_error
: When column types are incompatible in joins/set operations -
dt_invalid_let_error
: When using assignment operators incorrectly
Warning Classes:
-
dt_missing_fun_aggregate_warning
: When aggregation function is missing in operations that require it
Backward Compatibility
All condition classes inherit from base R's condition system, so existing tryCatch(..., error = ...)
code continues to work unchanged. The new classes simply provide more specific handling options when needed.
See Also
tryCatch
, test
, https://adv-r.hadley.nz/conditions.html
Examples
# Handle missing column errors specifically
DT <- data.table(a = 1:3, b = 4:6)
tryCatch({
setkey(DT, nonexistent_col)
}, dt_missing_column_error = function(e) {
cat("Missing column detected:", conditionMessage(e), "\n")
}, error = function(e) {
cat("Other error:", conditionMessage(e), "\n")
})