C part export a model dump string

This commit is contained in:
El Potaeto 2015-01-08 23:47:00 +01:00
parent 3d0bbae2c2
commit 6fd8bbe71a
3 changed files with 14 additions and 12 deletions

View File

@ -32,6 +32,8 @@ xgb.dump <- function(model, fname, fmap = "", with.stats=FALSE) {
if (typeof(fname) != "character") { if (typeof(fname) != "character") {
stop("xgb.dump: second argument must be type character") stop("xgb.dump: second argument must be type character")
} }
.Call("XGBoosterDumpModel_R", model, fname, fmap, as.integer(with.stats), PACKAGE = "xgboost") result <- .Call("XGBoosterDumpModel_R", model, fmap, as.integer(with.stats), PACKAGE = "xgboost")
writeLines(result, fname)
#unlist(str_split(a, "\n"))==""
return(TRUE) return(TRUE)
} }

View File

@ -272,20 +272,21 @@ extern "C" {
XGBoosterSaveModel(R_ExternalPtrAddr(handle), CHAR(asChar(fname))); XGBoosterSaveModel(R_ExternalPtrAddr(handle), CHAR(asChar(fname)));
_WrapperEnd(); _WrapperEnd();
} }
void XGBoosterDumpModel_R(SEXP handle, SEXP fname, SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats) {
SEXP fmap, SEXP with_stats) {
_WrapperBegin(); _WrapperBegin();
bst_ulong olen; bst_ulong olen;
const char **res = XGBoosterDumpModel(R_ExternalPtrAddr(handle), const char **res = XGBoosterDumpModel(R_ExternalPtrAddr(handle),
CHAR(asChar(fmap)), CHAR(asChar(fmap)),
asInteger(with_stats), asInteger(with_stats),
&olen); &olen);
FILE *fo = utils::FopenCheck(CHAR(asChar(fname)), "w"); SEXP out = PROTECT(allocVector(STRSXP, olen));
char buffer [2000];
for (size_t i = 0; i < olen; ++i) { for (size_t i = 0; i < olen; ++i) {
fprintf(fo, "booster[%u]:\n", static_cast<unsigned>(i)); sprintf (buffer, "booster[%u]:\n%s", static_cast<unsigned>(i), res[i]);
fprintf(fo, "%s", res[i]); SET_STRING_ELT(out, i, mkChar(buffer));
} }
fclose(fo);
_WrapperEnd(); _WrapperEnd();
UNPROTECT(1);
return out;
} }
} }

View File

@ -128,12 +128,11 @@ extern "C" {
*/ */
void XGBoosterSaveModel_R(SEXP handle, SEXP fname); void XGBoosterSaveModel_R(SEXP handle, SEXP fname);
/*! /*!
* \brief dump model into text file * \brief dump model into a string
* \param handle handle * \param handle handle
* \param fname file name of model that can be dumped into
* \param fmap name to fmap can be empty string * \param fmap name to fmap can be empty string
* \param with_stats whether dump statistics of splits * \param with_stats whether dump statistics of splits
*/ */
void XGBoosterDumpModel_R(SEXP handle, SEXP fname, SEXP fmap, SEXP with_stats); SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats);
} }
#endif // XGBOOST_WRAPPER_R_H_ #endif // XGBOOST_WRAPPER_R_H_