[R] Refactor callback structure and attributes (#9957)

This commit is contained in:
david-cortes
2024-03-01 08:57:47 +01:00
committed by GitHub
parent 3941b31ade
commit 2c12b956da
32 changed files with 2076 additions and 1339 deletions

View File

@@ -76,6 +76,7 @@ extern SEXP XGBSetGlobalConfig_R(SEXP);
extern SEXP XGBGetGlobalConfig_R(void);
extern SEXP XGBoosterFeatureScore_R(SEXP, SEXP);
extern SEXP XGBoosterSlice_R(SEXP, SEXP, SEXP, SEXP);
extern SEXP XGBoosterSliceAndReplace_R(SEXP, SEXP, SEXP, SEXP);
static const R_CallMethodDef CallEntries[] = {
{"XGDuplicate_R", (DL_FUNC) &XGDuplicate_R, 1},
@@ -138,6 +139,7 @@ static const R_CallMethodDef CallEntries[] = {
{"XGBGetGlobalConfig_R", (DL_FUNC) &XGBGetGlobalConfig_R, 0},
{"XGBoosterFeatureScore_R", (DL_FUNC) &XGBoosterFeatureScore_R, 2},
{"XGBoosterSlice_R", (DL_FUNC) &XGBoosterSlice_R, 4},
{"XGBoosterSliceAndReplace_R", (DL_FUNC) &XGBoosterSliceAndReplace_R, 4},
{NULL, NULL, 0}
};

View File

@@ -1674,3 +1674,18 @@ XGB_DLL SEXP XGBoosterSlice_R(SEXP handle, SEXP begin_layer, SEXP end_layer, SEX
Rf_unprotect(1);
return out;
}
XGB_DLL SEXP XGBoosterSliceAndReplace_R(SEXP handle, SEXP begin_layer, SEXP end_layer, SEXP step) {
R_API_BEGIN();
BoosterHandle old_handle = R_ExternalPtrAddr(handle);
BoosterHandle new_handle = nullptr;
CHECK_CALL(XGBoosterSlice(old_handle,
Rf_asInteger(begin_layer),
Rf_asInteger(end_layer),
Rf_asInteger(step),
&new_handle));
R_SetExternalPtrAddr(handle, new_handle);
CHECK_CALL(XGBoosterFree(old_handle));
R_API_END();
return R_NilValue;
}

View File

@@ -535,4 +535,14 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config);
*/
XGB_DLL SEXP XGBoosterSlice_R(SEXP handle, SEXP begin_layer, SEXP end_layer, SEXP step);
/*!
* \brief Slice a fitted booster model (by rounds), and replace its handle with the result
* \param handle handle to the fitted booster
* \param begin_layer start of the slice
* \param end_later end of the slice; end_layer=0 is equivalent to end_layer=num_boost_round
* \param step step size of the slice
* \return NULL
*/
XGB_DLL SEXP XGBoosterSliceAndReplace_R(SEXP handle, SEXP begin_layer, SEXP end_layer, SEXP step);
#endif // XGBOOST_WRAPPER_R_H_ // NOLINT(*)