Unverified Commit a2b60e84 authored by david-cortes's avatar david-cortes Committed by GitHub
Browse files

[R-package] expand user paths in file names (#4687)

parent d88b4456
...@@ -76,6 +76,8 @@ Booster <- R6::R6Class( ...@@ -76,6 +76,8 @@ Booster <- R6::R6Class(
stop("lgb.Booster: Can only use a string as model file path") stop("lgb.Booster: Can only use a string as model file path")
} }
modelfile <- path.expand(modelfile)
# Create booster from model # Create booster from model
handle <- .Call( handle <- .Call(
LGBM_BoosterCreateFromModelfile_R LGBM_BoosterCreateFromModelfile_R
...@@ -425,6 +427,8 @@ Booster <- R6::R6Class( ...@@ -425,6 +427,8 @@ Booster <- R6::R6Class(
num_iteration <- self$best_iter num_iteration <- self$best_iter
} }
filename <- path.expand(filename)
.Call( .Call(
LGBM_BoosterSaveModel_R LGBM_BoosterSaveModel_R
, private$handle , private$handle
...@@ -857,6 +861,7 @@ lgb.load <- function(filename = NULL, model_str = NULL) { ...@@ -857,6 +861,7 @@ lgb.load <- function(filename = NULL, model_str = NULL) {
if (!is.character(filename)) { if (!is.character(filename)) {
stop("lgb.load: filename should be character") stop("lgb.load: filename should be character")
} }
filename <- path.expand(filename)
if (!file.exists(filename)) { if (!file.exists(filename)) {
stop(sprintf("lgb.load: file '%s' passed to filename does not exist", filename)) stop(sprintf("lgb.load: file '%s' passed to filename does not exist", filename))
} }
...@@ -917,6 +922,7 @@ lgb.save <- function(booster, filename, num_iteration = NULL) { ...@@ -917,6 +922,7 @@ lgb.save <- function(booster, filename, num_iteration = NULL) {
if (!(is.character(filename) && length(filename) == 1L)) { if (!(is.character(filename) && length(filename) == 1L)) {
stop("lgb.save: filename should be a string") stop("lgb.save: filename should be a string")
} }
filename <- path.expand(filename)
# Store booster # Store booster
return( return(
......
...@@ -243,7 +243,7 @@ Dataset <- R6::R6Class( ...@@ -243,7 +243,7 @@ Dataset <- R6::R6Class(
handle <- .Call( handle <- .Call(
LGBM_DatasetCreateFromFile_R LGBM_DatasetCreateFromFile_R
, private$raw_data , path.expand(private$raw_data)
, params_str , params_str
, ref_handle , ref_handle
) )
...@@ -742,7 +742,7 @@ Dataset <- R6::R6Class( ...@@ -742,7 +742,7 @@ Dataset <- R6::R6Class(
.Call( .Call(
LGBM_DatasetSaveBinary_R LGBM_DatasetSaveBinary_R
, private$handle , private$handle
, fname , path.expand(fname)
) )
return(invisible(self)) return(invisible(self))
} }
......
...@@ -35,7 +35,7 @@ Predictor <- R6::R6Class( ...@@ -35,7 +35,7 @@ Predictor <- R6::R6Class(
# Create handle on it # Create handle on it
handle <- .Call( handle <- .Call(
LGBM_BoosterCreateFromModelfile_R LGBM_BoosterCreateFromModelfile_R
, modelfile , path.expand(modelfile)
) )
private$need_free_handle <- TRUE private$need_free_handle <- TRUE
...@@ -96,6 +96,8 @@ Predictor <- R6::R6Class( ...@@ -96,6 +96,8 @@ Predictor <- R6::R6Class(
# Check if data is a file name and not a matrix # Check if data is a file name and not a matrix
if (identical(class(data), "character") && length(data) == 1L) { 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 # Data is a filename, create a temporary file with a "lightgbm_" pattern in it
tmp_filename <- tempfile(pattern = "lightgbm_") tmp_filename <- tempfile(pattern = "lightgbm_")
on.exit(unlink(tmp_filename), add = TRUE) on.exit(unlink(tmp_filename), add = TRUE)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment