R call C Using DLL in R ver

  • Slides: 6
Download presentation
R call C Using DLL in R (ver. 1. 8. 1) T. B. Chen

R call C Using DLL in R (ver. 1. 8. 1) T. B. Chen in NCTU 10/29/2020

Flow chart show how-to Prepare : 1. mylib. c and 2. Mylib. def VC

Flow chart show how-to Prepare : 1. mylib. c and 2. Mylib. def VC ++ complier build mylib. dll Copy mylib. dll to. . Rrw 1081 1. Open R 2. Using dyn. load(“mylib. dll", " Function name”, ”cdecl”) 3. Building a sub-function in R by using. C(“function-name”, arguments) Done 10/29/2020

Building a DLL by using Visual C++ To build the DLL, use Visual C++

Building a DLL by using Visual C++ To build the DLL, use Visual C++ as follows: 1. From Microsoft Developer Studio, select File, then New. The New dialog appears with a scrolled list of options. 2. Select Project Workspace from the New dialog. The New Project Workspace dialog appears. 3. Enter the project name “AR” in the Name text field, then select Dynamic Link Library in the Project Type list (not MFCApp. Wizard(dll)). If you want to specify a particular location for the project workspace, you can do that in this dialog as well. 4. Click Create. A new project workspace is created in the directory you specified. 5. From the Insert menu, choose Files into Project. 6. Use the Insert Files into Project dialog to select the source code file Ar. c and the module definition file Ar. def. To view. def files, use the Files of Type dropdown. 7. Select Build, then Settings, to bring up the Project Settings dialog. Choose the C/C++ tab. 8. Add the definition DLL_LOAD to the Preprocessor Definitions text field in the Project Settings dialog. 9. Select Project and then Rebuild All from the menu bar. This builds the DLL and creates numerous files, including the DLL file, Ar. dll. 10. Copy Ar. dll to …/R/rw 1081/ 10/29/2020

Ar. c & Ar. def • Ar. c void arsim(double *x, long *n, double

Ar. c & Ar. def • Ar. c void arsim(double *x, long *n, double *phi) { long i; for (i=1; i<*n; i++) x[i] = *phi * x[i-1] + x[i] ; } • Ar. def ; ********************* ; ar. def module definition file ; ********************* LIBRARY ar EXPORTS arsim 10/29/2020

An example show R calling a DLL > dyn. load("ar. dll", "arsim", "cdecl") Don’t

An example show R calling a DLL > dyn. load("ar. dll", "arsim", "cdecl") Don’t worry about those NULL warning message! Warning message: DLL attempted to change FPU control word from 8001 f to 9001 f > Test<-function(x, a, b). C("arsim", as. double(x), as. integer(a), as. double(b)) > x<-rnorm(10) > Test(x, 10, 0. 7)[[1]] [1] 1. 7886214 0. 9355408 2. 2388264 1. 9190996 1. 5269978 2. 5275526 2. 8187632 [8] 1. 7084246 1. 1144688 0. 3400129 >x [1] 1. 78862144 -0. 31649423 1. 58394788 0. 35192107 0. 18362813 1. 45865408 [7] 1. 04947645 -0. 26470966 -0. 08142846 -0. 44011526 10/29/2020

Syntax illustration > dyn. load("ar. dll", "arsim", "cdecl") dyn. load : Dynamic routine in

Syntax illustration > dyn. load("ar. dll", "arsim", "cdecl") dyn. load : Dynamic routine in order to load a DLL. Argument: 1. Name of DLL 2. Function name in DLL 3. The calling convention "cdecl" in the call to dyn. load is the default calling convention generated by Visual C++. >Test<-function(x, a, b). C("arsim", as. double(x), as. integer(a), as. double(b)) Test<-function(x, a, b) : Building a function use the sub-routine of ar. dll. . C("arsim", as. double(x), as. integer(a), as. double(b)). C( “name of sub-routine in ar. dll”, 1 st argument data type (var 1), 2 nd argument data type (var 2), 3 rd argument data type (var 3) ) 10/29/2020