Interfacing C, C++ codes in a new R package

192 views Asked by At

Recently, I am creating a new R package with Rcpp for my project including Vines and Local Likelihood estimation with some C files. I created my own functions, collect previously written ones and try to combine all of them for a limited period of time (having not so much experience on C files). While creating the necessary \scr folder to include C files, I faced with these main problems:

  1. Firstly, I would like to use hfunc.c from here for the computation,

https://github.com/cran/CDVine/tree/master/src

with also our .c files for graphical modeling part (written in the format SEXP). Could it be possible to use both under the same \src folder in R package. If it is possible, what could be the fastest modification to load and use both together (I need to call them during the computation at some places). Otherwise, what do you suggest to combine them properly ?

  1. As a second issue, I will use two TMB templates for automatic differentiation, written in terms of .cpp and .h files again. During the web search, I found this site

<http://htmlpreview.github.io/?https://github.com/mlysy/TMBtools/master/doc/TMBtools.html >

to create another folder \src\TMB to put those files in it with, based on TMBtools. This folder automatically created Makevars and Makevars.win having those lines of codes;

# --- TMB-specific Makevars file ---
#
# In principle, TMB model compilation is a completely separate process from
# that of the remainder of 'src'.
# Therefore, other Makevars flags can be added here, e.g.,
#
## CXX_STD = CXX14 # uncomment this line to enable C++14 support
#
# Flags specifically for the TMB compilation can also be set
# through the 'TMB_FLAGS' argument below, e.g.,
#
## TMB_FLAGS = -I"../../inst/include" # add include directory inst/include
#
# --- TMB-specific compiling directives below ---

.PHONY: all tmblib

all: $(SHLIB)
$(SHLIB): tmblib

tmblib:
(cd TMB; $(R_HOME)/bin$(R_ARCH_BIN)/Rscript \
--no-save --no-restore compile.R '$(TMB_FLAGS)')

clean:
rm -rf *.so *.o TMB/*.so TMB/*.o

But since I need to use two templates, it seems I need further changes on those files, as I understand from this example (he suggested to create Makefile in \src)

https://rtbecard.gitlab.io/2018/02/11/Distributing-TMB-in-R-packages.html

But, I am confused a little with the existence of both created Makevars and Makefile, is there any possible conflict ? or any other way to load both TMB templates (LocalLikelihood1 and -2) placed in \src\TMB ? or maybe without using TMBtools but load them properly ?

This is my created new .R file for dynamic libraries;

#'test function for semVineDAG
## usethis namespace: start
## #' @useDynLib test2VDAG, .registration = TRUE
#'
#' @rawNamespace useDynLib(test2VDAG, .registration=TRUE); useDynLib(LocalLikelihood1); useDynLib(LocalLikelihood2)
#' @importFrom Rcpp sourceCpp

And also while building package since I got this note Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’ I already added the following .cpp file to fix it;

// RegisteringDynamic Symbols

#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>

void R_init_markovchain(DllInfo* info) {
  R_registerRoutines(info, NULL, NULL, NULL, NULL);
  R_useDynamicSymbols(info, TRUE);
}

in \src folder based on the suggestions given in this website.

Could you give me guidance about these issues like explaining to a person who knows almost nothing about C language ?

0

There are 0 answers