Coder Social home page Coder Social logo

cbuild's Introduction

cbuild

R build status Travis build status Codecov test coverage

The goal of cbuild is to provide tools for working with C both interactively and when constructing an R package. The two broad goals are:

  • Provide a way to interactively source C code into your R session. See source_function() and source_code() to get started.

  • Provide an automatic registration system for R package developers that use C, see process_attributes(). It can automatically generate the init.c file for you, using a comment system similar to Rcpp::export. For example, the following would generate an entry for the C function fn() in init.c, and generate the glue code to export it as an R routine named fn, which you could call from the R side with .Call(fn, 1):

    // [[ export() ]]
    SEXP fn(SEXP x) {
      return x;
    }
    

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("DavisVaughan/cbuild")

Examples

library(cbuild)

The easiest way to get started is with source_function(), which allows you to source a C function from text. It automatically includes R.h and Rinternals.h for you to use.

fn <- source_function("
  SEXP fn(SEXP x) {
    return x;
  }
")

fn(1)
#> [1] 1

From there, you can use source_code() to source larger chunks of code. Tag functions that you want to export with // [[ export() ]].

fns <- source_code("
  static SEXP helper(SEXP x) {
    return x;
  }
  
  // [[ export() ]]
  SEXP fn1(SEXP x) {
    return helper(x);
  }
  
  // [[ export() ]]
  SEXP fn2(SEXP x, SEXP y) {
    double result = REAL(x)[0] + REAL(y)[0];
    return Rf_ScalarReal(result);
  }
")

fns$fn1(1)
#> [1] 1

fns$fn2(1, 2)
#> [1] 3

If you have a full file to source, you can use source_file().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.