How to call the main function of a c code using R?

282 Views Asked by At

I'm trying to create an R package that uses this code: c code

I searched how to do this and tried the following ways:

1 - Using the inline package.

# orderer ####

#' Orderer
#'
#' @export

orderer <- function(associationPreprocessed){
  if(!is.character(associationPreprocessed)){
    stop("Error, the argument 'associationPreprocessed' must be of type 'character'")
  }
  code <- paste0(readLines("src/cfm-seriation.c"), collapse = "\n")
  cfm <- inline::cfunction(signature(argc = "integer", argv = "character"),
                           body = code,
                           language = "C", convention = ".C",
                           libargs = c("-lm", "-lpthread", "-lrt"),
                           includes = c("#include <stdio.h>", "#include <time.h>",
                                        "#include <math.h>", "#include <stdlib.h>",
                                        "#include <string.h>"))
  cfm(1L, paste0("f=", associationPreprocessed))
  return (NULL)
}

I stripped out all the header information of the c code to use this code snippet.

The idea was to pass the path for the data, Escherichia coli, using the argument associationPreprocessed.

2 - Using the .C() function to call main, with the useDynLib(pkg) to dynamically load the .so lib. Similar to the example of this thread.

I believe I got closer to achieving what I expected using the inline::cfuntion(), but I still haven't figured out how to do what I want.

All the examples that I've seen use a simple c code, with only one function.

0

There are 0 best solutions below