Skip to content

Commit

Permalink
[R-package] Convert LGBM_GetLastError_R to use R built-in types (#4242)
Browse files Browse the repository at this point in the history
* [R-package] move some functions over to SEXP objects

* clarify comment

* remove paste0()

* fix registration table

* fix cmake builds
  • Loading branch information
jameslamb authored May 2, 2021
1 parent 1a367c6 commit 66ee291
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 35 deletions.
29 changes: 3 additions & 26 deletions R-package/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,15 @@ lgb.encode.char <- function(arr, len) {
return(rawToChar(arr[seq_len(len)]))
}

# [description] Raise an error. Before raising that error, check for any error message
# stored in a buffer on the C++ side.
# [description] Get the most recent error stored on the C++ side and raise it
# as an R error.
lgb.last_error <- function() {
# Perform text error buffering
buf_len <- 200L
act_len <- 0L
err_msg <- raw(buf_len)
err_msg <- .Call(
LGBM_GetLastError_R
, buf_len
, act_len
, err_msg
)

# Check error buffer
if (act_len > buf_len) {
buf_len <- act_len
err_msg <- raw(buf_len)
err_msg <- .Call(
LGBM_GetLastError_R
, buf_len
, act_len
, err_msg
)
}

stop("api error: ", lgb.encode.char(arr = err_msg, len = act_len))

stop("api error: ", err_msg)
return(invisible(NULL))

}

lgb.params2str <- function(params, ...) {

# Check for a list as input
Expand Down
10 changes: 7 additions & 3 deletions R-package/src/lightgbm_R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ LGBM_SE EncodeChar(LGBM_SE dest, const char* src, LGBM_SE buf_len, LGBM_SE actua
return dest;
}

LGBM_SE LGBM_GetLastError_R(LGBM_SE buf_len, LGBM_SE actual_len, LGBM_SE err_msg) {
return EncodeChar(err_msg, LGBM_GetLastError(), buf_len, actual_len, std::strlen(LGBM_GetLastError()) + 1);
SEXP LGBM_GetLastError_R() {
SEXP out;
out = PROTECT(Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(out, 0, Rf_mkChar(LGBM_GetLastError()));
UNPROTECT(1);
return out;
}

SEXP LGBM_DatasetCreateFromFile_R(LGBM_SE filename,
Expand Down Expand Up @@ -640,7 +644,7 @@ SEXP LGBM_BoosterDumpModel_R(LGBM_SE handle,

// .Call() calls
static const R_CallMethodDef CallEntries[] = {
{"LGBM_GetLastError_R" , (DL_FUNC) &LGBM_GetLastError_R , 3},
{"LGBM_GetLastError_R" , (DL_FUNC) &LGBM_GetLastError_R , 0},
{"LGBM_DatasetCreateFromFile_R" , (DL_FUNC) &LGBM_DatasetCreateFromFile_R , 4},
{"LGBM_DatasetCreateFromCSC_R" , (DL_FUNC) &LGBM_DatasetCreateFromCSC_R , 9},
{"LGBM_DatasetCreateFromMat_R" , (DL_FUNC) &LGBM_DatasetCreateFromMat_R , 6},
Expand Down
6 changes: 1 addition & 5 deletions R-package/src/lightgbm_R.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
* \return err_msg error information
* \return error information
*/
LIGHTGBM_C_EXPORT LGBM_SE LGBM_GetLastError_R(
LGBM_SE buf_len,
LGBM_SE actual_len,
LGBM_SE err_msg
);
LIGHTGBM_C_EXPORT SEXP LGBM_GetLastError_R();

// --- start Dataset interface

Expand Down
2 changes: 1 addition & 1 deletion build_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ c_api_contents <- gsub(
, x = c_api_contents
)
c_api_symbols <- gsub(
pattern = "\\("
pattern = "\\(.*"
, replacement = ""
, x = c_api_contents
)
Expand Down

0 comments on commit 66ee291

Please sign in to comment.