Skip to content

Commit 18b28d9

Browse files
authored
[R] prefer startsWith to substr() or regular expressions (dmlc#10687)
1 parent fb9201a commit 18b28d9

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

R-package/R/utils.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ check.booster.params <- function(params, ...) {
104104

105105
# for multiclass, expect num_class to be set
106106
if (typeof(params[['objective']]) == "character" &&
107-
substr(NVL(params[['objective']], 'x'), 1, 6) == 'multi:' &&
107+
startsWith(NVL(params[['objective']], 'x'), 'multi:') &&
108108
as.numeric(NVL(params[['num_class']], 0)) < 2) {
109109
stop("'num_class' > 1 parameter must be set for multiclass classification")
110110
}

R-package/R/xgb.model.dt.tree.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ xgb.model.dt.tree <- function(model = NULL, text = NULL,
109109
} else {
110110
trees <- trees[trees >= 0 & trees <= max(td$Tree)]
111111
}
112-
td <- td[Tree %in% trees & !grepl('^booster', t)]
112+
td <- td[Tree %in% trees & !is.na(t) & !startsWith(t, 'booster')]
113113

114114
td[, Node := as.integer(sub("^([0-9]+):.*", "\\1", t))]
115115
if (!use_int_id) td[, ID := add.tree.id(Node, Tree)]

tests/ci_build/lint_r.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ my_linters <- list(
4343
spaces_inside_linter = lintr::spaces_inside_linter(),
4444
spaces_left_parentheses_linter = lintr::spaces_left_parentheses_linter(),
4545
sprintf = lintr::sprintf_linter(),
46+
string_boundary = lintr::string_boundary_linter(),
4647
trailing_blank_lines_linter = lintr::trailing_blank_lines_linter(),
4748
trailing_whitespace_linter = lintr::trailing_whitespace_linter(),
4849
true_false = lintr::T_and_F_symbol_linter(),

0 commit comments

Comments
 (0)