Skip to content

Commit 819098a

Browse files
authored
[R] Handle UTF-8 paths on Windows (dmlc#9448)
1 parent c1b2cff commit 819098a

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

R-package/R/xgb.Booster.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ xgb.Booster.handle <- function(params = list(), cachelist = list(),
1212
## A filename
1313
handle <- .Call(XGBoosterCreate_R, cachelist)
1414
modelfile <- path.expand(modelfile)
15-
.Call(XGBoosterLoadModel_R, handle, modelfile[1])
15+
.Call(XGBoosterLoadModel_R, handle, enc2utf8(modelfile[1]))
1616
class(handle) <- "xgb.Booster.handle"
1717
if (length(params) > 0) {
1818
xgb.parameters(handle) <- params

R-package/R/xgb.save.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ xgb.save <- function(model, fname) {
4343
}
4444
model <- xgb.Booster.complete(model, saveraw = FALSE)
4545
fname <- path.expand(fname)
46-
.Call(XGBoosterSaveModel_R, model$handle, fname[1])
46+
.Call(XGBoosterSaveModel_R, model$handle, enc2utf8(fname[1]))
4747
return(TRUE)
4848
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
context("Test Unicode handling")
2+
3+
data(agaricus.train, package = 'xgboost')
4+
data(agaricus.test, package = 'xgboost')
5+
train <- agaricus.train
6+
test <- agaricus.test
7+
set.seed(1994)
8+
9+
test_that("Can save and load models with Unicode paths", {
10+
nrounds <- 2
11+
bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
12+
eta = 1, nthread = 2, nrounds = nrounds, objective = "binary:logistic",
13+
eval_metric = "error")
14+
tmpdir <- tempdir()
15+
lapply(c("모델.json", "がうる・ぐら.json", "类继承.ubj"), function(x) {
16+
path <- file.path(tmpdir, x)
17+
xgb.save(bst, path)
18+
bst2 <- xgb.load(path)
19+
expect_equal(predict(bst, test$data), predict(bst2, test$data))
20+
})
21+
})

0 commit comments

Comments
 (0)