[R 데이터 분석] Using C/C++ in R , R언어에서 C/C++ 사용하기

Resources

C and C++ Basics

These are compiled languages and hence a compiler is needed to transform a human interpret able program into a machine executable one. The most commonly use compiler is gcc. This is already installed on most Linux distributions, including all the servers we have commonly used in this course. If you wish to compile C/C++ programs on your personal computer, you will need to install a compiler.
Mac users should download Xcode from Apple’s app store. Windows users should investigate Rtools or Cygwin.

There are three important differences from R

  • Variables and functions need to be declared before use.
  • Arrays are indexed from zero.
  • Arrays are stored in row-major order.
  • Statements are delimited using semicolons “;”.
  • Loops have minimal overhead and hence generally do not need to be avoided.

C Basics

We will review the following topics from C Tutorial:

  • Program Structure
  • Basic Syntax
  • Data types (Integer and Floating Point Types)
  • Variables
  • Operators (Arithmetic, Relational, Logical and Assignment)
  • Loops
  • Arrays
  • Pointers
  • Type Casting

We will not review these as they work very much like in R:

  • Decision Making

The remaining sections are mostly beyond the scope of what we will discuss here.

Calling C functions from R

We can call a function written in C from R using .C() or .call(). We begin with .C as it is simpler.

Using .C()

The basic recipe for using .C is as follows:

  • Write a function in .C that returns void and stores the desired result in one or more arguments expressly created for this purpose. Your function should have a C_ prefix.

  • Compile your function into a shared library accessible to R using this command: R CMD SHLIB my_func.c with my_func.c replaced by your c program.

  • Within an active R session, link to the shared library using dyn.load("my_func.so").

  • Write an R wrapper to call your C function using .C with the following syntax: .C("C_my_func", arg1, arg2). The reason to use an R wrapper is to ensure that the arguments passed are of the correct size and type in order to avoid potentially fatal errors.

The middle two steps can be avoided by including these functions into an R package. We will discuss this briefly when turning to C++ and Rcpp.

A conceptual key to understanding how .C works is realizing that function arguments are passed as pointers to objects in memory rather than by value.

We will look at some examples of C coding available here.

Using .Call()

We can use .Call() to create and modify R level objects directly using functions in C. R level objects are of type SEXP for “S-expression”. These types are defined in the header file Rinternals.h which should be included in all C functions to be called with .Call().

When we create R level objects, we must protect them from garbage collection using PROTECT or a similar function. We later UNPROTECT them to allow the memory to be reallocated.

Recall that scalars in R are length one vectors. Consequently, the must be coerced to C type scalars to be uses as such. This can be done using the C functions as*asLogicalasIntegerasRealCHAR(asChar()).

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Here is a simple example using the inline::cfunction.

library(inline)
## 
## Attaching package: 'inline'
## The following object is masked from 'package:Rcpp':
## 
##     registerPlugin
seqn = cfunction(signature(n_="integer", start_="integer"), 
 body="
 int i, n = asInteger(n_), start=asInteger(start_);
 SEXP out = PROTECT(allocVector(INTSXP, n));
 for(i=0; i<n; ++i){
   INTEGER(out)[i]=i+start;
 }
 UNPROTECT(1);
 return out;
")
seqn(8, 3)
## [1]  3  4  5  6  7  8  9 10
fib = cfunction(c(n_="integer"), body=
  "
  /* Declaratations */
  int n = asInteger(n_);
  SEXP out = PROTECT(allocVector(INTSXP, n));

  /* Function Body */
  INTEGER(out)[0] = 1;
  if(n > 1)  INTEGER(out)[1]=1;
  if(n > 2)
    {
      for(int i=2; i<n; i++)
        {
        INTEGER(out)[i] = INTEGER(out)[i-2] + INTEGER(out)[i-1];
        }
    }

  /* Clean up and return */
  UNPROTECT(1);
  return out;
  ")
fib(10)
##  [1]  1  1  2  3  5  8 13 21 34 55

Modifying inputs

Be careful about modifying input arguments without duplicating first as other R objects may point to the same location in memory owing to “copy-on-modify” semantics.
Here is a quick example from p. 441 of Advanced R using the “inline” package.

add_three = cfunction(c(x="numeric"),
  "
   REAL(x)[0] = REAL(x)[0] + 3;
   return x; 
  ")
y <- x <- 1
add_three(x)
## [1] 4
x
## [1] 4
y
## [1] 4

Here is a corrected version in which the argument is duplicated rather than modified in place.

add_3 = cfunction(c(x_="numeric"),
  "
   SEXP x = PROTECT(duplicate(x_));
   REAL(x)[0] = REAL(x)[0] + 3;
   UNPROTECT(1);
   return x; 
  ")
y <- x <- 1
add_3(x)
## [1] 4
x
## [1] 1
y
## [1] 1

Interfacing with C++ via Rcpp

The Rcpp package greatly simplifies the process of exposing functions written in C++ to R.

Using sourceCpp

We can use the function sourceCpp to read a function written in C++ into R interactively. The function takes care of the compilation using R CMD SHLIB and automatically generates an R wrapper for the underlying function. The shared library and other files will be written to the directory specified by cacheDir which defaults to a temporary directory for automated clean up.

To use a C++ function via sourceCpp:

  1. Write a your C++ function in a file with extension .cpp.
  2. In the source file, be sure to #include <Rcpp.h>.
  3. Designate functions exposed to R using the tag // [[Rcpp::export]].
  4. Compile and source your function using sourceCpp() and a link to the file.

Here are some minimal examples based on generating Fibonacci numbers. You can download the source code here.

The final Fibonacci example here makes two changes from the previous example:

  1. We add the line using namespace rcpp; to avoid having to type the prefix Rcpp:: when referring to the Rcpp namespace. This is much like using library() within R to add a package to the search path.

  2. We show how to print to the R console from within the C++ function using Rcout. This can be useful for manual debugging.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector FibCpp2(int n)
{
  // Error checking
  if(n <= 0)
    {
      throw std::range_error("n must be a positive integer");
    }

  // Allocate memory
  IntegerVector out(n);
  out[0]=1;

  Rcout << "Starting computations ... ";
  // Compute additional terms
  if(n > 0)
    {
      out[1]=1;
      int i;
      for(i=2; i<n; i++)
        {
          out[i] = out[i-1] + out[i-2];
        }
    }

  Rcout << "done." << std::endl;
  return out;
}
sourceCpp('~/Stats506/FibCPP2.cpp')
FibCpp2(10)
## Starting computations ... done.
##  [1]  1  1  2  3  5  8 13 21 34 55

Example

Here is another simple example.

Building a package with Rcpp

The sourceCpp function is convenient for interactive use and initial development. However, for larger projects with multiple functions it is better to maintain the code base using an R package. Rcpp automates much of the work involved in doing this via the compileAttributes function.

This function creates a C++ wrapper to any C++ functions tagged with the // [[Rcpp::export]] attribute that handles type conversion and an R wrapper that calls this function in turn. This makes for a relatively seamless process in which you only occasionally need to look into the translation details.

In the download above is avery simple R package Pkg with the getRegion function from the previous example.

[출처] https://jbhender.github.io/Stats506/F17/Using_C_Cpp.html

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 25084
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 24563
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 25942
21 [국가기술자격 빅데이터분석기사] 빅데이터 분석기사 정보 및 필기 시험 후기 분석 (기출 문제 정리) file 졸리운_곰 2022.09.21 56
20 [국가기술자격 빅데이터분석기사] `빅데이터분석기사` 자격증의 허상 졸리운_곰 2022.09.21 40
19 [국가기술자격 빅데이터분석기사] 빅데이터 분석기사 요약 - 4과목. 빅데이터 결과 해석 졸리운_곰 2022.09.19 29
18 [국가기술자격 빅데이터분석기사]빅데이터 분석기사 요약 - 3과목. 빅데이터 모델링 file 졸리운_곰 2022.09.19 41
17 [국가기술자격 빅데이터분석기사]빅데이터 분석기사 요약 - 2과목. 빅데이터 탐색 file 졸리운_곰 2022.09.19 24
16 [국가기술자격 빅데이터분석기사]빅데이터 분석기사 요약 - 1과목. 빅데이터 분석 기획 졸리운_곰 2022.09.19 49
15 [국가기술자격 빅데이터분석기사] [빅데이터분석기사 필기 요약] I.빅데이터 분석 기획 - 02. 데이터 분석 계획 file 졸리운_곰 2022.09.15 40
14 [국가기술자격 빅데이터분석기사][빅데이터분석기사 필기 요약] I.빅데이터 분석 기획 - 01. 빅데이터의 이해 (3) 졸리운_곰 2022.09.15 10
13 [국가기술자격 빅데이터분석기사][빅데이터분석기사 필기 요약] I.빅데이터 분석 기획 - 01. 빅데이터의 이해 (2) file 졸리운_곰 2022.09.13 17
12 [국가기술자격 빅데이터분석기사] [빅데이터분석기사 필기 요약] I.빅데이터 분석 기획 - 01. 빅데이터의 이해 (1) 졸리운_곰 2022.09.13 29
11 [국가기술자격 빅데이터분석기사] [인공지능] 베이즈 정리 ( Bayes' theorem ) file 졸리운_곰 2022.09.04 21
10 [ 국가기술자격 빅데이터분석기사 ] 빅데이터와 베이즈 정리 file 졸리운_곰 2022.09.04 42
9 [국가기술자격 빅데이터분석기사] 빅데이터분석기사 - 필기 정리1 file 졸리운_곰 2022.09.01 119
8 [국가기술자격 빅데이터분석기사] 빅데이터분석기사 노트정리 공유 file 졸리운_곰 2022.09.01 171
7 [국가기술자격 빅데이터분석기사] 빅데이터 분석기사 : 개요 및 특징 file 졸리운_곰 2022.09.01 60
6 [국가기술자격 빅데이터분석기사] 빅데이터 분석 기사 개요 file 졸리운_곰 2022.09.01 24
5 [빅데이터분석기사 실기] 빅데이터분석기사 3회 실기 문제 복원 (기출) + 시험공부 팁 file 졸리운_곰 2022.02.07 84
4 [빅데이터분석기사] 빅데이터분석기사 노트정리 공유 file 졸리운_곰 2022.02.07 1504
3 [빅데이터분석기사] 실기 : 시험 후기, [자격증] 빅데이터분석기사 실기 합격 후기 (비전공자 문과) file 졸리운_곰 2022.02.04 138
2 [자격소개] 국가기술 빅데이터 분석기사 졸리운_곰 2021.01.07 94
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED