Writing R Extensions
Total Page:16
File Type:pdf, Size:1020Kb
Writing R Extensions Version 3.1.3 RC (2015-03-04) R Core Team This manual is for R, version 3.1.3 RC (2015-03-04). Copyright c 1999{2015 R Core Team Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into an- other language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the R Core Team. i Table of Contents Acknowledgements :::::::::::::::::::::::::::::::::::::::::::::::: 1 1 Creating R packages::::::::::::::::::::::::::::::::::::::::::: 2 1.1 Package structure ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 3 1.1.1 The DESCRIPTION file ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 4 1.1.2 Licensing ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7 1.1.3 Package Dependencies :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 9 1.1.3.1 Suggested packages ::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 1.1.4 The INDEX file ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 1.1.5 Package subdirectories ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 1.1.6 Data in packages :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 15 1.1.7 Non-R scripts in packages ::::::::::::::::::::::::::::::::::::::::::::::::::: 16 1.2 Configure and cleanup :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 16 1.2.1 Using Makevars ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 19 1.2.1.1 OpenMP support ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 22 1.2.1.2 Using pthreads ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 23 1.2.1.3 Compiling in sub-directories :::::::::::::::::::::::::::::::::::::::::::: 24 1.2.2 Configure example::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 24 1.2.3 Using F95 code :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 26 1.2.4 Using C++11 code :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 27 1.3 Checking and building packages :::::::::::::::::::::::::::::::::::::::::::::::::: 28 1.3.1 Checking packages ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 29 1.3.2 Building package tarballs :::::::::::::::::::::::::::::::::::::::::::::::::::: 32 1.3.3 Building binary packages :::::::::::::::::::::::::::::::::::::::::::::::::::: 33 1.4 Writing package vignettes :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 34 1.4.1 Encodings and vignettes ::::::::::::::::::::::::::::::::::::::::::::::::::::: 36 1.4.2 Non-Sweave vignettes:::::::::::::::::::::::::::::::::::::::::::::::::::::::: 37 1.5 Package namespaces :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 37 1.5.1 Specifying imports and exports :::::::::::::::::::::::::::::::::::::::::::::: 38 1.5.2 Registering S3 methods :::::::::::::::::::::::::::::::::::::::::::::::::::::: 39 1.5.3 Load hooks :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 39 1.5.4 useDynLib :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 40 1.5.5 An example ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 42 1.5.6 Namespaces with S4 classes and methods :::::::::::::::::::::::::::::::::::: 42 1.6 Writing portable packages :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 44 1.6.1 PDF size :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 47 1.6.2 Check timing :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 48 1.6.3 Encoding issues ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 48 1.6.4 Portable C and C++ code ::::::::::::::::::::::::::::::::::::::::::::::::::: 49 1.6.5 Binary distribution :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 50 1.7 Diagnostic messages :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 51 1.8 Internationalization :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 52 1.8.1 C-level messages ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 52 1.8.2 R messages :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 53 1.8.3 Preparing translations ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 53 1.9 CITATION files :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 53 1.10 Package types ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 54 1.10.1 Frontend ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 54 ii 1.11 Services ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 55 2 Writing R documentation files :::::::::::::::::::::::::::::: 56 2.1 Rd format:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 56 2.1.1 Documenting functions :::::::::::::::::::::::::::::::::::::::::::::::::::::: 57 2.1.2 Documenting data sets :::::::::::::::::::::::::::::::::::::::::::::::::::::: 61 2.1.3 Documenting S4 classes and methods :::::::::::::::::::::::::::::::::::::::: 62 2.1.4 Documenting packages::::::::::::::::::::::::::::::::::::::::::::::::::::::: 63 2.2 Sectioning :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 63 2.3 Marking text ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 63 2.4 Lists and tables :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 65 2.5 Cross-references :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 66 2.6 Mathematics ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 67 2.7 Figures::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 67 2.8 Insertions :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 68 2.9 Indices ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 68 2.10 Platform-specific documentation ::::::::::::::::::::::::::::::::::::::::::::::::: 69 2.11 Conditional text::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 69 2.12 Dynamic pages :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 69 2.13 User-defined macros ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 70 2.14 Encoding ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 71 2.15 Processing documentation files :::::::::::::::::::::::::::::::::::::::::::::::::: 71 2.16 Editing Rd files ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 72 3 Tidying and profiling R code :::::::::::::::::::::::::::::::: 73 3.1 Tidying R code::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 73 3.2 Profiling R code for speed :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 73 3.3 Profiling R code for memory use :::::::::::::::::::::::::::::::::::::::::::::::::: 75 3.3.1 Memory statistics from Rprof ::::::::::::::::::::::::::::::::::::::::::::::: 75 3.3.2 Tracking memory allocations::::::::::::::::::::::::::::::::::::::::::::::::: 76 3.3.3 Tracing copies of an object :::::::::::::::::::::::::::::::::::::::::::::::::: 76 3.4 Profiling compiled code ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 76 3.4.1 Linux ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 77 3.4.1.1 sprof ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 77 3.4.1.2 oprofile and operf ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 77 3.4.2 Solaris :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 80 3.4.3 OS X:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 80 4 Debugging :::::::::::::::::::::::::::::::::::::::::::::::::::: 81 4.1 Browsing ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 81 4.2 Debugging R code :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 82 4.3 Checking memory access ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 86 4.3.1 Using gctorture:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 86 4.3.2 Using valgrind ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 87 4.3.3 Using the Address Sanitizer:::::::::::::::::::::::::::::::::::::::::::::::::: 88 4.3.3.1 Using the Leak Sanitizer :::::::::::::::::::::::::::::::::::::::::::::::: 89 4.3.4 Using the Undefined Behaviour Sanitizer ::::::::::::::::::::::::::::::::::::: 89 4.3.5 Other analyses with `clang' :::::::::::::::::::::::::::::::::::::::::::::::::: 91 4.3.6 Using `Dr. Memory' ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 91 4.3.7 Fortran array bounds checking ::::::::::::::::::::::::::::::::::::::::::::::: 91 4.4 Debugging compiled code::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 91 4.4.1 Finding entry points in dynamically loaded code ::::::::::::::::::::::::::::: 93 4.4.2 Inspecting R objects when debugging :::::::::::::::::::::::::::::::::::::::: 93 iii 5 System and foreign language interfaces::::::::::::::::::::: 96 5.1 Operating system access :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 96 5.2 Interface functions .C and .Fortran :::::::::::::::::::::::::::::::::::::::::::::: 96 5.3 dyn.load and dyn.unload ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 98 5.4 Registering native routines