I've been converting some R code to Rcpp functions recently. I'm simulating people parking their cars in parking lots. I have a function which picks which lot a person will park in based on what gate they enter through and the fullness of the parking lots.
#include <Rcpp.h>
#include <numeric>
#include <chrono>
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
// [[Rcpp::export]]
std::string pickLotcpp(std::string gate,DataFrame dist,DataFrame curr,NumericVector maxDist = 0.005) {
std::vector<std::string> gates = Rcpp::as<std::vector<std::string> >(dist["inGate"]);
std::vector<std::string> lots = Rcpp::as<std::vector<std::string> >(dist["lot"]);
NumericVector d = dist["dist"];
std::vector<std::string> currLots = Rcpp::as<std::vector<std::string> >(curr["Lot"]);
NumericVector cap = curr["Cap"];
NumericVector util = curr["Util"];
NumericVector percFree = (cap - util)/cap;
std::vector<std::string> relLot;
NumericVector relD;
int n = gates.size();
for(int i = 0; i < n; i++){
if(gates[i] == gate){
if(d[i] <= maxDist[0]){
relLot.push_back(lots[i]);
relD.push_back(pow(d[i],-2));
}
}
}
n = relLot.size();
int n2 = currLots.size();
NumericVector relPerc;
for(int i = 0; i < n; i++){
for(int j = 0; j < n2; j++){
if(relLot[i] == currLots[j]){
relPerc.push_back(percFree[j]);
}
}
}
relD = relD*relPerc;
NumericVector csV(relD.size());
std::partial_sum(relD.begin(), relD.end(), csV.begin());
NumericVector::iterator mv;
mv = std::max_element(csV.begin(),csV.end());
double maxV = *mv;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 gen(seed);
std::uniform_real_distribution<> dis(0, maxV);
double rv = dis(gen);
int done = 0;
int i = 0;
std::string selGate;
while(done < 1){
if(csV[i] >= rv){
selGate = relLot[i];
done = 1;
}
i++;
}
return selGate;
}
which works great in R:
fakeDist = structure(list(inGate = c("A", "A", "B", "B"), lot = c("Y", "Z", "Y", "Z"), dist = c(0.001, 0.003, 0.003, 0.001)), .Names = c("inGate", "lot", "dist"), row.names = c(NA, 4L), class = c("tbl_df", "tbl", "data.frame"))
fakeStatus = structure(list(Lot = c("Y", "Z"), Cap = c(100, 100), Util = c(0, 0)), .Names = c("Lot", "Cap", "Util"), row.names = c(NA, 2L), class = c("tbl_df", "tbl", "data.frame"))
pickLotcpp("A",fakeDist,fakeStatus)
#> [1] "Y"
Now I'm trying to write the function which will loop through all the gate activity and park people sequentially. So I have this Rcpp function:
// [[Rcpp::export]]
List test(DataFrame records, DataFrame currentLoc, DataFrame dist,
DataFrame currentStatus, NumericVector times){
List out(times.size());
NumericVector recID = records["ID"];
NumericVector recTime = records["Time"];
NumericVector recDir = records["Dir"];
std::vector<std::string> Gate = Rcpp::as<std::vector<std::string> >(records["Gate"]);
NumericVector currState = currentLoc["State"];
std::vector<std::string> currLot = Rcpp::as<std::vector<std::string> >(currentLoc["Lot"]);
out[0] = pickLotcpp(Gate[0],dist,currentStatus);
return out;
}
This is in the same file, under pickLotcpp. It compiles fine, but when called causes R to crash.
fakeData = structure(list(ID = c(1, 2, 3), Time = c(1, 2, 3), Dir = c(1, 1, 1), Gate = c("A", "A", "B")), .Names = c("ID", "Time", "Dir", "Gate"), row.names = c(NA, 3L), class = c("tbl_df", "tbl", "data.frame"))
fakeLoc = structure(list(ID = c(1, 2, 3), State = c(0, 0, 0), Lot = c("", "", "")), .Names = c("ID", "State", "Lot"), row.names = c(NA, 3L), class = c("tbl_df", "tbl", "data.frame"))
a = test(fakeData, fakeLoc, fakeDist, fakeStatus, 10)
I've written other Rcpp code where functions call functions and they work fine. The only thing I can think of is that I'm passing a DataFrame that was an input directly to the other function but I can't find anything that says I can't. I'm not an expert C++ programmer - I just started hacking in it a couple weeks ago and this has me stumped.
How can I call pickLotcpp
from test
passing along the needed distance and status data frames?
It seems not related to Rcpp.
Can you double check the line
selGate = relLot[i];
relLot
might be empty.