[R-package] JSON dump format and a couple of bugfixes (#1855)
* [R-package] JSON tree dump interface * [R-package] precision bugfix in xgb.attributes * [R-package] bugfix for cb.early.stop called from xgb.cv * [R-package] a bit more clarity on labels checking in xgb.cv * [R-package] test JSON dump for gblinear as well * whitespace lint
This commit is contained in:
committed by
Tianqi Chen
parent
0268dedeea
commit
b21e658a02
@@ -350,20 +350,37 @@ SEXP XGBoosterModelToRaw_R(SEXP handle) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats) {
|
||||
SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats, SEXP dump_format) {
|
||||
SEXP out;
|
||||
R_API_BEGIN();
|
||||
bst_ulong olen;
|
||||
const char **res;
|
||||
CHECK_CALL(XGBoosterDumpModel(R_ExternalPtrAddr(handle),
|
||||
const char *fmt = CHAR(asChar(dump_format));
|
||||
CHECK_CALL(XGBoosterDumpModelEx(R_ExternalPtrAddr(handle),
|
||||
CHAR(asChar(fmap)),
|
||||
asInteger(with_stats),
|
||||
fmt,
|
||||
&olen, &res));
|
||||
out = PROTECT(allocVector(STRSXP, olen));
|
||||
for (size_t i = 0; i < olen; ++i) {
|
||||
if (!strcmp("json", fmt)) {
|
||||
std::stringstream stream;
|
||||
stream << "booster[" << i <<"]\n" << res[i];
|
||||
SET_STRING_ELT(out, i, mkChar(stream.str().c_str()));
|
||||
stream << "[\n";
|
||||
for (size_t i = 0; i < olen; ++i) {
|
||||
stream << res[i];
|
||||
if (i < olen - 1) {
|
||||
stream << ",\n";
|
||||
} else {
|
||||
stream << "\n";
|
||||
}
|
||||
}
|
||||
stream << "]";
|
||||
SET_STRING_ELT(out, 0, mkChar(stream.str().c_str()));
|
||||
} else {
|
||||
for (size_t i = 0; i < olen; ++i) {
|
||||
std::stringstream stream;
|
||||
stream << "booster[" << i <<"]\n" << res[i];
|
||||
SET_STRING_ELT(out, i, mkChar(stream.str().c_str()));
|
||||
}
|
||||
}
|
||||
R_API_END();
|
||||
UNPROTECT(1);
|
||||
|
||||
Reference in New Issue
Block a user