[R] prefer startsWith to substr() or regular expressions (#10687)

This commit is contained in:
James Lamb 2024-08-09 08:18:46 -05:00 committed by GitHub
parent fb9201abae
commit 18b28d9315
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 3 additions and 2 deletions

View File

@ -104,7 +104,7 @@ check.booster.params <- function(params, ...) {
# for multiclass, expect num_class to be set # for multiclass, expect num_class to be set
if (typeof(params[['objective']]) == "character" && if (typeof(params[['objective']]) == "character" &&
substr(NVL(params[['objective']], 'x'), 1, 6) == 'multi:' && startsWith(NVL(params[['objective']], 'x'), 'multi:') &&
as.numeric(NVL(params[['num_class']], 0)) < 2) { as.numeric(NVL(params[['num_class']], 0)) < 2) {
stop("'num_class' > 1 parameter must be set for multiclass classification") stop("'num_class' > 1 parameter must be set for multiclass classification")
} }

View File

@ -109,7 +109,7 @@ xgb.model.dt.tree <- function(model = NULL, text = NULL,
} else { } else {
trees <- trees[trees >= 0 & trees <= max(td$Tree)] trees <- trees[trees >= 0 & trees <= max(td$Tree)]
} }
td <- td[Tree %in% trees & !grepl('^booster', t)] td <- td[Tree %in% trees & !is.na(t) & !startsWith(t, 'booster')]
td[, Node := as.integer(sub("^([0-9]+):.*", "\\1", t))] td[, Node := as.integer(sub("^([0-9]+):.*", "\\1", t))]
if (!use_int_id) td[, ID := add.tree.id(Node, Tree)] if (!use_int_id) td[, ID := add.tree.id(Node, Tree)]

View File

@ -43,6 +43,7 @@ my_linters <- list(
spaces_inside_linter = lintr::spaces_inside_linter(), spaces_inside_linter = lintr::spaces_inside_linter(),
spaces_left_parentheses_linter = lintr::spaces_left_parentheses_linter(), spaces_left_parentheses_linter = lintr::spaces_left_parentheses_linter(),
sprintf = lintr::sprintf_linter(), sprintf = lintr::sprintf_linter(),
string_boundary = lintr::string_boundary_linter(),
trailing_blank_lines_linter = lintr::trailing_blank_lines_linter(), trailing_blank_lines_linter = lintr::trailing_blank_lines_linter(),
trailing_whitespace_linter = lintr::trailing_whitespace_linter(), trailing_whitespace_linter = lintr::trailing_whitespace_linter(),
true_false = lintr::T_and_F_symbol_linter(), true_false = lintr::T_and_F_symbol_linter(),