Skip to content

Commit

Permalink
Merge pull request #205 from Rblp/bdh-use-double
Browse files Browse the repository at this point in the history
use double for int32 and int64 field types in bdh (closes #163)
  • Loading branch information
eddelbuettel authored Nov 18, 2016
2 parents f435d29 + c279954 commit 40a6ac4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2016-11-18 John Laing <[email protected]>

* R/bdh.R: Add option to use double for int32 and int64 field types in bdh
* R/RcppExports.R: Ditto
* man/bdh.Rd: Ditto
* src/RcppExports.cpp: Ditto
* src/bdh.cpp: Ditto
* inst/unitTests/runit.bdh.R: Ditto

2016-10-25 Dirk Eddelbuettel <[email protected]>

* DESCRIPTION (Version, Date): Release 0.3.5
Expand Down
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ authenticate_Impl <- function(con_, uuid_, ip_address_) {
.Call('Rblpapi_authenticate_Impl', PACKAGE = 'Rblpapi', con_, uuid_, ip_address_)
}

bdh_Impl <- function(con_, securities, fields, start_date_, end_date_, options_, overrides_, verbose, identity_) {
.Call('Rblpapi_bdh_Impl', PACKAGE = 'Rblpapi', con_, securities, fields, start_date_, end_date_, options_, overrides_, verbose, identity_)
bdh_Impl <- function(con_, securities, fields, start_date_, end_date_, options_, overrides_, verbose, identity_, int_as_double) {
.Call('Rblpapi_bdh_Impl', PACKAGE = 'Rblpapi', con_, securities, fields, start_date_, end_date_, options_, overrides_, verbose, identity_, int_as_double)
}

bdp_Impl <- function(con_, securities, fields, options_, overrides_, verbose, identity_) {
Expand Down
8 changes: 6 additions & 2 deletions R/bdh.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
##' @param con A connection object as created by a \code{blpConnect}
##' call, and retrieved via the internal function
##' \code{defaultConnection}.
##' @param int.as.double A boolean indicating whether integer fields should
##' be retrieved as doubles instead. This option is a workaround for very
##' large values which would overflow int32. Defaults to \sQuote{FALSE}
##' @return A list with as a many entries as there are entries in
##' \code{securities}; each list contains a data.frame with one row
##' per observations and as many columns as entries in
Expand Down Expand Up @@ -71,7 +74,8 @@
##' }
bdh <- function(securities, fields, start.date, end.date=NULL,
include.non.trading.days=FALSE, options=NULL, overrides=NULL,
verbose=FALSE, identity=NULL, con=defaultConnection()) {
verbose=FALSE, identity=NULL, con=defaultConnection(),
int.as.double=getOption("blpIntAsDouble", FALSE)) {
if (!class(start.date) == "Date") stop("start.date must be a Date object", call.=FALSE)
start.date <- format(start.date, format="%Y%m%d")
if (!is.null(end.date)) {
Expand All @@ -85,7 +89,7 @@ bdh <- function(securities, fields, start.date, end.date=NULL,
}

res <- bdh_Impl(con, securities, fields, start.date, end.date, options, overrides,
verbose, identity)
verbose, identity, int.as.double)
if (typeof(res)=="list" && length(res)==1) {
res <- res[[1]]
}
Expand Down
11 changes: 11 additions & 0 deletions inst/unitTests/runit.bdh.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ if (.runThisTest) {
checkTrue(length(col.types)==1L && col.types=="Date",
msg = "check column types == 'Date'")
}

test.bdhIntAsDouble <- function() {
checkException(bdh("SPX Index", "PX_VOLUME", as.Date("2000-12-14"), as.Date("2000-12-15")),
msg = "check int overflow")

res <- bdh("SPX Index", "PX_VOLUME", as.Date("2000-12-14"), as.Date("2000-12-15"), int.as.double=TRUE)
checkTrue(!is.integer(res$PX_VOLUME),
msg = "check volume is not integer")
checkTrue(is.numeric(res$PX_VOLUME),
msg = "check volume is numeric")
}
}
7 changes: 6 additions & 1 deletion man/bdh.Rd

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

7 changes: 4 additions & 3 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ BEGIN_RCPP
END_RCPP
}
// bdh_Impl
Rcpp::List bdh_Impl(SEXP con_, std::vector<std::string> securities, std::vector<std::string> fields, std::string start_date_, SEXP end_date_, SEXP options_, SEXP overrides_, bool verbose, SEXP identity_);
RcppExport SEXP Rblpapi_bdh_Impl(SEXP con_SEXP, SEXP securitiesSEXP, SEXP fieldsSEXP, SEXP start_date_SEXP, SEXP end_date_SEXP, SEXP options_SEXP, SEXP overrides_SEXP, SEXP verboseSEXP, SEXP identity_SEXP) {
Rcpp::List bdh_Impl(SEXP con_, std::vector<std::string> securities, std::vector<std::string> fields, std::string start_date_, SEXP end_date_, SEXP options_, SEXP overrides_, bool verbose, SEXP identity_, bool int_as_double);
RcppExport SEXP Rblpapi_bdh_Impl(SEXP con_SEXP, SEXP securitiesSEXP, SEXP fieldsSEXP, SEXP start_date_SEXP, SEXP end_date_SEXP, SEXP options_SEXP, SEXP overrides_SEXP, SEXP verboseSEXP, SEXP identity_SEXP, SEXP int_as_doubleSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Expand All @@ -34,7 +34,8 @@ BEGIN_RCPP
Rcpp::traits::input_parameter< SEXP >::type overrides_(overrides_SEXP);
Rcpp::traits::input_parameter< bool >::type verbose(verboseSEXP);
Rcpp::traits::input_parameter< SEXP >::type identity_(identity_SEXP);
rcpp_result_gen = Rcpp::wrap(bdh_Impl(con_, securities, fields, start_date_, end_date_, options_, overrides_, verbose, identity_));
Rcpp::traits::input_parameter< bool >::type int_as_double(int_as_doubleSEXP);
rcpp_result_gen = Rcpp::wrap(bdh_Impl(con_, securities, fields, start_date_, end_date_, options_, overrides_, verbose, identity_, int_as_double));
return rcpp_result_gen;
END_RCPP
}
Expand Down
9 changes: 8 additions & 1 deletion src/bdh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Rcpp::List bdh_Impl(SEXP con_,
std::vector<std::string> fields,
std::string start_date_, SEXP end_date_,
SEXP options_, SEXP overrides_,
bool verbose, SEXP identity_) {
bool verbose, SEXP identity_,
bool int_as_double) {

Session* session =
reinterpret_cast<Session*>(checkExternalPointer(con_,"blpapi::Session*"));
Expand All @@ -109,6 +110,12 @@ Rcpp::List bdh_Impl(SEXP con_,
rtypes.push_back(fieldInfoToRblpapiT(f.datatype,f.ftype));
}

// for bdh request all int fields as doubles b/c of implicit bbg conversion
if (int_as_double) {
std::transform(rtypes.begin(), rtypes.end(), rtypes.begin(),
[](RblpapiT x) { return x == RblpapiT::Integer || x == RblpapiT::Integer64 ? RblpapiT::Double : x; });
}

const std::string rdsrv = "//blp/refdata";
if (!session->openService(rdsrv.c_str())) {
Rcpp::stop("Failed to open " + rdsrv);
Expand Down

0 comments on commit 40a6ac4

Please sign in to comment.