Reputation: 937
I'm trying to realize a simple interop "R+Rcpp" and have strange problem with DataFrame::iterator
and std::algorithm
methods.
I have this code written on Rcpp.
struct TradeIndex
{
Date date;
long startIndex;
long finishIndex;
TradeIndex(Environment &tradeIndex)
{
date = Date(tradeIndex["Date"]);
startIndex = Rcpp::as<long>(tradeIndex["StartIndex"]) - 1;
finishIndex = Rcpp::as<long>(tradeIndex["FinishIndex"]) - 1;
}
};
struct Asset
{
std::string name;
DataFrame ticks;
std::vector<TradeIndex> indexes;
DataFrame highQuantiles;
DataFrame lowQuantiles;
Asset(Environment &asset)
{
ticks = Rcpp::as<DataFrame>(asset["Ticks"]);
indexes = std::vector<TradeIndex>();
auto &&rawIndexes = Rcpp::as<List>(asset["Indexes"]);
std::for_each(rawIndexes.begin(), rawIndexes.end(), [this](auto &&elem)
{
auto RElem = Rcpp::as<Environment>(elem);
this->indexes.push_back(TradeIndex(RElem));
});
highQuantiles = Rcpp::as<DataFrame>(asset["HighQuantiles"]);
lowQuantiles = Rcpp::as<DataFrame>(asset["LowQuantiles"]);
name = Rcpp::as<std::string>(asset["Name"]);
}
};
// [[Rcpp::plugins(cpp17)]]
// [[Rcpp::depends(BH)]]
// [[Rcpp::export]]
List StrategyRealization(Environment &RAsset, double basePortfolioMoney, double comission, Date startDate, Date endDate,
std::string &open, std::string &stopLoss, std::string &takeProfit, int direction, double damperLvl)
{
std::cout << "Started calculation" << std::endl;
string directionString = direction == 1 ? "long" : "short";
auto RAssetDataElem = Rcpp::as<Environment>(RAsset);
auto asset = Asset(RAssetDataElem);
auto &&quantiles = direction == 1 ? asset.lowQuantiles : asset.highQuantiles;
auto &&quantileIterator = std::find_if(quantiles.begin(), quantiles.end(), [&startDate](DataFrame &&elem)
{
DateVector &&dateVector = elem["Date"];
return Date(dateVector[0]) == startDate;
});
...
}
Here highQuantiles
and lowQuantiles
are DataFrames
with columns Date, q1, q2, q3
,
where Date
is a date vector, q1, q2, q3
are numeric vectors.
But this code crashes.
If I write code like this
auto &&quantileIterator = std::find_if(quantiles.begin(), quantiles.end(), [&startDate](auto &&elem)
{
DateVector &&dateVector = elem["Date"];
return Date(dateVector[0]) == startDate;
});
I get <Rcpp::not_compatible in eval(ei, envir): Expecting a single value: [extent=813].>
error.
Please, could you explain me why?
Where can I read about iterators for DataFrame
, List
, GenericVector
.
Upvotes: 0
Views: 62