Skip to content

Commit 589b385

Browse files
authored
[R] fix uses of 1:length(x) and other small things (#5992)
1 parent 801e6b6 commit 589b385

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

R-package/R/xgb.DMatrix.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ slice.xgb.DMatrix <- function(object, idxset, ...) {
357357
#' @export
358358
print.xgb.DMatrix <- function(x, verbose = FALSE, ...) {
359359
cat('xgb.DMatrix dim:', nrow(x), 'x', ncol(x), ' info: ')
360-
infos <- c()
360+
infos <- character(0)
361361
if (length(getinfo(x, 'label')) > 0) infos <- 'label'
362362
if (length(getinfo(x, 'weight')) > 0) infos <- c(infos, 'weight')
363363
if (length(getinfo(x, 'base_margin')) > 0) infos <- c(infos, 'base_margin')

R-package/R/xgb.plot.importance.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ xgb.plot.importance <- function(importance_matrix = NULL, top_n = NULL, measure
106106
par(mar = mar)
107107

108108
# reverse the order of rows to have the highest ranked at the top
109-
importance_matrix[nrow(importance_matrix):1,
109+
importance_matrix[rev(seq_len(nrow(importance_matrix))),
110110
barplot(Importance, horiz = TRUE, border = NA, cex.names = cex,
111111
names.arg = Feature, las = 1, ...)]
112112
grid(NULL, NA)
113113
# redraw over the grid
114-
importance_matrix[nrow(importance_matrix):1,
114+
importance_matrix[rev(seq_len(nrow(importance_matrix))),
115115
barplot(Importance, horiz = TRUE, border = NA, add = TRUE)]
116116
par(op)
117117
}

R-package/R/xgb.plot.shap.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ xgb.plot.shap <- function(data, shap_contrib = NULL, features = NULL, top_n = 1,
124124
stop("shap_contrib is not compatible with the provided data")
125125

126126
nsample <- if (is.null(subsample)) min(100000, nrow(data)) else as.integer(subsample * nrow(data))
127-
idx <- sample(1:nrow(data), nsample)
127+
idx <- sample(seq_len(nrow(data)), nsample)
128128
data <- data[idx, ]
129129

130130
if (is.null(shap_contrib)) {
@@ -162,7 +162,7 @@ xgb.plot.shap <- function(data, shap_contrib = NULL, features = NULL, top_n = 1,
162162
data <- data[, features, drop = FALSE]
163163
cols <- colnames(data)
164164
if (is.null(cols)) cols <- colnames(shap_contrib)
165-
if (is.null(cols)) cols <- paste0('X', 1:ncol(data))
165+
if (is.null(cols)) cols <- paste0('X', seq_len(ncol(data)))
166166
colnames(data) <- cols
167167
colnames(shap_contrib) <- cols
168168

R-package/demo/interaction_constraints.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ treeInteractions <- function(input_tree, input_max_depth) {
3636
interaction_trees <- trees[!is.na(Split) & !is.na(parent_1),
3737
c('Feature', paste0('parent_feat_', 1:(input_max_depth - 1))),
3838
with = FALSE]
39-
interaction_trees_split <- split(interaction_trees, 1:nrow(interaction_trees))
39+
interaction_trees_split <- split(interaction_trees, seq_len(nrow(interaction_trees)))
4040
interaction_list <- lapply(interaction_trees_split, as.character)
4141

4242
# Remove NAs (no parent interaction)
@@ -101,8 +101,8 @@ bst3_interactions <- treeInteractions(bst3_tree, 4)
101101

102102
# Show monotonic constraints still apply by checking scores after incrementing V1
103103
x1 <- sort(unique(x[['V1']]))
104-
for (i in 1:length(x1)){
105-
testdata <- copy(x[, -c('V1')])
104+
for (i in seq_along(x1)){
105+
testdata <- copy(x[, - ('V1')])
106106
testdata[['V1']] <- x1[i]
107107
testdata <- testdata[, paste0('V', 1:10), with = FALSE]
108108
pred <- predict(bst3, as.matrix(testdata))

R-package/tests/run_lint.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ my_linters <- list(
66
assignment_linter = lintr::assignment_linter,
77
closed_curly_linter = lintr::closed_curly_linter,
88
commas_linter = lintr::commas_linter,
9-
# commented_code_linter = lintr::commented_code_linter,
9+
equals_na = lintr::equals_na_linter,
1010
infix_spaces_linter = lintr::infix_spaces_linter,
1111
line_length_linter = lintr::line_length_linter,
1212
no_tab_linter = lintr::no_tab_linter,
1313
object_usage_linter = lintr::object_usage_linter,
14-
# snake_case_linter = lintr::snake_case_linter,
15-
# multiple_dots_linter = lintr::multiple_dots_linter,
1614
object_length_linter = lintr::object_length_linter,
1715
open_curly_linter = lintr::open_curly_linter,
18-
# single_quotes_linter = lintr::single_quotes_linter,
16+
semicolon = lintr::semicolon_terminator_linter,
17+
seq = lintr::seq_linter,
1918
spaces_inside_linter = lintr::spaces_inside_linter,
2019
spaces_left_parentheses_linter = lintr::spaces_left_parentheses_linter,
2120
trailing_blank_lines_linter = lintr::trailing_blank_lines_linter,
2221
trailing_whitespace_linter = lintr::trailing_whitespace_linter,
23-
true_false = lintr::T_and_F_symbol_linter
22+
true_false = lintr::T_and_F_symbol_linter,
23+
unneeded_concatenation = lintr::unneeded_concatenation_linter
2424
)
2525

2626
results <- lapply(

R-package/tests/testthat/test_dmatrix.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ test_that("xgb.DMatrix: colnames", {
9999
dtest <- xgb.DMatrix(test_data, label = test_label)
100100
expect_equal(colnames(dtest), colnames(test_data))
101101
expect_error(colnames(dtest) <- 'asdf')
102-
new_names <- make.names(1:ncol(test_data))
102+
new_names <- make.names(seq_len(ncol(test_data)))
103103
expect_silent(colnames(dtest) <- new_names)
104104
expect_equal(colnames(dtest), new_names)
105105
expect_silent(colnames(dtest) <- NULL)

R-package/tests/testthat/test_helpers.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ test_that("SHAPs sum to predictions, with or without DART", {
174174

175175
expect_equal(rowSums(shap), pred, tol = tol)
176176
expect_equal(apply(shapi, 1, sum), pred, tol = tol)
177-
for (i in 1 : nrow(d))
177+
for (i in seq_len(nrow(d)))
178178
for (f in list(rowSums, colSums))
179179
expect_equal(f(shapi[i, , ]), shap[i, ], tol = tol)
180180
}

0 commit comments

Comments
 (0)