diff --git a/R-package/R/xgb.Booster.R b/R-package/R/xgb.Booster.R index 080067039c62..6a53577e990f 100644 --- a/R-package/R/xgb.Booster.R +++ b/R-package/R/xgb.Booster.R @@ -12,7 +12,7 @@ xgb.Booster.handle <- function(params = list(), cachelist = list(), ## A filename handle <- .Call(XGBoosterCreate_R, cachelist) modelfile <- path.expand(modelfile) - .Call(XGBoosterLoadModel_R, handle, modelfile[1]) + .Call(XGBoosterLoadModel_R, handle, enc2utf8(modelfile[1])) class(handle) <- "xgb.Booster.handle" if (length(params) > 0) { xgb.parameters(handle) <- params diff --git a/R-package/R/xgb.save.R b/R-package/R/xgb.save.R index 42ecb4153a84..14be0f065ff8 100644 --- a/R-package/R/xgb.save.R +++ b/R-package/R/xgb.save.R @@ -43,6 +43,6 @@ xgb.save <- function(model, fname) { } model <- xgb.Booster.complete(model, saveraw = FALSE) fname <- path.expand(fname) - .Call(XGBoosterSaveModel_R, model$handle, fname[1]) + .Call(XGBoosterSaveModel_R, model$handle, enc2utf8(fname[1])) return(TRUE) } diff --git a/R-package/tests/testthat/test_unicode.R b/R-package/tests/testthat/test_unicode.R new file mode 100644 index 000000000000..cac544ef9fc9 --- /dev/null +++ b/R-package/tests/testthat/test_unicode.R @@ -0,0 +1,21 @@ +context("Test Unicode handling") + +data(agaricus.train, package = 'xgboost') +data(agaricus.test, package = 'xgboost') +train <- agaricus.train +test <- agaricus.test +set.seed(1994) + +test_that("Can save and load models with Unicode paths", { + nrounds <- 2 + bst <- xgboost(data = train$data, label = train$label, max_depth = 2, + eta = 1, nthread = 2, nrounds = nrounds, objective = "binary:logistic", + eval_metric = "error") + tmpdir <- tempdir() + lapply(c("모델.json", "がうる・ぐら.json", "类继承.ubj"), function(x) { + path <- file.path(tmpdir, x) + xgb.save(bst, path) + bst2 <- xgb.load(path) + expect_equal(predict(bst, test$data), predict(bst2, test$data)) + }) +})