move N,D from CPP to R function