[R] rename slice to avoid dplyr conflict (#10017)

This commit is contained in:
david-cortes 2024-01-30 22:33:37 +01:00 committed by GitHub
parent df7cf744b4
commit 5e00a71671
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 24 deletions

View File

@ -15,7 +15,6 @@ S3method(print,xgb.DMatrix)
S3method(print,xgb.cv.synchronous)
S3method(setinfo,xgb.Booster)
S3method(setinfo,xgb.DMatrix)
S3method(slice,xgb.DMatrix)
S3method(variable.names,xgb.Booster)
export("xgb.attr<-")
export("xgb.attributes<-")
@ -30,7 +29,6 @@ export(cb.reset.parameters)
export(cb.save.model)
export(getinfo)
export(setinfo)
export(slice)
export(xgb.DMatrix)
export(xgb.DMatrix.hasinfo)
export(xgb.DMatrix.save)
@ -70,6 +68,7 @@ export(xgb.save)
export(xgb.save.raw)
export(xgb.set.config)
export(xgb.slice.Booster)
export(xgb.slice.DMatrix)
export(xgb.train)
export(xgboost)
import(methods)

View File

@ -1228,19 +1228,15 @@ xgb.get.DMatrix.data <- function(dmat) {
#' data(agaricus.train, package='xgboost')
#' dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
#'
#' dsub <- slice(dtrain, 1:42)
#' dsub <- xgb.slice.DMatrix(dtrain, 1:42)
#' labels1 <- getinfo(dsub, 'label')
#' dsub <- dtrain[1:42, ]
#' labels2 <- getinfo(dsub, 'label')
#' all.equal(labels1, labels2)
#'
#' @rdname slice.xgb.DMatrix
#' @rdname xgb.slice.DMatrix
#' @export
slice <- function(object, idxset) UseMethod("slice")
#' @rdname slice.xgb.DMatrix
#' @export
slice.xgb.DMatrix <- function(object, idxset) {
xgb.slice.DMatrix <- function(object, idxset) {
if (!inherits(object, "xgb.DMatrix")) {
stop("object must be xgb.DMatrix")
}
@ -1264,10 +1260,10 @@ slice.xgb.DMatrix <- function(object, idxset) {
return(structure(ret, class = "xgb.DMatrix"))
}
#' @rdname slice.xgb.DMatrix
#' @rdname xgb.slice.DMatrix
#' @export
`[.xgb.DMatrix` <- function(object, idxset, colset = NULL) {
slice(object, idxset)
xgb.slice.DMatrix(object, idxset)
}

View File

@ -197,12 +197,12 @@ xgb.cv <- function(params = list(), data, nrounds, nfold, label = NULL, missing
nthread = params$nthread
)
bst_folds <- lapply(seq_along(folds), function(k) {
dtest <- slice(dall, folds[[k]])
dtest <- xgb.slice.DMatrix(dall, folds[[k]])
# code originally contributed by @RolandASc on stackoverflow
if (is.null(train_folds))
dtrain <- slice(dall, unlist(folds[-k]))
dtrain <- xgb.slice.DMatrix(dall, unlist(folds[-k]))
else
dtrain <- slice(dall, train_folds[[k]])
dtrain <- xgb.slice.DMatrix(dall, train_folds[[k]])
bst <- xgb.Booster(
params = params,
cachelist = list(dtrain, dtest),

View File

@ -1,15 +1,12 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xgb.DMatrix.R
\name{slice}
\alias{slice}
\alias{slice.xgb.DMatrix}
\name{xgb.slice.DMatrix}
\alias{xgb.slice.DMatrix}
\alias{[.xgb.DMatrix}
\title{Get a new DMatrix containing the specified rows of
original xgb.DMatrix object}
\usage{
slice(object, idxset)
\method{slice}{xgb.DMatrix}(object, idxset)
xgb.slice.DMatrix(object, idxset)
\method{[}{xgb.DMatrix}(object, idxset, colset = NULL)
}
@ -28,7 +25,7 @@ original xgb.DMatrix object
data(agaricus.train, package='xgboost')
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
dsub <- slice(dtrain, 1:42)
dsub <- xgb.slice.DMatrix(dtrain, 1:42)
labels1 <- getinfo(dsub, 'label')
dsub <- dtrain[1:42, ]
labels2 <- getinfo(dsub, 'label')

View File

@ -166,7 +166,7 @@ test_that("xgb.DMatrix: getinfo & setinfo", {
test_that("xgb.DMatrix: slice, dim", {
dtest <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads)
expect_equal(dim(dtest), dim(test_data))
dsub1 <- slice(dtest, 1:42)
dsub1 <- xgb.slice.DMatrix(dtest, 1:42)
expect_equal(nrow(dsub1), 42)
expect_equal(ncol(dsub1), ncol(test_data))
@ -182,12 +182,12 @@ test_that("xgb.DMatrix: slice, trailing empty rows", {
dtrain <- xgb.DMatrix(
data = train_data, label = train_label, nthread = n_threads
)
slice(dtrain, 6513L)
xgb.slice.DMatrix(dtrain, 6513L)
train_data[6513, ] <- 0
dtrain <- xgb.DMatrix(
data = train_data, label = train_label, nthread = n_threads
)
slice(dtrain, 6513L)
xgb.slice.DMatrix(dtrain, 6513L)
expect_equal(nrow(dtrain), 6513)
})