Rblp / Rblpapi

R package interfacing the Bloomberg API from https://www.bloomberglabs.com/api/
Other
167 stars 75 forks source link

Add test_bds.R #352

Closed mtkerbeR closed 2 years ago

mtkerbeR commented 2 years ago

As discussed in #351 :

Adds a file to test return type of function bds. Note that this checks for the behaviour as of 0.3.11, i.e. bds returning a list whose first element is a data.frame

eddelbuettel commented 2 years ago

Lovely. You even put the old comment around as if it had been a RUnit function ;-) And the header / copyright comment was of course meant as you moment to shine. We can still tweak.

Otherwise good to merge, I guess, and we can sort out if/when to adjust the state of 3.11. Maybe adding boolean flag shortenResult=FALSE can be added that leaves it as a list by default, but if true returns the more compact object?

mtkerbeR commented 2 years ago

@eddelbuettel Thanks for your review - if you ever want to change back to RUnit, not much code to change ;-)

From my point of view, having some boolean flag like shortenResult (or simplifySingleton) would be a good solution to switch between both return types (and modify existing code written before resp. since the change depending on the default).

Maybe the same parameter could also be used for function bdh - and if so, it might probably be set to TRUE to keep the current behavior of bdh (and get back the former behaviour of bds)?

On the one hand, default return types of functions bds and bdh would be similar (if the resulting list is of length 1, return the first entry, otherwise return a list - which might be relevant if a future version of bds accepts more than one security).

On the other hand, such a parameter for bdh would allow for something like the code below, without having to take care of the special case of securities being of length 1:


sec_1 <- c("DAX Index")
sec_2 <- c("DAX Index", "SPX Index")

fields <- "PX_Last"

library(Rblpapi)
library(zoo)
blpConnect()

prices_1 <- bdh(sec_1, "PX_LAST", Sys.Date()-10, returnAs = "zoo", simplifySingleton = FALSE) |> do.call(what = "merge")
prices_2 <- bdh(sec_2, "PX_LAST", Sys.Date()-10, returnAs = "zoo", simplifySingleton = FALSE) |> do.call(what = "merge")
eddelbuettel commented 2 years ago

Yes, exactly. Nice example! The point is to both "limit surprises when scripting" but also be useful when noodling more interactively.

eddelbuettel commented 2 years ago

Ok, lemme merge this and maybe propose a follow-up PR with the 'simplify' argument (borrowing from sapply) in the next few days. I will need one of you good folks to test with Bbg.