-
Notifications
You must be signed in to change notification settings - Fork 26
/
java_utils.R
36 lines (31 loc) · 1.21 KB
/
java_utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#' Java object to data.table
#'
#' @description Converts a Java object returned by r5r_core to an R data.table
#'
#' @param obj A Java Object reference
#'
#' @return An R data.table
#' @family java support functions
#'
#' @keywords internal
java_to_dt <- function(obj) {
# check input
if(class(obj)[1] != "jobjRef"){
stop("Input must be an object of class 'jobjRef'")}
# get column names from Java table
columns <- obj$getColumnNames()
# get the contents of each column in a vector, and return them in a list
dt <- lapply(columns, function(column_name) {
# check column data type, so we can call the appropriate Java function
column_type <- obj$getColumnType(column_name)
if (column_type == "String") { v <- obj$getStringColumn(column_name) }
if (column_type == "Integer") { v <- obj$getIntegerColumn(column_name) }
if (column_type == "Long") { v <- obj$getLongColumn(column_name) }
if (column_type == "Double") { v <- obj$getDoubleColumn(column_name) }
if (column_type == "Boolean") { v <- obj$getBooleanColumn(column_name) }
return(v)
})
# convert list of vectors to a data.table, and rename columns accordingly
data.table::setDT(dt)
data.table::setnames(dt, new = columns)
}