Unverified Commit 66ee2919 authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[R-package] Convert LGBM_GetLastError_R to use R built-in types (#4242)

* [R-package] move some functions over to SEXP objects

* clarify comment

* remove paste0()

* fix registration table

* fix cmake builds
parent 1a367c65
...@@ -25,38 +25,15 @@ lgb.encode.char <- function(arr, len) { ...@@ -25,38 +25,15 @@ lgb.encode.char <- function(arr, len) {
return(rawToChar(arr[seq_len(len)])) return(rawToChar(arr[seq_len(len)]))
} }
# [description] Raise an error. Before raising that error, check for any error message # [description] Get the most recent error stored on the C++ side and raise it
# stored in a buffer on the C++ side. # as an R error.
lgb.last_error <- function() { lgb.last_error <- function() {
# Perform text error buffering
buf_len <- 200L
act_len <- 0L
err_msg <- raw(buf_len)
err_msg <- .Call( err_msg <- .Call(
LGBM_GetLastError_R LGBM_GetLastError_R
, buf_len
, act_len
, err_msg
) )
stop("api error: ", 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))
return(invisible(NULL)) return(invisible(NULL))
} }
lgb.params2str <- function(params, ...) { lgb.params2str <- function(params, ...) {
# Check for a list as input # Check for a list as input
......
...@@ -56,8 +56,12 @@ LGBM_SE EncodeChar(LGBM_SE dest, const char* src, LGBM_SE buf_len, LGBM_SE actua ...@@ -56,8 +56,12 @@ LGBM_SE EncodeChar(LGBM_SE dest, const char* src, LGBM_SE buf_len, LGBM_SE actua
return dest; return dest;
} }
LGBM_SE LGBM_GetLastError_R(LGBM_SE buf_len, LGBM_SE actual_len, LGBM_SE err_msg) { SEXP LGBM_GetLastError_R() {
return EncodeChar(err_msg, LGBM_GetLastError(), buf_len, actual_len, std::strlen(LGBM_GetLastError()) + 1); 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, SEXP LGBM_DatasetCreateFromFile_R(LGBM_SE filename,
...@@ -640,7 +644,7 @@ SEXP LGBM_BoosterDumpModel_R(LGBM_SE handle, ...@@ -640,7 +644,7 @@ SEXP LGBM_BoosterDumpModel_R(LGBM_SE handle,
// .Call() calls // .Call() calls
static const R_CallMethodDef CallEntries[] = { 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_DatasetCreateFromFile_R" , (DL_FUNC) &LGBM_DatasetCreateFromFile_R , 4},
{"LGBM_DatasetCreateFromCSC_R" , (DL_FUNC) &LGBM_DatasetCreateFromCSC_R , 9}, {"LGBM_DatasetCreateFromCSC_R" , (DL_FUNC) &LGBM_DatasetCreateFromCSC_R , 9},
{"LGBM_DatasetCreateFromMat_R" , (DL_FUNC) &LGBM_DatasetCreateFromMat_R , 6}, {"LGBM_DatasetCreateFromMat_R" , (DL_FUNC) &LGBM_DatasetCreateFromMat_R , 6},
......
...@@ -15,11 +15,7 @@ ...@@ -15,11 +15,7 @@
* \return err_msg error information * \return err_msg error information
* \return error information * \return error information
*/ */
LIGHTGBM_C_EXPORT LGBM_SE LGBM_GetLastError_R( LIGHTGBM_C_EXPORT SEXP LGBM_GetLastError_R();
LGBM_SE buf_len,
LGBM_SE actual_len,
LGBM_SE err_msg
);
// --- start Dataset interface // --- start Dataset interface
......
...@@ -397,7 +397,7 @@ c_api_contents <- gsub( ...@@ -397,7 +397,7 @@ c_api_contents <- gsub(
, x = c_api_contents , x = c_api_contents
) )
c_api_symbols <- gsub( c_api_symbols <- gsub(
pattern = "\\(" pattern = "\\(.*"
, replacement = "" , replacement = ""
, x = c_api_contents , x = c_api_contents
) )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment