1

I would like to, within my c++ code that will be called in an R package (use .C function in R), call a function from a specific R package (The function does not belong to base R). Based on my searches, we could include R.H and Rmath.H files in the case of Base R functions are needed (e.g. rnorm or pnorm). However, the specific function I need to call within the c++ file belongs to a standalone package (condMVN from condMVNorm package).

My guess is that we need to find header files for the condMVNorm package that include the header for the condMVN function. Then including this header file would allow me to compile the c++ code and call this compiled function in R using .C. However, I have difficulties in finding an affirmative answer to my question and finding the header files from the condMVNorm package.

References:

-https://dms.umontreal.ca/wiki/index.php/Writing_R_Extensions#The_R_API:_entry_points_for_C_code

Any help or suggestions would be appreciated. Thanks for your time.

jtianpy
  • 21
  • 3
  • 1
    Can you not go along these lines https://stackoverflow.com/questions/38016851/call-r-functions-in-rcpp and https://stackoverflow.com/questions/38703682/calling-function-from-package-in-rcpp-code/38704319. (perhaps I'm not understanding but [condMVNorm](https://github.com/cran/condMVNorm) doesn't seem to include compiled code) – user20650 Sep 07 '21 at 15:58
  • Thanks for pointing it out. I did come across the idea of using the RCPP package. After went through the RCPP book and other references, it seems to me that RCPP is useful in the case where an R instance is needed in a standalone c++ program. In our case, we hope to call this condMVNorm function in just one function of our c++ program. The whole c++ program will be wrapped as a function and be called in R using .C function. This is the reason why I am wondering if there is a way to call a function from different package like we are calling pnorm in c++. – jtianpy Sep 08 '21 at 01:26
  • "*Rcpp is useful in the case where an R instance is needed in a standalone c++ program.*". This isn't quite right. Rcpp provides the glue for easily passing R objects to c++ and back again. So really its for integrating c++ functions which will be called in R rather than the other way around. – user20650 Sep 08 '21 at 01:45
  • Probably I didn't understand the difference between Rinside and RCPP. Do you mean that using RCPP, in this case, is a way to go? Sorry for the silly question as I am quite new to the interaction between R and C++. Thanks for the help! – jtianpy Sep 08 '21 at 04:18
  • Rinside is used to embed r in c++ apps. Rcpp allows you to easily call c++ function in R. It allows passing objects from R to c++ and back again easily. The way I understand your question, is that Rcpp is the way to go. Check out https://stackoverflow.com/questions/38703682/calling-function-from-package-in-rcpp-code/38704319 to see how to use an R function in c++ using Rcpp. There are a bunch on slides at https://cran.r-project.org/web/packages/Rcpp/index.html and examples at https://gallery.rcpp.org/ – user20650 Sep 08 '21 at 08:42

0 Answers0