From a2b60e84c17462d6e467f4363a0fae203ca7fee5 Mon Sep 17 00:00:00 2001 From: david-cortes Date: Fri, 22 Oct 2021 06:40:14 +0300 Subject: [PATCH] [R-package] expand user paths in file names (#4687) --- R-package/R/lgb.Booster.R | 6 ++++++ R-package/R/lgb.Dataset.R | 4 ++-- R-package/R/lgb.Predictor.R | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/R-package/R/lgb.Booster.R b/R-package/R/lgb.Booster.R index 586f3a38899d..5214118a3a42 100644 --- a/R-package/R/lgb.Booster.R +++ b/R-package/R/lgb.Booster.R @@ -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 @@ -425,6 +427,8 @@ Booster <- R6::R6Class( num_iteration <- self$best_iter } + filename <- path.expand(filename) + .Call( LGBM_BoosterSaveModel_R , private$handle @@ -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)) } @@ -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( diff --git a/R-package/R/lgb.Dataset.R b/R-package/R/lgb.Dataset.R index 13310ea7e3ff..f6e66908bba3 100644 --- a/R-package/R/lgb.Dataset.R +++ b/R-package/R/lgb.Dataset.R @@ -243,7 +243,7 @@ Dataset <- R6::R6Class( handle <- .Call( LGBM_DatasetCreateFromFile_R - , private$raw_data + , path.expand(private$raw_data) , params_str , ref_handle ) @@ -742,7 +742,7 @@ Dataset <- R6::R6Class( .Call( LGBM_DatasetSaveBinary_R , private$handle - , fname + , path.expand(fname) ) return(invisible(self)) } diff --git a/R-package/R/lgb.Predictor.R b/R-package/R/lgb.Predictor.R index 147bed11ae7f..c27238825a17 100644 --- a/R-package/R/lgb.Predictor.R +++ b/R-package/R/lgb.Predictor.R @@ -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 @@ -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)