MCPcopy Create free account
hub / github.com/dmlc/xgboost / MakeArrayInterfaceFromRDataFrame

Function MakeArrayInterfaceFromRDataFrame

R-package/src/xgboost_R.cc:174–213  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

172}
173
174[[nodiscard]] std::string MakeArrayInterfaceFromRDataFrame(SEXP R_df) {
175 auto make_vec = [&](auto const *ptr, std::size_t len) {
176 auto v = xgboost::linalg::MakeVec(ptr, len);
177 return xgboost::linalg::ArrayInterface(v);
178 };
179
180 R_xlen_t n_features = Rf_xlength(R_df);
181 std::vector<xgboost::Json> array(n_features);
182 CHECK_GT(n_features, 0);
183 std::size_t len = Rf_xlength(VECTOR_ELT(R_df, 0));
184
185 // The `data.frame` in R actually converts all data into numeric. The other type
186 // handlers here are not used. At the moment they are kept as a reference for when we
187 // can avoid making data copies during transformation.
188 for (R_xlen_t i = 0; i < n_features; ++i) {
189 switch (TYPEOF(VECTOR_ELT(R_df, i))) {
190 case INTSXP: {
191 auto const *ptr = INTEGER(VECTOR_ELT(R_df, i));
192 array[i] = make_vec(ptr, len);
193 break;
194 }
195 case REALSXP: {
196 auto const *ptr = REAL(VECTOR_ELT(R_df, i));
197 array[i] = make_vec(ptr, len);
198 break;
199 }
200 case LGLSXP: {
201 auto const *ptr = LOGICAL(VECTOR_ELT(R_df, i));
202 array[i] = make_vec(ptr, len);
203 break;
204 }
205 default: {
206 LOG(FATAL) << "data.frame has unsupported type.";
207 }
208 }
209 }
210
211 xgboost::Json jinterface{std::move(array)};
212 return xgboost::Json::Dump(jinterface);
213}
214
215void AddMissingToJson(xgboost::Json *jconfig, SEXP missing, SEXPTYPE arr_type) {
216 if (Rf_isNull(missing) || ISNAN(Rf_asReal(missing))) {

Callers 3

XGDMatrixCreateFromDF_RFunction · 0.85
XGBoosterPredictGenericFunction · 0.85

Calls 2

MakeVecFunction · 0.85
ArrayInterfaceFunction · 0.50

Tested by

no test coverage detected