Skip to content

Commit

Permalink
[R-package] expand user paths in file names (#4687)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cortes authored Oct 22, 2021
1 parent d88b445 commit a2b60e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 6 additions & 0 deletions R-package/R/lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Booster <- R6::R6Class(
stop("lgb.Booster: Can only use a string as model file path")
}

modelfile <- path.expand(modelfile)

# Create booster from model
handle <- .Call(
LGBM_BoosterCreateFromModelfile_R
Expand Down Expand Up @@ -425,6 +427,8 @@ Booster <- R6::R6Class(
num_iteration <- self$best_iter
}

filename <- path.expand(filename)

.Call(
LGBM_BoosterSaveModel_R
, private$handle
Expand Down Expand Up @@ -857,6 +861,7 @@ lgb.load <- function(filename = NULL, model_str = NULL) {
if (!is.character(filename)) {
stop("lgb.load: filename should be character")
}
filename <- path.expand(filename)
if (!file.exists(filename)) {
stop(sprintf("lgb.load: file '%s' passed to filename does not exist", filename))
}
Expand Down Expand Up @@ -917,6 +922,7 @@ lgb.save <- function(booster, filename, num_iteration = NULL) {
if (!(is.character(filename) && length(filename) == 1L)) {
stop("lgb.save: filename should be a string")
}
filename <- path.expand(filename)

# Store booster
return(
Expand Down
4 changes: 2 additions & 2 deletions R-package/R/lgb.Dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Dataset <- R6::R6Class(

handle <- .Call(
LGBM_DatasetCreateFromFile_R
, private$raw_data
, path.expand(private$raw_data)
, params_str
, ref_handle
)
Expand Down Expand Up @@ -742,7 +742,7 @@ Dataset <- R6::R6Class(
.Call(
LGBM_DatasetSaveBinary_R
, private$handle
, fname
, path.expand(fname)
)
return(invisible(self))
}
Expand Down
4 changes: 3 additions & 1 deletion R-package/R/lgb.Predictor.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Predictor <- R6::R6Class(
# Create handle on it
handle <- .Call(
LGBM_BoosterCreateFromModelfile_R
, modelfile
, path.expand(modelfile)
)
private$need_free_handle <- TRUE

Expand Down Expand Up @@ -96,6 +96,8 @@ Predictor <- R6::R6Class(
# Check if data is a file name and not a matrix
if (identical(class(data), "character") && length(data) == 1L) {

data <- path.expand(data)

# Data is a filename, create a temporary file with a "lightgbm_" pattern in it
tmp_filename <- tempfile(pattern = "lightgbm_")
on.exit(unlink(tmp_filename), add = TRUE)
Expand Down

0 comments on commit a2b60e8

Please sign in to comment.