using visual c++ 2008 gong yu - 统计之都 · pdf filehow to integrate r into c/c++ app...
TRANSCRIPT
![Page 1: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/1.jpg)
Using Visual C++ 2008
GONG Yu
![Page 2: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/9.jpg)
Run CMD, enter the src\gnuwin32 dir
Type make ,then press enter key
![Page 10: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/11.jpg)
Run Visual Studio 2008 命令提示
![Page 12: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/13.jpg)
Load R:
![Page 14: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/14.jpg)
Create Object : we can create integer, real, string,bool vector or matrix.
Be carful with string encode !!!
![Page 15: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/15.jpg)
Define Var Name :
![Page 16: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/16.jpg)
Run R Code:
![Page 17: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/17.jpg)
Output Result:
![Page 18: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/18.jpg)
Put All together: main
.c
![Page 19: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/19.jpg)
RE
ng
ine.h
![Page 20: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/20.jpg)
Ren
gin
e.c
![Page 21: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/23.jpg)
Modify R MakefileCompile to
generate .def fileGenerate .lib file
Write own code using visual c++
![Page 24: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/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: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/26.jpg)
Rserve http://www.rforge.net/Rserve/
Rjava http://www.rforge.net/rJava/
R core team .Writing R Extensions
![Page 27: Using Visual C++ 2008 GONG Yu - 统计之都 · PDF fileHow to integrate R into c/c++ app using Visual C++ 2008 ... Run Visual Studio 2008 ... We also can use visual C++ write a dll](https://reader035.vdocuments.net/reader035/viewer/2022062907/5a9e3b2b7f8b9a75458c57c2/html5/thumbnails/27.jpg)