Skip to content

Commit

Permalink
R api 3.0.0.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
wenwenluo committed May 21, 2024
1 parent f47f184 commit 522f359
Show file tree
Hide file tree
Showing 29 changed files with 2,629 additions and 921 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: RDolphinDB
Type: Package
Title: Connecting to DolphinDB server with R
Version: 2.0.11.1
Date: 2024-03-19
Version: 3.0.0.1
Date: 2024-05-17
Author: mrDrivingDuck
Maintainer: mrDrivingDuck <[email protected]>
Description: The R API of DolphinDB.
Expand Down
1 change: 1 addition & 0 deletions R/RDolphinDB.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ setClass("RDolphinDB", slots = list(connected = "logical"))
#'
#' @description Method of getting connection with DolphinDB server.
#' @description (If the input contains 'username' and 'password', a log-in job will be done)
#' @description Currently, there is at most one TCP connection between the R API and dolphindb. If you call dbConnect multiple times, it closes the previously created connection and creates a new one
#' @param conn The connector object.
#' @param host The host running the DolphinDB server.
#' @param port The port running the DolphinDB server.
Expand Down
37 changes: 35 additions & 2 deletions R/RDolphinDB_impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ DDB_GetEntity <- function(xxdb_type) {

} else if (xxdb_type == 12) {
# Character Matrix
return (NULL)
result <- ReturnMatrixString()
result <- DDB_SetReceiveVectorNA(result, ReturnMatrixNAIndex())
result <- DDB_ReceiveMatrixLable(result)
Clear()
return (result)

} else if (xxdb_type == 13) {
# DataFrame
Expand Down Expand Up @@ -276,6 +280,10 @@ DDB_GetEntity <- function(xxdb_type) {
clm[NAIndex] <- NA
result <- cbind(result, clm)

} else if (typelist[i] == 21) {
# factor
clm <- ReturnTableColumnFactor(i)
result <- cbind(result, clm)
} else {
print("error in DataFrame")
Clear()
Expand Down Expand Up @@ -387,6 +395,10 @@ DDB_GetEntity <- function(xxdb_type) {
result <- DDB_SetReceiveVectorNA(result, ReturnVectorNAIndex(i))
result <- result

} else if (anytypelist[i] == 21) {
# Factor
result <- ReturnVectorFactor(i)

} else if (anytypelist[i] == 9) {
# Logical Matrix
result <- ReturnMatrixBool(i)
Expand Down Expand Up @@ -462,6 +474,10 @@ DDB_GetEntity <- function(xxdb_type) {
clm[NAIndex] <- NA
result <- cbind(result, clm)

} else if (typelist[k] == 21) {
# factor
clm <- ReturnTableColumnFactor(k, i)
result <- cbind(result, clm)
} else {
print("error in DataFrame")
Clear()
Expand All @@ -470,6 +486,10 @@ DDB_GetEntity <- function(xxdb_type) {
}

result <- result[,-1]
if(class(result)[1] != "data.frame"){
# if the result only contains one column, then must convert it to dataFrame Explicitly, or it will be a vecor
result = data.frame(result)
}
colnames(result) <- ReturnTableColumeName(i)

} else {
Expand All @@ -481,6 +501,12 @@ DDB_GetEntity <- function(xxdb_type) {
Clear()
return (anyVector)

} else if (xxdb_type == 21) {
# Factor
result <- ReturnVectorFactor()
Clear()
return (result)

} else {
# print("Error")
Clear()
Expand Down Expand Up @@ -572,11 +598,15 @@ DDB_UploadVector <- function(vec) {
NAIndex <- DDB_SetUploadVectorNA(vec)
UploadVectorDouble(vec, NAIndex)

} else if (is.character(vec) || is.factor(vec)) {
} else if (is.character(vec)) {

NAIndex <- DDB_SetUploadVectorNA(vec)
UploadVectorString(vec, NAIndex)

} else if(is.factor(vec)){

UploadVectorSymbol(vec)

} else if (length(class(vec)) > 1 &&
class(vec)[1] == "POSIXct") {

Expand Down Expand Up @@ -691,6 +721,7 @@ DDB_UploadObjectCheck <- function(args) {

} else if (class(args[[i]])[1] == "POSIXct" && length(args[[i]]) > 1) {

} else if (is.factor(args[[i]])){
} else {
print("Data form not support yet.")
return (FALSE)
Expand All @@ -713,6 +744,8 @@ DDB_UploadEntity <- function(args) {
DDB_UploadMatrix(args[[i]])
} else if (is.data.frame(args[[i]])) {
DDB_UploadTable(args[[i]])
} else if (is.factor(args[[i]])){
UploadVectorSymbol(args[[i]])
} else if (is.vector(args[[i]]) && length(args[[i]]) > 1) {
DDB_UploadVector(args[[i]])
} else if (is.vector(args[[i]]) && length(args[[i]]) == 1) {
Expand Down
12 changes: 12 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ UploadMatrixDouble <- function(R_mtx, R_NAIndex) {
invisible(.Call('_RDolphinDB_UploadMatrixDouble', PACKAGE = 'RDolphinDB', R_mtx, R_NAIndex))
}

UploadVectorSymbol <- function(R_vec) {
invisible(.Call('_RDolphinDB_UploadVectorSymbol', PACKAGE = 'RDolphinDB', R_vec))
}

UploadVectorString <- function(R_vec, R_NAIndex) {
invisible(.Call('_RDolphinDB_UploadVectorString', PACKAGE = 'RDolphinDB', R_vec, R_NAIndex))
}
Expand Down Expand Up @@ -153,6 +157,10 @@ ReturnVectorBool <- function(index = -1L) {
.Call('_RDolphinDB_ReturnVectorBool', PACKAGE = 'RDolphinDB', index)
}

ReturnVectorFactor <- function(index = -1L) {
.Call('_RDolphinDB_ReturnVectorFactor', PACKAGE = 'RDolphinDB', index)
}

ReturnVectorInt <- function(index = -1L) {
.Call('_RDolphinDB_ReturnVectorInt', PACKAGE = 'RDolphinDB', index)
}
Expand Down Expand Up @@ -241,6 +249,10 @@ ReturnTableColumnLogical <- function(index, entity_index = -1L) {
.Call('_RDolphinDB_ReturnTableColumnLogical', PACKAGE = 'RDolphinDB', index, entity_index)
}

ReturnTableColumnFactor <- function(index, entity_index = -1L) {
.Call('_RDolphinDB_ReturnTableColumnFactor', PACKAGE = 'RDolphinDB', index, entity_index)
}

ReturnTableColumnInteger <- function(index, entity_index = -1L) {
.Call('_RDolphinDB_ReturnTableColumnInteger', PACKAGE = 'RDolphinDB', index, entity_index)
}
Expand Down
3 changes: 2 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ help("dbClose")
|NANOTIMESTAMP|2012.06.13T13:30:10.008007006|POSIXct|2012-06-13 13:30:10|
|FLOAT|2.1f|Numeric|2.1|
|DOUBLE|2.1|Numeric|2.1|
|STRING|"123"|Character|"123"|
|STRING|"123"|Character|"123"|
|SYMBOL|"123"|Factor|"123"|
2 changes: 2 additions & 0 deletions man/dbConnect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ BEGIN_RCPP
return R_NilValue;
END_RCPP
}
// UploadVectorSymbol
void UploadVectorSymbol(IntegerVector R_vec);
RcppExport SEXP _RDolphinDB_UploadVectorSymbol(SEXP R_vecSEXP) {
BEGIN_RCPP
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< IntegerVector >::type R_vec(R_vecSEXP);
UploadVectorSymbol(R_vec);
return R_NilValue;
END_RCPP
}
// UploadVectorString
void UploadVectorString(CharacterVector R_vec, IntegerVector R_NAIndex);
RcppExport SEXP _RDolphinDB_UploadVectorString(SEXP R_vecSEXP, SEXP R_NAIndexSEXP) {
Expand Down Expand Up @@ -409,6 +419,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// ReturnVectorFactor
IntegerVector ReturnVectorFactor(int index);
RcppExport SEXP _RDolphinDB_ReturnVectorFactor(SEXP indexSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< int >::type index(indexSEXP);
rcpp_result_gen = Rcpp::wrap(ReturnVectorFactor(index));
return rcpp_result_gen;
END_RCPP
}
// ReturnVectorInt
IntegerVector ReturnVectorInt(int index);
RcppExport SEXP _RDolphinDB_ReturnVectorInt(SEXP indexSEXP) {
Expand Down Expand Up @@ -659,6 +680,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// ReturnTableColumnFactor
IntegerVector ReturnTableColumnFactor(int index, int entity_index);
RcppExport SEXP _RDolphinDB_ReturnTableColumnFactor(SEXP indexSEXP, SEXP entity_indexSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< int >::type index(indexSEXP);
Rcpp::traits::input_parameter< int >::type entity_index(entity_indexSEXP);
rcpp_result_gen = Rcpp::wrap(ReturnTableColumnFactor(index, entity_index));
return rcpp_result_gen;
END_RCPP
}
// ReturnTableColumnInteger
IntegerVector ReturnTableColumnInteger(int index, int entity_index);
RcppExport SEXP _RDolphinDB_ReturnTableColumnInteger(SEXP indexSEXP, SEXP entity_indexSEXP) {
Expand Down Expand Up @@ -750,6 +783,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_RDolphinDB_UploadMatrixBool", (DL_FUNC) &_RDolphinDB_UploadMatrixBool, 2},
{"_RDolphinDB_UploadMatrixInt", (DL_FUNC) &_RDolphinDB_UploadMatrixInt, 2},
{"_RDolphinDB_UploadMatrixDouble", (DL_FUNC) &_RDolphinDB_UploadMatrixDouble, 2},
{"_RDolphinDB_UploadVectorSymbol", (DL_FUNC) &_RDolphinDB_UploadVectorSymbol, 1},
{"_RDolphinDB_UploadVectorString", (DL_FUNC) &_RDolphinDB_UploadVectorString, 2},
{"_RDolphinDB_UploadVectorDouble", (DL_FUNC) &_RDolphinDB_UploadVectorDouble, 2},
{"_RDolphinDB_UploadVectorBool", (DL_FUNC) &_RDolphinDB_UploadVectorBool, 2},
Expand All @@ -771,6 +805,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_RDolphinDB_ReturnScalarTime", (DL_FUNC) &_RDolphinDB_ReturnScalarTime, 1},
{"_RDolphinDB_ReturnScalarDate", (DL_FUNC) &_RDolphinDB_ReturnScalarDate, 1},
{"_RDolphinDB_ReturnVectorBool", (DL_FUNC) &_RDolphinDB_ReturnVectorBool, 1},
{"_RDolphinDB_ReturnVectorFactor", (DL_FUNC) &_RDolphinDB_ReturnVectorFactor, 1},
{"_RDolphinDB_ReturnVectorInt", (DL_FUNC) &_RDolphinDB_ReturnVectorInt, 1},
{"_RDolphinDB_ReturnVectorDouble", (DL_FUNC) &_RDolphinDB_ReturnVectorDouble, 1},
{"_RDolphinDB_ReturnVectorString", (DL_FUNC) &_RDolphinDB_ReturnVectorString, 1},
Expand All @@ -793,6 +828,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_RDolphinDB_ReturnTableColumeName", (DL_FUNC) &_RDolphinDB_ReturnTableColumeName, 1},
{"_RDolphinDB_ReturnEmptyDataFrame", (DL_FUNC) &_RDolphinDB_ReturnEmptyDataFrame, 1},
{"_RDolphinDB_ReturnTableColumnLogical", (DL_FUNC) &_RDolphinDB_ReturnTableColumnLogical, 2},
{"_RDolphinDB_ReturnTableColumnFactor", (DL_FUNC) &_RDolphinDB_ReturnTableColumnFactor, 2},
{"_RDolphinDB_ReturnTableColumnInteger", (DL_FUNC) &_RDolphinDB_ReturnTableColumnInteger, 2},
{"_RDolphinDB_ReturnTableColumnDouble", (DL_FUNC) &_RDolphinDB_ReturnTableColumnDouble, 2},
{"_RDolphinDB_ReturnTableColumnTime", (DL_FUNC) &_RDolphinDB_ReturnTableColumnTime, 2},
Expand Down
56 changes: 56 additions & 0 deletions src/connector_cppapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,43 @@ void UploadMatrixDouble(NumericMatrix R_mtx, IntegerVector R_NAIndex)
cnt.Rcpp_UploadEntity(mtx, NAIndex, R_mtx.nrow(), R_mtx.ncol());
}

//[[Rcpp::export]]
void UploadVectorSymbol(IntegerVector R_vec)
{
std::vector<std::string> symbolBase;
std::vector<int> index;

CharacterVector level = R_vec.attr("levels");
LogicalVector isNaVec = is_na(level);
int naIndex = INT_MAX;

//dolphindb中空值固定在首位,需要找到R中的空值位置并跳过
symbolBase.push_back("");
for(int i = 0; i < level.size(); ++i){
if(isNaVec[i]){
naIndex = i == INT_MAX ? i : i + 1;
}
else{
symbolBase.push_back(std::string(level[i]));
}
}

index.reserve(R_vec.size());
for(int i = 0; i < R_vec.size(); ++i){
if(R_vec[i] == naIndex){
index.push_back(0);
}
else if(R_vec[i] > naIndex){
index.push_back(R_vec[i] - 1);
}
else{
index.push_back(R_vec[i]);
}
}

cnt.Rcpp_UploadSymbolVector(symbolBase, index);
}

//[[Rcpp::export]]
void UploadVectorString(CharacterVector R_vec, IntegerVector R_NAIndex)
{
Expand Down Expand Up @@ -347,6 +384,15 @@ LogicalVector ReturnVectorBool(int index = -1)
);
}

//[[Rcpp::export]]
IntegerVector ReturnVectorFactor(int index = -1)
{
void* tmp = cnt.Rcpp_GetEntity(index)->
getVector()->
getVector();
return wrap(*((IntegerVector*)tmp));
}

//[[Rcpp::export]]
IntegerVector ReturnVectorInt(int index = -1)
{
Expand Down Expand Up @@ -721,6 +767,16 @@ LogicalVector ReturnTableColumnLogical(int index, int entity_index = -1)
);
}

//[[Rcpp::export]]
IntegerVector ReturnTableColumnFactor(int index, int entity_index = -1)
{
void* tmp = cnt.Rcpp_GetEntity(entity_index)->
getTable()->
getTableClm(index-1)->
getVector();
return wrap(*((IntegerVector*)tmp));
}

//[[Rcpp::export]]
IntegerVector ReturnTableColumnInteger(int index, int entity_index = -1)
{
Expand Down
2 changes: 1 addition & 1 deletion src/include/Matrixx.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Matrixx
void* getRowLable() {return lable_row->getVector();}
void* getClmLable() {return lable_clm->getVector();}
void* getMatrix() {
if(mtx->isDate()) {
if(mtx->isDate() || mtx->isString()) {
return mtx->getStringVector();
}
return mtx->getVector();
Expand Down
23 changes: 23 additions & 0 deletions src/include/R_CPP_Connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Rcpp_Connector
void Rcpp_UploadEntity(vector <bool>& mtx, vector <int>& NAIndex, int row, int clm);
void Rcpp_UploadEntity(vector <int>& mtx, vector <int>& NAIndex, int row, int clm);
void Rcpp_UploadEntity(vector <double>& mtx, vector <int>& NAIndex, int row, int clm);
void Rcpp_UploadSymbolVector(const std::vector<std::string>& symbolBase, const std::vector<int>& index);
};

void Rcpp_Connector::Rcpp_UploadDateVector(vector <double>& vec, vector <int>& NAIndex)
Expand Down Expand Up @@ -340,6 +341,28 @@ void Rcpp_Connector::Rcpp_UploadMatrixBasic(int type)
socket->write(buffer.getBuffer(), buffer.size(), actual);
}

void Rcpp_Connector::Rcpp_UploadSymbolVector(const std::vector<std::string>& symbolBase, const std::vector<int>& index){
Buffer buffer;
int flag = (DATA_FORM::DF_VECTOR << 8) + DATA_TYPE::DT_SYMBOL + 128;
buffer.write((short) flag);
buffer.write((int) index.size());
buffer.write((int) 1);

//write symbol Base
buffer.write((int) 0); //symbolBaseID
buffer.write((int) symbolBase.size()); //symbolBaseSize
for(const auto& base : symbolBase){
buffer.write(base);
}
//write index
for (auto ind : index)
{
buffer.write(ind);
}
size_t actual = 0;
socket->write(buffer.getBuffer(), buffer.size(), actual);
}

void Rcpp_Connector::Rcpp_UploadEntity(vector <string>& vec, vector <int>& NAIndex)
{
for (unsigned int i = 0; i < NAIndex.size(); i++)
Expand Down
2 changes: 1 addition & 1 deletion src/include/R_Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum R_TYPE {
SCALAR_DATE, SCALAR_DATETIME,
VECTOR_DATE, VECTOR_DATETIME,
MATRIX_DATE, MATRIX_DATETIME,
VECTOR_ANY
VECTOR_ANY, VECTOR_FACTOR
};

#define DDB_NULL_INTEGER -2147483648
Expand Down
Loading

0 comments on commit 522f359

Please sign in to comment.