[R] Don't write files to user's directory (#9966)
This commit is contained in:
@@ -38,13 +38,14 @@ bst <- xgb.train(data = xgb.DMatrix(agaricus.train$data, label = agaricus.train$
|
||||
objective = "binary:logistic")
|
||||
|
||||
# Save as a stand-alone file; load it with xgb.load()
|
||||
xgb.save(bst, 'xgb.model')
|
||||
bst2 <- xgb.load('xgb.model')
|
||||
fname <- file.path(tempdir(), "xgb_model.ubj")
|
||||
xgb.save(bst, fname)
|
||||
bst2 <- xgb.load(fname)
|
||||
|
||||
# Save as a stand-alone file (JSON); load it with xgb.load()
|
||||
xgb.save(bst, 'xgb.model.json')
|
||||
bst2 <- xgb.load('xgb.model.json')
|
||||
if (file.exists('xgb.model.json')) file.remove('xgb.model.json')
|
||||
fname <- file.path(tempdir(), "xgb_model.json")
|
||||
xgb.save(bst, fname)
|
||||
bst2 <- xgb.load(fname)
|
||||
|
||||
# Save as a raw byte vector; load it with xgb.load.raw()
|
||||
xgb_bytes <- xgb.save.raw(bst)
|
||||
@@ -55,11 +56,11 @@ obj <- list(xgb_model_bytes = xgb.save.raw(bst), description = "My first XGBoost
|
||||
# Persist the R object. Here, saveRDS() is okay, since it doesn't persist
|
||||
# xgb.Booster directly. What's being persisted is the future-proof byte representation
|
||||
# as given by xgb.save.raw().
|
||||
saveRDS(obj, 'my_object.rds')
|
||||
fname <- file.path(tempdir(), "my_object.Rds")
|
||||
saveRDS(obj, fname)
|
||||
# Read back the R object
|
||||
obj2 <- readRDS('my_object.rds')
|
||||
obj2 <- readRDS(fname)
|
||||
# Re-construct xgb.Booster object from the bytes
|
||||
bst2 <- xgb.load.raw(obj2$xgb_model_bytes)
|
||||
if (file.exists('my_object.rds')) file.remove('my_object.rds')
|
||||
|
||||
}
|
||||
|
||||
@@ -45,12 +45,12 @@ bst <- xgboost(
|
||||
objective = "binary:logistic"
|
||||
)
|
||||
|
||||
saveRDS(bst, "xgb.model.rds")
|
||||
fname <- file.path(tempdir(), "xgb_model.Rds")
|
||||
saveRDS(bst, fname)
|
||||
|
||||
# Warning: The resulting RDS file is only compatible with the current XGBoost version.
|
||||
# Refer to the section titled "a-compatibility-note-for-saveRDS-save".
|
||||
bst1 <- readRDS("xgb.model.rds")
|
||||
if (file.exists("xgb.model.rds")) file.remove("xgb.model.rds")
|
||||
bst1 <- readRDS(fname)
|
||||
# the handle is invalid:
|
||||
print(bst1$handle)
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ data.table::setDTthreads(nthread)
|
||||
dtrain <- with(
|
||||
agaricus.train, xgb.DMatrix(data, label = label, nthread = nthread)
|
||||
)
|
||||
xgb.DMatrix.save(dtrain, 'xgb.DMatrix.data')
|
||||
dtrain <- xgb.DMatrix('xgb.DMatrix.data')
|
||||
if (file.exists('xgb.DMatrix.data')) file.remove('xgb.DMatrix.data')
|
||||
fname <- file.path(tempdir(), "xgb.DMatrix.data")
|
||||
xgb.DMatrix.save(dtrain, fname)
|
||||
dtrain <- xgb.DMatrix(fname)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ Save xgb.DMatrix object to binary file
|
||||
\examples{
|
||||
data(agaricus.train, package='xgboost')
|
||||
dtrain <- with(agaricus.train, xgb.DMatrix(data, label = label, nthread = 2))
|
||||
xgb.DMatrix.save(dtrain, 'xgb.DMatrix.data')
|
||||
dtrain <- xgb.DMatrix('xgb.DMatrix.data')
|
||||
if (file.exists('xgb.DMatrix.data')) file.remove('xgb.DMatrix.data')
|
||||
fname <- file.path(tempdir(), "xgb.DMatrix.data")
|
||||
xgb.DMatrix.save(dtrain, fname)
|
||||
dtrain <- xgb.DMatrix(fname)
|
||||
}
|
||||
|
||||
@@ -79,9 +79,9 @@ xgb.attr(bst, "my_attribute") <- "my attribute value"
|
||||
print(xgb.attr(bst, "my_attribute"))
|
||||
xgb.attributes(bst) <- list(a = 123, b = "abc")
|
||||
|
||||
xgb.save(bst, "xgb.model")
|
||||
bst1 <- xgb.load("xgb.model")
|
||||
if (file.exists("xgb.model")) file.remove("xgb.model")
|
||||
fname <- file.path(tempdir(), "xgb.ubj")
|
||||
xgb.save(bst, fname)
|
||||
bst1 <- xgb.load(fname)
|
||||
print(xgb.attr(bst1, "my_attribute"))
|
||||
print(xgb.attributes(bst1))
|
||||
|
||||
|
||||
@@ -43,9 +43,9 @@ bst <- xgb.train(
|
||||
objective = "binary:logistic"
|
||||
)
|
||||
|
||||
xgb.save(bst, 'xgb.model')
|
||||
bst <- xgb.load('xgb.model')
|
||||
if (file.exists('xgb.model')) file.remove('xgb.model')
|
||||
fname <- file.path(tempdir(), "xgb.ubj")
|
||||
xgb.save(bst, fname)
|
||||
bst <- xgb.load(fname)
|
||||
}
|
||||
\seealso{
|
||||
\code{\link{xgb.save}}, \code{\link{xgb.Booster.complete}}.
|
||||
|
||||
@@ -46,9 +46,9 @@ bst <- xgb.train(
|
||||
nrounds = 2,
|
||||
objective = "binary:logistic"
|
||||
)
|
||||
xgb.save(bst, 'xgb.model')
|
||||
bst <- xgb.load('xgb.model')
|
||||
if (file.exists('xgb.model')) file.remove('xgb.model')
|
||||
fname <- file.path(tempdir(), "xgb.ubj")
|
||||
xgb.save(bst, fname)
|
||||
bst <- xgb.load(fname)
|
||||
}
|
||||
\seealso{
|
||||
\code{\link{xgb.load}}, \code{\link{xgb.Booster.complete}}.
|
||||
|
||||
Reference in New Issue
Block a user