integrate r into c/c++ apps
TRANSCRIPT
![Page 1: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/1.jpg)
Using Visual C++ 2008
GONG Yu
![Page 2: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/2.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/3.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/4.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/5.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/6.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/7.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/8.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/9.jpg)
Run CMD, enter the src\gnuwin32 dir
Type make ,then press enter key
![Page 10: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/10.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/11.jpg)
Run Visual Studio 2008 命令提示
![Page 12: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/12.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/13.jpg)
Load R:
![Page 14: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/14.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/15.jpg)
Define Var Name :
![Page 16: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/16.jpg)
Run R Code:
![Page 17: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/17.jpg)
Output Result:
![Page 18: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/18.jpg)
Put All together: main
.c
![Page 19: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/19.jpg)
RE
ng
ine.h
![Page 20: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/20.jpg)
Ren
gin
e.c
![Page 21: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/21.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/22.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/23.jpg)
Modify R MakefileCompile to
generate .def fileGenerate .lib file
Write own code using visual c++
![Page 24: Integrate R into C/C++ APpS](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/24.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/25.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/26.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022022418/58a2d1011a28ab724d8b6620/html5/thumbnails/27.jpg)