Skip to content

Commit

Permalink
[R-package] remove unused code checking has_header in Dataset() (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Aug 25, 2021
1 parent bd28a36 commit f5925c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
10 changes: 0 additions & 10 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,6 @@ Dataset <- R6::R6Class(

}

# Check has header or not
has_header <- FALSE
if (!is.null(private$params$has_header) || !is.null(private$params$header)) {
params_has_header <- tolower(as.character(private$params$has_header)) == "true"
params_header <- tolower(as.character(private$params$header)) == "true"
if (params_has_header || params_header) {
has_header <- TRUE
}
}

# Generate parameter str
params_str <- lgb.params2str(params = private$params)

Expand Down
42 changes: 42 additions & 0 deletions R-package/tests/testthat/test_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,45 @@ test_that("lgb.Dataset: should be able to use and retrieve long feature names",
expect_equal(col_names[1L], long_name)
expect_equal(nchar(col_names[1L]), 1000L)
})

test_that("lgb.Dataset: should be able to create a Dataset from a text file with a header", {
train_file <- tempfile(pattern = "train_", fileext = ".csv")
write.table(
data.frame(y = rnorm(100L), x1 = rnorm(100L), x2 = rnorm(100L))
, file = train_file
, sep = ","
, col.names = TRUE
, row.names = FALSE
, quote = FALSE
)

dtrain <- lgb.Dataset(
data = train_file
, params = list(header = TRUE)
)
dtrain$construct()
expect_identical(dtrain$get_colnames(), c("x1", "x2"))
expect_identical(dtrain$get_params(), list(header = TRUE))
expect_identical(dtrain$dim(), c(100L, 2L))
})

test_that("lgb.Dataset: should be able to create a Dataset from a text file without a header", {
train_file <- tempfile(pattern = "train_", fileext = ".csv")
write.table(
data.frame(y = rnorm(100L), x1 = rnorm(100L), x2 = rnorm(100L))
, file = train_file
, sep = ","
, col.names = FALSE
, row.names = FALSE
, quote = FALSE
)

dtrain <- lgb.Dataset(
data = train_file
, params = list(header = FALSE)
)
dtrain$construct()
expect_identical(dtrain$get_colnames(), c("Column_0", "Column_1"))
expect_identical(dtrain$get_params(), list(header = FALSE))
expect_identical(dtrain$dim(), c(100L, 2L))
})

0 comments on commit f5925c3

Please sign in to comment.