[R] use Rf_ prefix for R C functions. (#10465)

This commit is contained in:
david-cortes 2024-06-21 08:37:18 +02:00 committed by GitHub
parent 26eb68859f
commit 61ac8eec8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 84 deletions

View File

@ -352,7 +352,7 @@ void _DMatrixFinalizer(SEXP ext) {
XGB_DLL SEXP XGBSetGlobalConfig_R(SEXP json_str) { XGB_DLL SEXP XGBSetGlobalConfig_R(SEXP json_str) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGBSetGlobalConfig(CHAR(asChar(json_str)))); CHECK_CALL(XGBSetGlobalConfig(CHAR(Rf_asChar(json_str))));
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
@ -362,7 +362,7 @@ XGB_DLL SEXP XGBGetGlobalConfig_R() {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGBGetGlobalConfig(&json_str)); CHECK_CALL(XGBGetGlobalConfig(&json_str));
R_API_END(); R_API_END();
return mkString(json_str); return Rf_mkString(json_str);
} }
XGB_DLL SEXP XGDMatrixCreateFromURI_R(SEXP uri, SEXP silent, SEXP data_split_mode) { XGB_DLL SEXP XGDMatrixCreateFromURI_R(SEXP uri, SEXP silent, SEXP data_split_mode) {
@ -385,7 +385,7 @@ XGB_DLL SEXP XGDMatrixCreateFromURI_R(SEXP uri, SEXP silent, SEXP data_split_mod
} }
XGB_DLL SEXP XGDMatrixCreateFromMat_R(SEXP mat, SEXP missing, SEXP n_threads) { XGB_DLL SEXP XGDMatrixCreateFromMat_R(SEXP mat, SEXP missing, SEXP n_threads) {
SEXP ret = PROTECT(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue)); SEXP ret = Rf_protect(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue));
R_API_BEGIN(); R_API_BEGIN();
DMatrixHandle handle; DMatrixHandle handle;
@ -400,7 +400,7 @@ XGB_DLL SEXP XGDMatrixCreateFromMat_R(SEXP mat, SEXP missing, SEXP n_threads) {
R_SetExternalPtrAddr(ret, handle); R_SetExternalPtrAddr(ret, handle);
R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE); R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE);
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
@ -413,8 +413,8 @@ XGB_DLL SEXP XGDMatrixCreateFromDF_R(SEXP df, SEXP missing, SEXP n_threads) {
{ {
const std::string sinterface = MakeArrayInterfaceFromRDataFrame(df); const std::string sinterface = MakeArrayInterfaceFromRDataFrame(df);
xgboost::Json jconfig{xgboost::Object{}}; xgboost::Json jconfig{xgboost::Object{}};
jconfig["missing"] = asReal(missing); jconfig["missing"] = Rf_asReal(missing);
jconfig["nthread"] = asInteger(n_threads); jconfig["nthread"] = Rf_asInteger(n_threads);
std::string sconfig = xgboost::Json::Dump(jconfig); std::string sconfig = xgboost::Json::Dump(jconfig);
rc = XGDMatrixCreateFromColumnar(sinterface.c_str(), sconfig.c_str(), &handle); rc = XGDMatrixCreateFromColumnar(sinterface.c_str(), sconfig.c_str(), &handle);
@ -446,9 +446,9 @@ void CreateFromSparse(SEXP indptr, SEXP indices, SEXP data, std::string *indptr_
XGB_DLL SEXP XGDMatrixCreateFromCSC_R(SEXP indptr, SEXP indices, SEXP data, SEXP num_row, XGB_DLL SEXP XGDMatrixCreateFromCSC_R(SEXP indptr, SEXP indices, SEXP data, SEXP num_row,
SEXP missing, SEXP n_threads) { SEXP missing, SEXP n_threads) {
SEXP ret = PROTECT(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue)); SEXP ret = Rf_protect(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue));
R_API_BEGIN(); R_API_BEGIN();
std::int32_t threads = asInteger(n_threads); std::int32_t threads = Rf_asInteger(n_threads);
DMatrixHandle handle; DMatrixHandle handle;
int res_code; int res_code;
@ -474,15 +474,15 @@ XGB_DLL SEXP XGDMatrixCreateFromCSC_R(SEXP indptr, SEXP indices, SEXP data, SEXP
R_SetExternalPtrAddr(ret, handle); R_SetExternalPtrAddr(ret, handle);
R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE); R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE);
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
XGB_DLL SEXP XGDMatrixCreateFromCSR_R(SEXP indptr, SEXP indices, SEXP data, SEXP num_col, XGB_DLL SEXP XGDMatrixCreateFromCSR_R(SEXP indptr, SEXP indices, SEXP data, SEXP num_col,
SEXP missing, SEXP n_threads) { SEXP missing, SEXP n_threads) {
SEXP ret = PROTECT(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue)); SEXP ret = Rf_protect(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue));
R_API_BEGIN(); R_API_BEGIN();
std::int32_t threads = asInteger(n_threads); std::int32_t threads = Rf_asInteger(n_threads);
DMatrixHandle handle; DMatrixHandle handle;
int res_code; int res_code;
@ -508,12 +508,12 @@ XGB_DLL SEXP XGDMatrixCreateFromCSR_R(SEXP indptr, SEXP indices, SEXP data, SEXP
R_SetExternalPtrAddr(ret, handle); R_SetExternalPtrAddr(ret, handle);
R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE); R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE);
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset, SEXP allow_groups) { XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset, SEXP allow_groups) {
SEXP ret = PROTECT(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue)); SEXP ret = Rf_protect(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue));
R_API_BEGIN(); R_API_BEGIN();
R_xlen_t len = Rf_xlength(idxset); R_xlen_t len = Rf_xlength(idxset);
const int *idxset_ = INTEGER(idxset); const int *idxset_ = INTEGER(idxset);
@ -537,22 +537,22 @@ XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset, SEXP allow_groups
R_SetExternalPtrAddr(ret, res); R_SetExternalPtrAddr(ret, res);
R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE); R_RegisterCFinalizerEx(ret, _DMatrixFinalizer, TRUE);
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
XGB_DLL SEXP XGDMatrixSaveBinary_R(SEXP handle, SEXP fname, SEXP silent) { XGB_DLL SEXP XGDMatrixSaveBinary_R(SEXP handle, SEXP fname, SEXP silent) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGDMatrixSaveBinary(R_ExternalPtrAddr(handle), CHECK_CALL(XGDMatrixSaveBinary(R_ExternalPtrAddr(handle),
CHAR(asChar(fname)), CHAR(Rf_asChar(fname)),
asInteger(silent))); Rf_asInteger(silent)));
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
XGB_DLL SEXP XGDMatrixSetInfo_R(SEXP handle, SEXP field, SEXP array) { XGB_DLL SEXP XGDMatrixSetInfo_R(SEXP handle, SEXP field, SEXP array) {
R_API_BEGIN(); R_API_BEGIN();
SEXP field_ = PROTECT(Rf_asChar(field)); SEXP field_ = Rf_protect(Rf_asChar(field));
SEXP arr_dim = Rf_getAttrib(array, R_DimSymbol); SEXP arr_dim = Rf_getAttrib(array, R_DimSymbol);
int res_code; int res_code;
{ {
@ -562,7 +562,7 @@ XGB_DLL SEXP XGDMatrixSetInfo_R(SEXP handle, SEXP field, SEXP array) {
R_ExternalPtrAddr(handle), CHAR(field_), array_str.c_str()); R_ExternalPtrAddr(handle), CHAR(field_), array_str.c_str());
} }
CHECK_CALL(res_code); CHECK_CALL(res_code);
UNPROTECT(1); Rf_unprotect(1);
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
@ -570,11 +570,11 @@ XGB_DLL SEXP XGDMatrixSetInfo_R(SEXP handle, SEXP field, SEXP array) {
XGB_DLL SEXP XGDMatrixSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP array) { XGB_DLL SEXP XGDMatrixSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP array) {
R_API_BEGIN(); R_API_BEGIN();
size_t len{0}; size_t len{0};
if (!isNull(array)) { if (!Rf_isNull(array)) {
len = Rf_xlength(array); len = Rf_xlength(array);
} }
SEXP str_info_holder = PROTECT(Rf_allocVector(VECSXP, len)); SEXP str_info_holder = Rf_protect(Rf_allocVector(VECSXP, len));
if (TYPEOF(array) == STRSXP) { if (TYPEOF(array) == STRSXP) {
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
SET_VECTOR_ELT(str_info_holder, i, STRING_ELT(array, i)); SET_VECTOR_ELT(str_info_holder, i, STRING_ELT(array, i));
@ -585,7 +585,7 @@ XGB_DLL SEXP XGDMatrixSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP array) {
} }
} }
SEXP field_ = PROTECT(Rf_asChar(field)); SEXP field_ = Rf_protect(Rf_asChar(field));
const char *name = CHAR(field_); const char *name = CHAR(field_);
int res_code; int res_code;
{ {
@ -600,7 +600,7 @@ XGB_DLL SEXP XGDMatrixSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP array) {
res_code = XGDMatrixSetStrFeatureInfo(R_ExternalPtrAddr(handle), name, vec.data(), len); res_code = XGDMatrixSetStrFeatureInfo(R_ExternalPtrAddr(handle), name, vec.data(), len);
} }
CHECK_CALL(res_code); CHECK_CALL(res_code);
UNPROTECT(2); Rf_unprotect(2);
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
@ -610,19 +610,19 @@ XGB_DLL SEXP XGDMatrixGetStrFeatureInfo_R(SEXP handle, SEXP field) {
R_API_BEGIN(); R_API_BEGIN();
char const **out_features{nullptr}; char const **out_features{nullptr};
bst_ulong len{0}; bst_ulong len{0};
const char *name = CHAR(asChar(field)); const char *name = CHAR(Rf_asChar(field));
XGDMatrixGetStrFeatureInfo(R_ExternalPtrAddr(handle), name, &len, &out_features); XGDMatrixGetStrFeatureInfo(R_ExternalPtrAddr(handle), name, &len, &out_features);
if (len > 0) { if (len > 0) {
ret = PROTECT(allocVector(STRSXP, len)); ret = Rf_protect(Rf_allocVector(STRSXP, len));
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
SET_STRING_ELT(ret, i, mkChar(out_features[i])); SET_STRING_ELT(ret, i, Rf_mkChar(out_features[i]));
} }
} else { } else {
ret = PROTECT(R_NilValue); ret = Rf_protect(R_NilValue);
} }
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
@ -631,11 +631,11 @@ XGB_DLL SEXP XGDMatrixGetFloatInfo_R(SEXP handle, SEXP field) {
R_API_BEGIN(); R_API_BEGIN();
bst_ulong olen; bst_ulong olen;
const float *res; const float *res;
CHECK_CALL(XGDMatrixGetFloatInfo(R_ExternalPtrAddr(handle), CHAR(asChar(field)), &olen, &res)); CHECK_CALL(XGDMatrixGetFloatInfo(R_ExternalPtrAddr(handle), CHAR(Rf_asChar(field)), &olen, &res));
ret = PROTECT(allocVector(REALSXP, olen)); ret = Rf_protect(Rf_allocVector(REALSXP, olen));
std::copy(res, res + olen, REAL(ret)); std::copy(res, res + olen, REAL(ret));
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
@ -644,11 +644,11 @@ XGB_DLL SEXP XGDMatrixGetUIntInfo_R(SEXP handle, SEXP field) {
R_API_BEGIN(); R_API_BEGIN();
bst_ulong olen; bst_ulong olen;
const unsigned *res; const unsigned *res;
CHECK_CALL(XGDMatrixGetUIntInfo(R_ExternalPtrAddr(handle), CHAR(asChar(field)), &olen, &res)); CHECK_CALL(XGDMatrixGetUIntInfo(R_ExternalPtrAddr(handle), CHAR(Rf_asChar(field)), &olen, &res));
ret = PROTECT(allocVector(INTSXP, olen)); ret = Rf_protect(Rf_allocVector(INTSXP, olen));
std::copy(res, res + olen, INTEGER(ret)); std::copy(res, res + olen, INTEGER(ret));
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
@ -657,7 +657,7 @@ XGB_DLL SEXP XGDMatrixNumRow_R(SEXP handle) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGDMatrixNumRow(R_ExternalPtrAddr(handle), &nrow)); CHECK_CALL(XGDMatrixNumRow(R_ExternalPtrAddr(handle), &nrow));
R_API_END(); R_API_END();
return ScalarInteger(static_cast<int>(nrow)); return Rf_ScalarInteger(static_cast<int>(nrow));
} }
XGB_DLL SEXP XGDMatrixNumCol_R(SEXP handle) { XGB_DLL SEXP XGDMatrixNumCol_R(SEXP handle) {
@ -665,7 +665,7 @@ XGB_DLL SEXP XGDMatrixNumCol_R(SEXP handle) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGDMatrixNumCol(R_ExternalPtrAddr(handle), &ncol)); CHECK_CALL(XGDMatrixNumCol(R_ExternalPtrAddr(handle), &ncol));
R_API_END(); R_API_END();
return ScalarInteger(static_cast<int>(ncol)); return Rf_ScalarInteger(static_cast<int>(ncol));
} }
XGB_DLL SEXP XGProxyDMatrixCreate_R() { XGB_DLL SEXP XGProxyDMatrixCreate_R() {
@ -1171,12 +1171,12 @@ XGB_DLL SEXP XGBoosterGetNumFeature_R(SEXP handle) {
XGB_DLL SEXP XGBoosterSetParam_R(SEXP handle, SEXP name, SEXP val) { XGB_DLL SEXP XGBoosterSetParam_R(SEXP handle, SEXP name, SEXP val) {
R_API_BEGIN(); R_API_BEGIN();
SEXP name_ = PROTECT(Rf_asChar(name)); SEXP name_ = Rf_protect(Rf_asChar(name));
SEXP val_ = PROTECT(Rf_asChar(val)); SEXP val_ = Rf_protect(Rf_asChar(val));
CHECK_CALL(XGBoosterSetParam(R_ExternalPtrAddr(handle), CHECK_CALL(XGBoosterSetParam(R_ExternalPtrAddr(handle),
CHAR(name_), CHAR(name_),
CHAR(val_))); CHAR(val_)));
UNPROTECT(2); Rf_unprotect(2);
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
@ -1193,8 +1193,8 @@ XGB_DLL SEXP XGBoosterUpdateOneIter_R(SEXP handle, SEXP iter, SEXP dtrain) {
XGB_DLL SEXP XGBoosterTrainOneIter_R(SEXP handle, SEXP dtrain, SEXP iter, SEXP grad, SEXP hess) { XGB_DLL SEXP XGBoosterTrainOneIter_R(SEXP handle, SEXP dtrain, SEXP iter, SEXP grad, SEXP hess) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_EQ(Rf_xlength(grad), Rf_xlength(hess)) << "gradient and hess must have same length."; CHECK_EQ(Rf_xlength(grad), Rf_xlength(hess)) << "gradient and hess must have same length.";
SEXP gdim = getAttrib(grad, R_DimSymbol); SEXP gdim = Rf_getAttrib(grad, R_DimSymbol);
SEXP hdim = getAttrib(hess, R_DimSymbol); SEXP hdim = Rf_getAttrib(hess, R_DimSymbol);
int res_code; int res_code;
{ {
@ -1203,7 +1203,7 @@ XGB_DLL SEXP XGBoosterTrainOneIter_R(SEXP handle, SEXP dtrain, SEXP iter, SEXP g
const std::string s_hess = Rf_isNull(hdim)? const std::string s_hess = Rf_isNull(hdim)?
MakeArrayInterfaceFromRVector(hess) : MakeArrayInterfaceFromRMat(hess); MakeArrayInterfaceFromRVector(hess) : MakeArrayInterfaceFromRMat(hess);
res_code = XGBoosterTrainOneIter(R_ExternalPtrAddr(handle), R_ExternalPtrAddr(dtrain), res_code = XGBoosterTrainOneIter(R_ExternalPtrAddr(handle), R_ExternalPtrAddr(dtrain),
asInteger(iter), s_grad.c_str(), s_hess.c_str()); Rf_asInteger(iter), s_grad.c_str(), s_hess.c_str());
} }
CHECK_CALL(res_code); CHECK_CALL(res_code);
@ -1217,7 +1217,7 @@ XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evn
CHECK_EQ(Rf_xlength(dmats), Rf_xlength(evnames)) CHECK_EQ(Rf_xlength(dmats), Rf_xlength(evnames))
<< "dmats and evnams must have same length"; << "dmats and evnams must have same length";
R_xlen_t len = Rf_xlength(dmats); R_xlen_t len = Rf_xlength(dmats);
SEXP evnames_lst = PROTECT(Rf_allocVector(VECSXP, len)); SEXP evnames_lst = Rf_protect(Rf_allocVector(VECSXP, len));
for (R_xlen_t i = 0; i < len; i++) { for (R_xlen_t i = 0; i < len; i++) {
SET_VECTOR_ELT(evnames_lst, i, Rf_asChar(VECTOR_ELT(evnames, i))); SET_VECTOR_ELT(evnames_lst, i, Rf_asChar(VECTOR_ELT(evnames, i)));
} }
@ -1236,15 +1236,15 @@ XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evn
vec_sptr[i] = vec_names[i].c_str(); vec_sptr[i] = vec_names[i].c_str();
} }
res_code = XGBoosterEvalOneIter(R_ExternalPtrAddr(handle), res_code = XGBoosterEvalOneIter(R_ExternalPtrAddr(handle),
asInteger(iter), Rf_asInteger(iter),
BeginPtr(vec_dmats), BeginPtr(vec_dmats),
BeginPtr(vec_sptr), BeginPtr(vec_sptr),
len, &ret); len, &ret);
} }
CHECK_CALL(res_code); CHECK_CALL(res_code);
UNPROTECT(1); Rf_unprotect(1);
R_API_END(); R_API_END();
return mkString(ret); return Rf_mkString(ret);
} }
namespace { namespace {
@ -1303,8 +1303,8 @@ SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config,
SEXP base_margin) { SEXP base_margin) {
SEXP r_out_shape; SEXP r_out_shape;
SEXP r_out_result; SEXP r_out_result;
SEXP r_out = PROTECT(allocVector(VECSXP, 2)); SEXP r_out = Rf_protect(Rf_allocVector(VECSXP, 2));
SEXP json_config_ = PROTECT(Rf_asChar(json_config)); SEXP json_config_ = Rf_protect(Rf_asChar(json_config));
R_API_BEGIN(); R_API_BEGIN();
char const *c_json_config = CHAR(json_config_); char const *c_json_config = CHAR(json_config_);
@ -1386,21 +1386,21 @@ SEXP XGBoosterPredictGeneric(SEXP handle, SEXP input_data, SEXP json_config,
} }
CHECK_CALL(res_code); CHECK_CALL(res_code);
r_out_shape = PROTECT(allocVector(INTSXP, out_dim)); r_out_shape = Rf_protect(Rf_allocVector(INTSXP, out_dim));
size_t len = 1; size_t len = 1;
int *r_out_shape_ = INTEGER(r_out_shape); int *r_out_shape_ = INTEGER(r_out_shape);
for (size_t i = 0; i < out_dim; ++i) { for (size_t i = 0; i < out_dim; ++i) {
r_out_shape_[i] = out_shape[i]; r_out_shape_[i] = out_shape[i];
len *= out_shape[i]; len *= out_shape[i];
} }
r_out_result = PROTECT(allocVector(REALSXP, len)); r_out_result = Rf_protect(Rf_allocVector(REALSXP, len));
std::copy(out_result, out_result + len, REAL(r_out_result)); std::copy(out_result, out_result + len, REAL(r_out_result));
SET_VECTOR_ELT(r_out, 0, r_out_shape); SET_VECTOR_ELT(r_out, 0, r_out_shape);
SET_VECTOR_ELT(r_out, 1, r_out_result); SET_VECTOR_ELT(r_out, 1, r_out_result);
R_API_END(); R_API_END();
UNPROTECT(4); Rf_unprotect(4);
return r_out; return r_out;
} }
@ -1432,14 +1432,14 @@ XGB_DLL SEXP XGBoosterPredictFromColumnar_R(SEXP handle, SEXP R_df, SEXP missing
XGB_DLL SEXP XGBoosterLoadModel_R(SEXP handle, SEXP fname) { XGB_DLL SEXP XGBoosterLoadModel_R(SEXP handle, SEXP fname) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGBoosterLoadModel(R_ExternalPtrAddr(handle), CHAR(asChar(fname)))); CHECK_CALL(XGBoosterLoadModel(R_ExternalPtrAddr(handle), CHAR(Rf_asChar(fname))));
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
XGB_DLL SEXP XGBoosterSaveModel_R(SEXP handle, SEXP fname) { XGB_DLL SEXP XGBoosterSaveModel_R(SEXP handle, SEXP fname) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGBoosterSaveModel(R_ExternalPtrAddr(handle), CHAR(asChar(fname)))); CHECK_CALL(XGBoosterSaveModel(R_ExternalPtrAddr(handle), CHAR(Rf_asChar(fname))));
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
@ -1457,15 +1457,15 @@ XGB_DLL SEXP XGBoosterSaveModelToRaw_R(SEXP handle, SEXP json_config) {
SEXP ret; SEXP ret;
R_API_BEGIN(); R_API_BEGIN();
bst_ulong olen; bst_ulong olen;
char const *c_json_config = CHAR(asChar(json_config)); char const *c_json_config = CHAR(Rf_asChar(json_config));
char const *raw; char const *raw;
CHECK_CALL(XGBoosterSaveModelToBuffer(R_ExternalPtrAddr(handle), c_json_config, &olen, &raw)) CHECK_CALL(XGBoosterSaveModelToBuffer(R_ExternalPtrAddr(handle), c_json_config, &olen, &raw))
ret = PROTECT(allocVector(RAWSXP, olen)); ret = Rf_protect(Rf_allocVector(RAWSXP, olen));
if (olen != 0) { if (olen != 0) {
std::memcpy(RAW(ret), raw, olen); std::memcpy(RAW(ret), raw, olen);
} }
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
@ -1477,12 +1477,12 @@ XGB_DLL SEXP XGBoosterSaveJsonConfig_R(SEXP handle) {
&len, &len,
&ret)); &ret));
R_API_END(); R_API_END();
return mkString(ret); return Rf_mkString(ret);
} }
XGB_DLL SEXP XGBoosterLoadJsonConfig_R(SEXP handle, SEXP value) { XGB_DLL SEXP XGBoosterLoadJsonConfig_R(SEXP handle, SEXP value) {
R_API_BEGIN(); R_API_BEGIN();
CHECK_CALL(XGBoosterLoadJsonConfig(R_ExternalPtrAddr(handle), CHAR(asChar(value)))); CHECK_CALL(XGBoosterLoadJsonConfig(R_ExternalPtrAddr(handle), CHAR(Rf_asChar(value))));
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
@ -1493,12 +1493,12 @@ XGB_DLL SEXP XGBoosterSerializeToBuffer_R(SEXP handle) {
bst_ulong out_len; bst_ulong out_len;
const char *raw; const char *raw;
CHECK_CALL(XGBoosterSerializeToBuffer(R_ExternalPtrAddr(handle), &out_len, &raw)); CHECK_CALL(XGBoosterSerializeToBuffer(R_ExternalPtrAddr(handle), &out_len, &raw));
ret = PROTECT(allocVector(RAWSXP, out_len)); ret = Rf_protect(Rf_allocVector(RAWSXP, out_len));
if (out_len != 0) { if (out_len != 0) {
memcpy(RAW(ret), raw, out_len); memcpy(RAW(ret), raw, out_len);
} }
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return ret; return ret;
} }
@ -1513,19 +1513,19 @@ XGB_DLL SEXP XGBoosterUnserializeFromBuffer_R(SEXP handle, SEXP raw) {
XGB_DLL SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats, SEXP dump_format) { XGB_DLL SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats, SEXP dump_format) {
SEXP out; SEXP out;
SEXP continuation_token = PROTECT(R_MakeUnwindCont()); SEXP continuation_token = Rf_protect(R_MakeUnwindCont());
SEXP dump_format_ = PROTECT(Rf_asChar(dump_format)); SEXP dump_format_ = Rf_protect(Rf_asChar(dump_format));
SEXP fmap_ = PROTECT(Rf_asChar(fmap)); SEXP fmap_ = Rf_protect(Rf_asChar(fmap));
R_API_BEGIN(); R_API_BEGIN();
bst_ulong olen; bst_ulong olen;
const char **res; const char **res;
const char *fmt = CHAR(dump_format_); const char *fmt = CHAR(dump_format_);
CHECK_CALL(XGBoosterDumpModelEx(R_ExternalPtrAddr(handle), CHECK_CALL(XGBoosterDumpModelEx(R_ExternalPtrAddr(handle),
CHAR(fmap_), CHAR(fmap_),
asInteger(with_stats), Rf_asInteger(with_stats),
fmt, fmt,
&olen, &res)); &olen, &res));
out = PROTECT(allocVector(STRSXP, olen)); out = Rf_protect(Rf_allocVector(STRSXP, olen));
try { try {
if (!strcmp("json", fmt)) { if (!strcmp("json", fmt)) {
std::stringstream stream; std::stringstream stream;
@ -1553,7 +1553,7 @@ XGB_DLL SEXP XGBoosterDumpModel_R(SEXP handle, SEXP fmap, SEXP with_stats, SEXP
R_ContinueUnwind(continuation_token); R_ContinueUnwind(continuation_token);
} }
R_API_END(); R_API_END();
UNPROTECT(4); Rf_unprotect(4);
return out; return out;
} }
@ -1563,35 +1563,35 @@ XGB_DLL SEXP XGBoosterGetAttr_R(SEXP handle, SEXP name) {
int success; int success;
const char *val; const char *val;
CHECK_CALL(XGBoosterGetAttr(R_ExternalPtrAddr(handle), CHECK_CALL(XGBoosterGetAttr(R_ExternalPtrAddr(handle),
CHAR(asChar(name)), CHAR(Rf_asChar(name)),
&val, &val,
&success)); &success));
if (success) { if (success) {
out = PROTECT(allocVector(STRSXP, 1)); out = Rf_protect(Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(out, 0, mkChar(val)); SET_STRING_ELT(out, 0, Rf_mkChar(val));
} else { } else {
out = PROTECT(R_NilValue); out = Rf_protect(R_NilValue);
} }
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return out; return out;
} }
XGB_DLL SEXP XGBoosterSetAttr_R(SEXP handle, SEXP name, SEXP val) { XGB_DLL SEXP XGBoosterSetAttr_R(SEXP handle, SEXP name, SEXP val) {
R_API_BEGIN(); R_API_BEGIN();
const char *v = nullptr; const char *v = nullptr;
SEXP name_ = PROTECT(Rf_asChar(name)); SEXP name_ = Rf_protect(Rf_asChar(name));
SEXP val_; SEXP val_;
int n_protected = 1; int n_protected = 1;
if (!Rf_isNull(val)) { if (!Rf_isNull(val)) {
val_ = PROTECT(Rf_asChar(val)); val_ = Rf_protect(Rf_asChar(val));
n_protected++; n_protected++;
v = CHAR(val_); v = CHAR(val_);
} }
CHECK_CALL(XGBoosterSetAttr(R_ExternalPtrAddr(handle), CHECK_CALL(XGBoosterSetAttr(R_ExternalPtrAddr(handle),
CHAR(name_), v)); CHAR(name_), v));
UNPROTECT(n_protected); Rf_unprotect(n_protected);
R_API_END(); R_API_END();
return R_NilValue; return R_NilValue;
} }
@ -1604,15 +1604,15 @@ XGB_DLL SEXP XGBoosterGetAttrNames_R(SEXP handle) {
CHECK_CALL(XGBoosterGetAttrNames(R_ExternalPtrAddr(handle), CHECK_CALL(XGBoosterGetAttrNames(R_ExternalPtrAddr(handle),
&len, &res)); &len, &res));
if (len > 0) { if (len > 0) {
out = PROTECT(allocVector(STRSXP, len)); out = Rf_protect(Rf_allocVector(STRSXP, len));
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
SET_STRING_ELT(out, i, mkChar(res[i])); SET_STRING_ELT(out, i, Rf_mkChar(res[i]));
} }
} else { } else {
out = PROTECT(R_NilValue); out = Rf_protect(R_NilValue);
} }
R_API_END(); R_API_END();
UNPROTECT(1); Rf_unprotect(1);
return out; return out;
} }
@ -1620,10 +1620,10 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config) {
SEXP out_features_sexp; SEXP out_features_sexp;
SEXP out_scores_sexp; SEXP out_scores_sexp;
SEXP out_shape_sexp; SEXP out_shape_sexp;
SEXP r_out = PROTECT(allocVector(VECSXP, 3)); SEXP r_out = Rf_protect(Rf_allocVector(VECSXP, 3));
R_API_BEGIN(); R_API_BEGIN();
char const *c_json_config = CHAR(asChar(json_config)); char const *c_json_config = CHAR(Rf_asChar(json_config));
bst_ulong out_n_features; bst_ulong out_n_features;
char const **out_features; char const **out_features;
@ -1634,7 +1634,7 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config) {
CHECK_CALL(XGBoosterFeatureScore(R_ExternalPtrAddr(handle), c_json_config, CHECK_CALL(XGBoosterFeatureScore(R_ExternalPtrAddr(handle), c_json_config,
&out_n_features, &out_features, &out_n_features, &out_features,
&out_dim, &out_shape, &out_scores)); &out_dim, &out_shape, &out_scores));
out_shape_sexp = PROTECT(allocVector(INTSXP, out_dim)); out_shape_sexp = Rf_protect(Rf_allocVector(INTSXP, out_dim));
size_t len = 1; size_t len = 1;
int *out_shape_sexp_ = INTEGER(out_shape_sexp); int *out_shape_sexp_ = INTEGER(out_shape_sexp);
for (size_t i = 0; i < out_dim; ++i) { for (size_t i = 0; i < out_dim; ++i) {
@ -1642,12 +1642,12 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config) {
len *= out_shape[i]; len *= out_shape[i];
} }
out_features_sexp = PROTECT(allocVector(STRSXP, out_n_features)); out_features_sexp = Rf_protect(Rf_allocVector(STRSXP, out_n_features));
for (size_t i = 0; i < out_n_features; ++i) { for (size_t i = 0; i < out_n_features; ++i) {
SET_STRING_ELT(out_features_sexp, i, mkChar(out_features[i])); SET_STRING_ELT(out_features_sexp, i, Rf_mkChar(out_features[i]));
} }
out_scores_sexp = PROTECT(allocVector(REALSXP, len)); out_scores_sexp = Rf_protect(Rf_allocVector(REALSXP, len));
std::copy(out_scores, out_scores + len, REAL(out_scores_sexp)); std::copy(out_scores, out_scores + len, REAL(out_scores_sexp));
SET_VECTOR_ELT(r_out, 0, out_features_sexp); SET_VECTOR_ELT(r_out, 0, out_features_sexp);
@ -1655,7 +1655,7 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config) {
SET_VECTOR_ELT(r_out, 2, out_scores_sexp); SET_VECTOR_ELT(r_out, 2, out_scores_sexp);
R_API_END(); R_API_END();
UNPROTECT(4); Rf_unprotect(4);
return r_out; return r_out;
} }

View File

@ -8,6 +8,9 @@
#define XGBOOST_R_H_ // NOLINT(*) #define XGBOOST_R_H_ // NOLINT(*)
#ifndef R_NO_REMAP
# define R_NO_REMAP
#endif
#include <R.h> #include <R.h>
#include <Rinternals.h> #include <Rinternals.h>
#include <R_ext/Altrep.h> #include <R_ext/Altrep.h>