integrate r into c/c++ apps

27
Using Visual C++ 2008 GONG Yu

Upload: doankhanh

Post on 14-Feb-2017

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Integrate R into C/C++ APpS

Using Visual C++ 2008

GONG Yu

Page 2: Integrate R into C/C++ APpS

Why We need Integrate R Into Our Application

Different interface for Integrate R into ourapplication

How to integrate R into c/c++ app usingVisual C++ 2008

Known issues

Summary

Q&A

Page 3: Integrate R into C/C++ APpS

More and more applications need analysispower

Statistical algorithms are hard to implement

Time for application develop is so limited

So why we need write so complicated code insuch short time, while they are alreadyimplemented by others?

Don’t Reinvent the wheel

Page 4: Integrate R into C/C++ APpS

Interface for C/C++

Rembedded RDCOM(used by RExcel)Pipe(used by

Tinn-R)

Coremethod

Write low-level C/C++ code , linked with R.dll

Using COM tech Direct use R.exe, through pipe to input data, and receive output

Pros 1.Flexible2.Performance3.Low -level

1 .Easy to use 2 .Can used in different language and applications

1.Easy to use 2. Can used in different language

Cons 1.Little Docs2.Need read R code

1. Need marshaling and un-marshaling

2. Performance3. Not open source

1. Performance2. Stability

Page 5: Integrate R into C/C++ APpS

Interface for Other Language

Language Java Python

Interface rJava rpy, rpy2

website http://www.rforge.net/rJava rpy.sourceforge.net

Page 6: Integrate R into C/C++ APpS

Those interface(rdcom , rjava , rpy,etc.) all used low-level Rembeddedcode.

If we know how to use low-levelcode to integrate R into application,we can easily develop analysisapplication base on R.

How?

Page 7: Integrate R into C/C++ APpS

PrerequisiteRtools(www.murdoch-sutherland.com/Rtools)

R source code (using subversion)

Visual C++ 2008 express (MSDN)

ProcedureCompile R, generate .def file

Generate .lib file for visual c++

Using visual c++ write our application

Page 8: Integrate R into C/C++ APpS

Before compile R , Edit src\gnuwin32\makefile

R.dll: $(OBJS) $(OBJS-EXTRA) $(MAINLIBS) $(EXTRALIBS) dllversion.o

@$(ECHO) EXPORTS > R.def

@$(NM) $^ | $(SED) -n 's/^.* [BCDRT] _/ /p' | $(SORT) | uniq > R0.def

@comm -23 R0.def Rdll.hide >> R.def

cp R.def ../../R.def

$(DLL) -shared $(DLLFLAGS) $($*-DLLFLAGS) -o $@ R.def $^ $($*-DLLLIBS) $(DLLLIBS)

@$(RM) R.def R0.def

Page 9: Integrate R into C/C++ APpS

Run CMD, enter the src\gnuwin32 dir

Type make ,then press enter key

Page 10: Integrate R into C/C++ APpS

After compile ,the r.def will in the R root dir

Or we can use pexports to export the .def file

http://www.emmestech.com/software/pexports-0.43/download_pexports.html

Page 11: Integrate R into C/C++ APpS

Run Visual Studio 2008 命令提示

Page 12: Integrate R into C/C++ APpS

Use visual c++ 2008 express create an empty console application, then write code

LoadRCreate Object

(vector ,matrix, etc.)Define Var Name

(Optional)

Run R Code(ParseCode,EvalCode)

Output ResultPlot Graphic(Optional)

EndR

Page 13: Integrate R into C/C++ APpS

Load R:

Page 14: Integrate R into C/C++ APpS

Create Object : we can create integer, real, string,bool vector or matrix.

Be carful with string encode !!!

Page 15: Integrate R into C/C++ APpS

Define Var Name :

Page 16: Integrate R into C/C++ APpS

Run R Code:

Page 17: Integrate R into C/C++ APpS

Output Result:

Page 18: Integrate R into C/C++ APpS

Put All together: main

.c

Page 19: Integrate R into C/C++ APpS

RE

ng

ine.h

Page 20: Integrate R into C/C++ APpS

Ren

gin

e.c

Page 21: Integrate R into C/C++ APpS

When Parse R Code(ParseOneLine) we canonly know it’s right or not, but can’t get errormessage like RGUI or Rconsole does

When Execute R Code(ExcuteOneLine),we alsocan’t get error message

To get error message we need modify R sourceCode( in r-devel mailing list there have somediscusses but not correct)

Page 22: Integrate R into C/C++ APpS

In original PDF output, the Chinese font too ugly

When need PDF output, use Cairo instead

attention: original Cairo code can’t display Chinesecharacter correctly ,need modify, in cairotalk.cchange to following !!!

Page 23: Integrate R into C/C++ APpS

Modify R MakefileCompile to

generate .def fileGenerate .lib file

Write own code using visual c++

Page 24: Integrate R into C/C++ APpS

LoadRCreate Object

(vector ,matrix, etc.)Define Var Name

(Optional)

Run R Code(ParseCode,EvalCode)

Output ResultPlot Graphic(Optional)

EndR

Page 25: Integrate R into C/C++ APpS

This Slide and sample code only showsimplest functions, when you need moreadvance functions, the best way is reading theR source code(Using Source Insight to viewcode)

We also can use visual C++ write a dll ,so itcan be used by other language like( delphi ,visual basic ,c# ,etc.)

Page 26: Integrate R into C/C++ APpS

Rserve http://www.rforge.net/Rserve/

Rjava http://www.rforge.net/rJava/

R core team .Writing R Extensions

Page 27: Integrate R into C/C++ APpS