Skip to content

Commit 522f359

Browse files
committed
R api 3.0.0.1 release
1 parent f47f184 commit 522f359

29 files changed

+2629
-921
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: RDolphinDB
22
Type: Package
33
Title: Connecting to DolphinDB server with R
4-
Version: 2.0.11.1
5-
Date: 2024-03-19
4+
Version: 3.0.0.1
5+
Date: 2024-05-17
66
Author: mrDrivingDuck
77
Maintainer: mrDrivingDuck <jingtang.zhang@dolphindb.com>
88
Description: The R API of DolphinDB.

R/RDolphinDB.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ setClass("RDolphinDB", slots = list(connected = "logical"))
2222
#'
2323
#' @description Method of getting connection with DolphinDB server.
2424
#' @description (If the input contains 'username' and 'password', a log-in job will be done)
25+
#' @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
2526
#' @param conn The connector object.
2627
#' @param host The host running the DolphinDB server.
2728
#' @param port The port running the DolphinDB server.

R/RDolphinDB_impl.R

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ DDB_GetEntity <- function(xxdb_type) {
231231

232232
} else if (xxdb_type == 12) {
233233
# Character Matrix
234-
return (NULL)
234+
result <- ReturnMatrixString()
235+
result <- DDB_SetReceiveVectorNA(result, ReturnMatrixNAIndex())
236+
result <- DDB_ReceiveMatrixLable(result)
237+
Clear()
238+
return (result)
235239

236240
} else if (xxdb_type == 13) {
237241
# DataFrame
@@ -276,6 +280,10 @@ DDB_GetEntity <- function(xxdb_type) {
276280
clm[NAIndex] <- NA
277281
result <- cbind(result, clm)
278282

283+
} else if (typelist[i] == 21) {
284+
# factor
285+
clm <- ReturnTableColumnFactor(i)
286+
result <- cbind(result, clm)
279287
} else {
280288
print("error in DataFrame")
281289
Clear()
@@ -387,6 +395,10 @@ DDB_GetEntity <- function(xxdb_type) {
387395
result <- DDB_SetReceiveVectorNA(result, ReturnVectorNAIndex(i))
388396
result <- result
389397

398+
} else if (anytypelist[i] == 21) {
399+
# Factor
400+
result <- ReturnVectorFactor(i)
401+
390402
} else if (anytypelist[i] == 9) {
391403
# Logical Matrix
392404
result <- ReturnMatrixBool(i)
@@ -462,6 +474,10 @@ DDB_GetEntity <- function(xxdb_type) {
462474
clm[NAIndex] <- NA
463475
result <- cbind(result, clm)
464476

477+
} else if (typelist[k] == 21) {
478+
# factor
479+
clm <- ReturnTableColumnFactor(k, i)
480+
result <- cbind(result, clm)
465481
} else {
466482
print("error in DataFrame")
467483
Clear()
@@ -470,6 +486,10 @@ DDB_GetEntity <- function(xxdb_type) {
470486
}
471487

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

475495
} else {
@@ -481,6 +501,12 @@ DDB_GetEntity <- function(xxdb_type) {
481501
Clear()
482502
return (anyVector)
483503

504+
} else if (xxdb_type == 21) {
505+
# Factor
506+
result <- ReturnVectorFactor()
507+
Clear()
508+
return (result)
509+
484510
} else {
485511
# print("Error")
486512
Clear()
@@ -572,11 +598,15 @@ DDB_UploadVector <- function(vec) {
572598
NAIndex <- DDB_SetUploadVectorNA(vec)
573599
UploadVectorDouble(vec, NAIndex)
574600

575-
} else if (is.character(vec) || is.factor(vec)) {
601+
} else if (is.character(vec)) {
576602

577603
NAIndex <- DDB_SetUploadVectorNA(vec)
578604
UploadVectorString(vec, NAIndex)
579605

606+
} else if(is.factor(vec)){
607+
608+
UploadVectorSymbol(vec)
609+
580610
} else if (length(class(vec)) > 1 &&
581611
class(vec)[1] == "POSIXct") {
582612

@@ -691,6 +721,7 @@ DDB_UploadObjectCheck <- function(args) {
691721

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

724+
} else if (is.factor(args[[i]])){
694725
} else {
695726
print("Data form not support yet.")
696727
return (FALSE)
@@ -713,6 +744,8 @@ DDB_UploadEntity <- function(args) {
713744
DDB_UploadMatrix(args[[i]])
714745
} else if (is.data.frame(args[[i]])) {
715746
DDB_UploadTable(args[[i]])
747+
} else if (is.factor(args[[i]])){
748+
UploadVectorSymbol(args[[i]])
716749
} else if (is.vector(args[[i]]) && length(args[[i]]) > 1) {
717750
DDB_UploadVector(args[[i]])
718751
} else if (is.vector(args[[i]]) && length(args[[i]]) == 1) {

R/RcppExports.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ UploadMatrixDouble <- function(R_mtx, R_NAIndex) {
6969
invisible(.Call('_RDolphinDB_UploadMatrixDouble', PACKAGE = 'RDolphinDB', R_mtx, R_NAIndex))
7070
}
7171

72+
UploadVectorSymbol <- function(R_vec) {
73+
invisible(.Call('_RDolphinDB_UploadVectorSymbol', PACKAGE = 'RDolphinDB', R_vec))
74+
}
75+
7276
UploadVectorString <- function(R_vec, R_NAIndex) {
7377
invisible(.Call('_RDolphinDB_UploadVectorString', PACKAGE = 'RDolphinDB', R_vec, R_NAIndex))
7478
}
@@ -153,6 +157,10 @@ ReturnVectorBool <- function(index = -1L) {
153157
.Call('_RDolphinDB_ReturnVectorBool', PACKAGE = 'RDolphinDB', index)
154158
}
155159

160+
ReturnVectorFactor <- function(index = -1L) {
161+
.Call('_RDolphinDB_ReturnVectorFactor', PACKAGE = 'RDolphinDB', index)
162+
}
163+
156164
ReturnVectorInt <- function(index = -1L) {
157165
.Call('_RDolphinDB_ReturnVectorInt', PACKAGE = 'RDolphinDB', index)
158166
}
@@ -241,6 +249,10 @@ ReturnTableColumnLogical <- function(index, entity_index = -1L) {
241249
.Call('_RDolphinDB_ReturnTableColumnLogical', PACKAGE = 'RDolphinDB', index, entity_index)
242250
}
243251

252+
ReturnTableColumnFactor <- function(index, entity_index = -1L) {
253+
.Call('_RDolphinDB_ReturnTableColumnFactor', PACKAGE = 'RDolphinDB', index, entity_index)
254+
}
255+
244256
ReturnTableColumnInteger <- function(index, entity_index = -1L) {
245257
.Call('_RDolphinDB_ReturnTableColumnInteger', PACKAGE = 'RDolphinDB', index, entity_index)
246258
}

README_CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,5 @@ help("dbClose")
108108
|NANOTIMESTAMP|2012.06.13T13:30:10.008007006|POSIXct|2012-06-13 13:30:10|
109109
|FLOAT|2.1f|Numeric|2.1|
110110
|DOUBLE|2.1|Numeric|2.1|
111-
|STRING|"123"|Character|"123"|
111+
|STRING|"123"|Character|"123"|
112+
|SYMBOL|"123"|Factor|"123"|

man/dbConnect.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ BEGIN_RCPP
189189
return R_NilValue;
190190
END_RCPP
191191
}
192+
// UploadVectorSymbol
193+
void UploadVectorSymbol(IntegerVector R_vec);
194+
RcppExport SEXP _RDolphinDB_UploadVectorSymbol(SEXP R_vecSEXP) {
195+
BEGIN_RCPP
196+
Rcpp::RNGScope rcpp_rngScope_gen;
197+
Rcpp::traits::input_parameter< IntegerVector >::type R_vec(R_vecSEXP);
198+
UploadVectorSymbol(R_vec);
199+
return R_NilValue;
200+
END_RCPP
201+
}
192202
// UploadVectorString
193203
void UploadVectorString(CharacterVector R_vec, IntegerVector R_NAIndex);
194204
RcppExport SEXP _RDolphinDB_UploadVectorString(SEXP R_vecSEXP, SEXP R_NAIndexSEXP) {
@@ -409,6 +419,17 @@ BEGIN_RCPP
409419
return rcpp_result_gen;
410420
END_RCPP
411421
}
422+
// ReturnVectorFactor
423+
IntegerVector ReturnVectorFactor(int index);
424+
RcppExport SEXP _RDolphinDB_ReturnVectorFactor(SEXP indexSEXP) {
425+
BEGIN_RCPP
426+
Rcpp::RObject rcpp_result_gen;
427+
Rcpp::RNGScope rcpp_rngScope_gen;
428+
Rcpp::traits::input_parameter< int >::type index(indexSEXP);
429+
rcpp_result_gen = Rcpp::wrap(ReturnVectorFactor(index));
430+
return rcpp_result_gen;
431+
END_RCPP
432+
}
412433
// ReturnVectorInt
413434
IntegerVector ReturnVectorInt(int index);
414435
RcppExport SEXP _RDolphinDB_ReturnVectorInt(SEXP indexSEXP) {
@@ -659,6 +680,18 @@ BEGIN_RCPP
659680
return rcpp_result_gen;
660681
END_RCPP
661682
}
683+
// ReturnTableColumnFactor
684+
IntegerVector ReturnTableColumnFactor(int index, int entity_index);
685+
RcppExport SEXP _RDolphinDB_ReturnTableColumnFactor(SEXP indexSEXP, SEXP entity_indexSEXP) {
686+
BEGIN_RCPP
687+
Rcpp::RObject rcpp_result_gen;
688+
Rcpp::RNGScope rcpp_rngScope_gen;
689+
Rcpp::traits::input_parameter< int >::type index(indexSEXP);
690+
Rcpp::traits::input_parameter< int >::type entity_index(entity_indexSEXP);
691+
rcpp_result_gen = Rcpp::wrap(ReturnTableColumnFactor(index, entity_index));
692+
return rcpp_result_gen;
693+
END_RCPP
694+
}
662695
// ReturnTableColumnInteger
663696
IntegerVector ReturnTableColumnInteger(int index, int entity_index);
664697
RcppExport SEXP _RDolphinDB_ReturnTableColumnInteger(SEXP indexSEXP, SEXP entity_indexSEXP) {
@@ -750,6 +783,7 @@ static const R_CallMethodDef CallEntries[] = {
750783
{"_RDolphinDB_UploadMatrixBool", (DL_FUNC) &_RDolphinDB_UploadMatrixBool, 2},
751784
{"_RDolphinDB_UploadMatrixInt", (DL_FUNC) &_RDolphinDB_UploadMatrixInt, 2},
752785
{"_RDolphinDB_UploadMatrixDouble", (DL_FUNC) &_RDolphinDB_UploadMatrixDouble, 2},
786+
{"_RDolphinDB_UploadVectorSymbol", (DL_FUNC) &_RDolphinDB_UploadVectorSymbol, 1},
753787
{"_RDolphinDB_UploadVectorString", (DL_FUNC) &_RDolphinDB_UploadVectorString, 2},
754788
{"_RDolphinDB_UploadVectorDouble", (DL_FUNC) &_RDolphinDB_UploadVectorDouble, 2},
755789
{"_RDolphinDB_UploadVectorBool", (DL_FUNC) &_RDolphinDB_UploadVectorBool, 2},
@@ -771,6 +805,7 @@ static const R_CallMethodDef CallEntries[] = {
771805
{"_RDolphinDB_ReturnScalarTime", (DL_FUNC) &_RDolphinDB_ReturnScalarTime, 1},
772806
{"_RDolphinDB_ReturnScalarDate", (DL_FUNC) &_RDolphinDB_ReturnScalarDate, 1},
773807
{"_RDolphinDB_ReturnVectorBool", (DL_FUNC) &_RDolphinDB_ReturnVectorBool, 1},
808+
{"_RDolphinDB_ReturnVectorFactor", (DL_FUNC) &_RDolphinDB_ReturnVectorFactor, 1},
774809
{"_RDolphinDB_ReturnVectorInt", (DL_FUNC) &_RDolphinDB_ReturnVectorInt, 1},
775810
{"_RDolphinDB_ReturnVectorDouble", (DL_FUNC) &_RDolphinDB_ReturnVectorDouble, 1},
776811
{"_RDolphinDB_ReturnVectorString", (DL_FUNC) &_RDolphinDB_ReturnVectorString, 1},
@@ -793,6 +828,7 @@ static const R_CallMethodDef CallEntries[] = {
793828
{"_RDolphinDB_ReturnTableColumeName", (DL_FUNC) &_RDolphinDB_ReturnTableColumeName, 1},
794829
{"_RDolphinDB_ReturnEmptyDataFrame", (DL_FUNC) &_RDolphinDB_ReturnEmptyDataFrame, 1},
795830
{"_RDolphinDB_ReturnTableColumnLogical", (DL_FUNC) &_RDolphinDB_ReturnTableColumnLogical, 2},
831+
{"_RDolphinDB_ReturnTableColumnFactor", (DL_FUNC) &_RDolphinDB_ReturnTableColumnFactor, 2},
796832
{"_RDolphinDB_ReturnTableColumnInteger", (DL_FUNC) &_RDolphinDB_ReturnTableColumnInteger, 2},
797833
{"_RDolphinDB_ReturnTableColumnDouble", (DL_FUNC) &_RDolphinDB_ReturnTableColumnDouble, 2},
798834
{"_RDolphinDB_ReturnTableColumnTime", (DL_FUNC) &_RDolphinDB_ReturnTableColumnTime, 2},

src/connector_cppapi.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,43 @@ void UploadMatrixDouble(NumericMatrix R_mtx, IntegerVector R_NAIndex)
165165
cnt.Rcpp_UploadEntity(mtx, NAIndex, R_mtx.nrow(), R_mtx.ncol());
166166
}
167167

168+
//[[Rcpp::export]]
169+
void UploadVectorSymbol(IntegerVector R_vec)
170+
{
171+
std::vector<std::string> symbolBase;
172+
std::vector<int> index;
173+
174+
CharacterVector level = R_vec.attr("levels");
175+
LogicalVector isNaVec = is_na(level);
176+
int naIndex = INT_MAX;
177+
178+
//dolphindb中空值固定在首位,需要找到R中的空值位置并跳过
179+
symbolBase.push_back("");
180+
for(int i = 0; i < level.size(); ++i){
181+
if(isNaVec[i]){
182+
naIndex = i == INT_MAX ? i : i + 1;
183+
}
184+
else{
185+
symbolBase.push_back(std::string(level[i]));
186+
}
187+
}
188+
189+
index.reserve(R_vec.size());
190+
for(int i = 0; i < R_vec.size(); ++i){
191+
if(R_vec[i] == naIndex){
192+
index.push_back(0);
193+
}
194+
else if(R_vec[i] > naIndex){
195+
index.push_back(R_vec[i] - 1);
196+
}
197+
else{
198+
index.push_back(R_vec[i]);
199+
}
200+
}
201+
202+
cnt.Rcpp_UploadSymbolVector(symbolBase, index);
203+
}
204+
168205
//[[Rcpp::export]]
169206
void UploadVectorString(CharacterVector R_vec, IntegerVector R_NAIndex)
170207
{
@@ -347,6 +384,15 @@ LogicalVector ReturnVectorBool(int index = -1)
347384
);
348385
}
349386

387+
//[[Rcpp::export]]
388+
IntegerVector ReturnVectorFactor(int index = -1)
389+
{
390+
void* tmp = cnt.Rcpp_GetEntity(index)->
391+
getVector()->
392+
getVector();
393+
return wrap(*((IntegerVector*)tmp));
394+
}
395+
350396
//[[Rcpp::export]]
351397
IntegerVector ReturnVectorInt(int index = -1)
352398
{
@@ -721,6 +767,16 @@ LogicalVector ReturnTableColumnLogical(int index, int entity_index = -1)
721767
);
722768
}
723769

770+
//[[Rcpp::export]]
771+
IntegerVector ReturnTableColumnFactor(int index, int entity_index = -1)
772+
{
773+
void* tmp = cnt.Rcpp_GetEntity(entity_index)->
774+
getTable()->
775+
getTableClm(index-1)->
776+
getVector();
777+
return wrap(*((IntegerVector*)tmp));
778+
}
779+
724780
//[[Rcpp::export]]
725781
IntegerVector ReturnTableColumnInteger(int index, int entity_index = -1)
726782
{

src/include/Matrixx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Matrixx
5858
void* getRowLable() {return lable_row->getVector();}
5959
void* getClmLable() {return lable_clm->getVector();}
6060
void* getMatrix() {
61-
if(mtx->isDate()) {
61+
if(mtx->isDate() || mtx->isString()) {
6262
return mtx->getStringVector();
6363
}
6464
return mtx->getVector();

src/include/R_CPP_Connector.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class Rcpp_Connector
120120
void Rcpp_UploadEntity(vector <bool>& mtx, vector <int>& NAIndex, int row, int clm);
121121
void Rcpp_UploadEntity(vector <int>& mtx, vector <int>& NAIndex, int row, int clm);
122122
void Rcpp_UploadEntity(vector <double>& mtx, vector <int>& NAIndex, int row, int clm);
123+
void Rcpp_UploadSymbolVector(const std::vector<std::string>& symbolBase, const std::vector<int>& index);
123124
};
124125

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

344+
void Rcpp_Connector::Rcpp_UploadSymbolVector(const std::vector<std::string>& symbolBase, const std::vector<int>& index){
345+
Buffer buffer;
346+
int flag = (DATA_FORM::DF_VECTOR << 8) + DATA_TYPE::DT_SYMBOL + 128;
347+
buffer.write((short) flag);
348+
buffer.write((int) index.size());
349+
buffer.write((int) 1);
350+
351+
//write symbol Base
352+
buffer.write((int) 0); //symbolBaseID
353+
buffer.write((int) symbolBase.size()); //symbolBaseSize
354+
for(const auto& base : symbolBase){
355+
buffer.write(base);
356+
}
357+
//write index
358+
for (auto ind : index)
359+
{
360+
buffer.write(ind);
361+
}
362+
size_t actual = 0;
363+
socket->write(buffer.getBuffer(), buffer.size(), actual);
364+
}
365+
343366
void Rcpp_Connector::Rcpp_UploadEntity(vector <string>& vec, vector <int>& NAIndex)
344367
{
345368
for (unsigned int i = 0; i < NAIndex.size(); i++)

0 commit comments

Comments
 (0)