-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathR-exts.texi
18747 lines (15843 loc) · 724 KB
/
R-exts.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\input texinfo
@c %**start of header
@setfilename R-exts.info
@settitle Writing R Extensions
@documentencoding UTF-8
@defcodeindex ap
@defcodeindex ae
@defcodeindex em
@defcodeindex for
@c %**end of header
@c Put the functions in the variable index
@syncodeindex fn vr
@dircategory Programming
@direntry
* R Extensions: (R-exts). Writing R Extensions.
@end direntry
@finalout
@include R-defs.texi
@include version.texi
@comment move to R-defs.texi?
@macro apifun {f}
@apindex \f\
@findex \f\
@end macro
@macro apivar {f}
@apindex \f\
@vindex \f\
@end macro
@macro apihdr {f}
@apindex \f\
@end macro
@macro eapifun {f}
@aeindex \f\
@findex \f\
@end macro
@macro eapivar {f}
@aeindex \f\
@vindex \f\
@end macro
@macro eapihdr {f}
@aeindex \f\
@end macro
@macro embfun {f}
@emindex \f\
@findex \f\
@end macro
@macro embvar {f}
@emindex \f\
@vindex \f\
@end macro
@macro embhdr {f}
@emindex \f\
@end macro
@macro forfun {f}
@forindex \f\
@findex \f\
@end macro
@copying
This manual is for R, version @value{VERSION}.
@Rcopyright{1999}
@quotation
@permission{}
@end quotation
@end copying
@titlepage
@title Writing R Extensions
@subtitle Version @value{VERSION}
@author R Core Team
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@ifplaintext
@insertcopying
@end ifplaintext
@c @ifnothtml
@contents
@c @end ifnothtml
@ifnottex
@node Top
@top Writing R Extensions
This is a guide to extending @R{}, describing the process of creating
@R{} add-on packages, writing @R{} documentation, @R{}'s system and
foreign language interfaces, and the @R{} @acronym{API}.
@insertcopying
@end ifnottex
@node Acknowledgements
@unnumbered Acknowledgements
The contributions to early versions of this manual by @I{Saikat DebRoy}
(who wrote the first draft of a guide to using @code{.Call} and
@code{.External}) and @I{Adrian Trapletti} (who provided information on the
C++ interface) are gratefully acknowledged.
@node Creating R packages
@chapter Creating R packages
@cindex Packages
@cindex Creating packages
Packages provide a mechanism for loading optional code, data and
documentation as needed. The @R{} distribution itself includes about 30
packages.
In the following, we assume that you know the @code{library()} command,
including its @code{lib.loc} argument, and we also assume basic
knowledge of the @command{R CMD INSTALL} utility. Otherwise, please
look at @R{}'s help pages on
@example
?library
?INSTALL
@end example
@noindent
before reading on.
For packages which contain code to be compiled, a computing environment
including a number of tools is assumed; the ``R Installation and
Administration'' manual describes what is needed for each OS.
Once a source package is created, it must be installed by
the command @code{R CMD INSTALL}.
@xref{Add-on packages, , ,
R-admin, R Installation and Administration}.
Other types of extensions are supported (but rare): @xref{Package types}.
Some notes on terminology complete this introduction. These will help
with the reading of this manual, and also in describing concepts
accurately when asking for help.
A @emph{package} is a directory of files which extend @R{}, a
@emph{source package} (the master files of a package), or a tarball
containing the files of a source package, or an @emph{installed}
package, the result of running @command{R CMD INSTALL} on a source
package. On some platforms (notably macOS and @cputype{x86_64} Windows)
there are also @emph{binary packages}, a zip file or tarball containing
the files of an installed package which can be unpacked rather than
installing from sources.
A package is @strong{not}@footnote{although this is a persistent
@I{mis-usage}. It seems to stem from S, whose analogues of @R{}'s packages
were officially known as @emph{library sections} and later as
@emph{chapters}, but almost always referred to as @emph{libraries}.} a
@emph{library}. The latter is used in two senses in @R{} documentation.
@itemize
@item
A directory into which packages are installed, e.g.@:
@file{/usr/lib/R/library}: in that sense it is sometimes referred to as
a @emph{library directory} or @emph{library tree} (since the library is
a directory which contains packages as directories, which themselves
contain directories).
@item
That used by the operating system, as a shared, dynamic or static
library or (especially on Windows) a DLL, where the second L stands for
`library'. Installed packages may contain compiled code in what is
known on Unix-alikes as a @emph{shared object} and on Windows as a DLL.
The concept of a @emph{shared library} (@emph{dynamic library} on macOS)
as a collection of compiled code to which a package might link is also
used, especially for @R{} itself on some platforms. On most platforms
these concepts are interchangeable (shared objects and DLLs can both be
loaded into the @R{} process and be linked against), but macOS
distinguishes between shared objects (extension @file{.so}) and dynamic
libraries (extension @file{.dylib}).
@end itemize
There are a number of well-defined operations on source packages.
@itemize
@item
The most common is @emph{installation} which takes a source package and
installs it in a library using @command{R CMD INSTALL} or
@code{install.packages}.
@item
Source packages can be @emph{built}. This involves taking a source
directory and creating a tarball ready for distribution, including
cleaning it up and creating PDF/@HTML{} documentation from any @emph{vignettes}
it may contain. Source packages (and most often tarballs) can be
@emph{checked}, when a test installation is done and tested (including
running its examples); also, the contents of the package are tested in
various ways for consistency and portability.
@item
@emph{Compilation} is not a correct term for a package. Installing a
source package which contains C, C++ or Fortran code will involve
compiling that code. There is also the possibility of `byte' compiling
the @R{} code in a package (using the facilities of package
@pkg{compiler}): nowadays this is enabled by default for all
packages. So @emph{compiling} a package may come to mean byte-compiling
its @R{} code.
@item
It used to be unambiguous to talk about @emph{loading} an installed
package using @code{library()}, but since the advent of package
namespaces this has been less clear: people now often talk about
@emph{loading} the package's namespace and then @emph{attaching} the
package so it becomes visible on the search path. Function
@code{library} performs both steps, but a package's namespace can be
loaded without the package being attached (for example by calls like
@code{splines::ns}).
@end itemize
The concept of @emph{lazy loading} of code or data is mentioned at
several points. This is part of the installation, always selected for
@R{} code but optional for data. When used the @R{} objects of the
package are created at installation time and stored in a database in the
@file{R} directory of the installed package, being loaded into the
session at first use. This makes the @R{} session start up faster and
use less (virtual) memory.
(For technical details,
@pxref{Lazy loading, , , R-ints, R Internals}.)
@cindex CRAN
@acronym{CRAN} is a network of WWW sites holding the @R{} distributions
and contributed code, especially @R{} packages. Users of @R{} are
encouraged to join in the collaborative project and to submit their own
packages to @acronym{CRAN}: current instructions are linked from
@uref{https://CRAN.R-project.org/banner.shtml#submitting}.
@node Package structure
@section Package structure
@cindex Package structure
The sources of an @R{} package consist of a subdirectory containing the
files @file{DESCRIPTION} and @file{NAMESPACE}, and the subdirectories
@file{R}, @file{data}, @file{demo}, @file{exec}, @file{inst},
@file{man}, @file{po}, @file{src}, @file{tests}, @file{tools} and
@file{vignettes} (some of which can be missing, but which should not be
empty). The package subdirectory may also contain files @file{INDEX},
@file{configure}, @file{cleanup}, @file{LICENSE}, @file{LICENCE} and
@file{NEWS}. Other files such as @file{INSTALL} (for non-standard
installation instructions), @file{README}/@file{README.md}@footnote{This
seems to be commonly used for a file in `markdown' format. Be aware
that most users of @R{} will not know that, nor know how to view such a
file: platforms such as macOS and Windows do not have a default viewer
set in their file associations. The @acronym{CRAN} package web pages
render such files in @HTML{}: the converter used expects the file to be
encoded in UTF-8.}, or @file{ChangeLog} will be ignored by @R{}, but may
be useful to end users. The utility @command{R CMD build} may add files
in a @file{build} directory (but this should not be used for other
purposes).
Except where specifically mentioned,@footnote{currently, top-level files
@file{.Rbuildignore} and @file{.Rinstignore}, and
@file{vignettes/.install_extras}.} packages should not contain
Unix-style `hidden' files/directories (that is, those whose name starts
with a dot).
The @file{DESCRIPTION} and @file{INDEX} files are described in the
subsections below. The @file{NAMESPACE} file is described in the
section on @ref{Package namespaces}.
@cindex configure file
@cindex cleanup file
The optional files @file{configure} and @file{cleanup} are (@I{Bourne})
shell scripts which are, respectively, executed before and (if option
@option{--clean} was given) after installation on Unix-alikes, see
@ref{Configure and cleanup}. The analogues on Windows are
@file{configure.win} and @file{cleanup.win}. Since @R{} 4.2.0 on Windows,
@file{configure.ucrt} and @file{cleanup.ucrt} are supported and take
precedence over @file{configure.win} and @file{cleanup.win}. They can
hence be used to provide content specific to @abbr{UCRT} or @I{Rtools42} and newer, if needed,
but the support for @file{.ucrt} files may be removed in future when
building packages from source on the older versions of R will no longer
be needed, and hence the files may be renamed back to @file{.win}.
For the conventions for files @file{NEWS} and @file{ChangeLog} in the
@acronym{GNU} project see
@uref{https://www.gnu.org/prep/standards/standards.html#Documentation}.
The package subdirectory should be given the same name as the package.
Because some file systems (e.g., those on Windows and by default on
macOS) are not case-sensitive, to maintain portability it is strongly
recommended that case distinctions not be used to distinguish different
packages. For example, if you have a package named @file{foo}, do not
also create a package named @file{Foo}.
To ensure that file names are valid across file systems and supported
operating systems, the @acronym{ASCII} control characters as well as the
characters @samp{"}, @samp{*}, @samp{:}, @samp{/}, @samp{<}, @samp{>},
@samp{?}, @samp{\}, and @samp{|} are not allowed in file names. In
addition, files with names @samp{con}, @samp{prn}, @samp{aux},
@samp{clock$}, @samp{nul}, @samp{com1} to @samp{com9}, and @samp{lpt1}
to @samp{lpt9} after conversion to lower case and stripping possible
``extensions'' (e.g., @samp{lpt5.foo.bar}), are disallowed. Also, file
names in the same directory must not differ only by case (see the
previous paragraph). In addition, the basenames of @samp{.Rd} files may
be used in URLs and so must be @acronym{ASCII} and not contain @code{%}.
For maximal portability filenames should only contain only
@acronym{ASCII} characters not excluded already (that is
@code{A-Za-z0-9._!#$%&+,;=@@^()@{@}'[]} --- we exclude space as many
utilities do not accept spaces in file paths): non-English alphabetic
characters cannot be guaranteed to be supported in all locales. It
would be good practice to avoid the shell metacharacters
@code{()@{@}'[]$~}: @code{~} is also used as part of `8.3' filenames on
Windows. In addition, some applications on Windows can only work with path
names of certain length, following an earlier limit in the Windows operating
system. Packages are normally distributed as tarballs, and these have a limit
on path lengths. So, to be friendly to users who themselves may want to use a
relatively long path where they extract the package, and for maximal
portability, 100 bytes.
A source package if possible should not contain binary executable files:
they are not portable, and a security risk if they are of the
appropriate architecture. @command{R CMD check} will warn about
them@footnote{false positives are possible, but only a handful have been
seen so far.} unless they are listed (one filepath per line) in a file
@file{BinaryFiles} at the top level of the package. Note that
@acronym{CRAN} will not accept submissions containing binary files
even if they are listed.
The @R{} function @code{package.skeleton} can help to create the
structure for a new package: see its help page for details.
@node The DESCRIPTION file
@subsection The @file{DESCRIPTION} file
@cindex DESCRIPTION file
The @file{DESCRIPTION} file contains basic information about the package
in the following format:
@quotation
@cartouche
@smallexample
Package: pkgname
Version: 0.5-1
Date: 2015-01-01
Title: My First Collection of Functions
Authors@@R: c(person("Joe", "Developer", role = c("aut", "cre"),
email = "Joe.Developer@@some.domain.net",
comment = c(ORCID = "nnnn-nnnn-nnnn-nnnn")),
person("Pat", "Developer", role = "aut"),
person("A.", "User", role = "ctb",
email = "A.User@@whereever.net"))
Author: Joe Developer [aut, cre],
Pat Developer [aut],
A. User [ctb]
Maintainer: Joe Developer <Joe.Developer@@some.domain.net>
Depends: R (>= 3.1.0), nlme
Suggests: MASS
Description: A (one paragraph) description of what
the package does and why it may be useful.
License: GPL (>= 2)
URL: https://www.r-project.org, http://www.another.url
BugReports: https://pkgname.bugtracker.url
@end smallexample
@end cartouche
@end quotation
@noindent
The format is that of a version of a `Debian Control File' (see the help
for @samp{read.dcf} and
@uref{https://www.debian.org/doc/debian-policy/ch-controlfields.html}:
@R{} does not require encoding in UTF-8 and does not support comments
starting with @samp{#}). Fields start with an @acronym{ASCII} name
immediately followed by a colon: the value starts after the colon and a
space. Continuation lines (for example, for descriptions longer than
one line) start with a space or tab. Field names are case-sensitive:
all those used by @R{} are capitalized.
For maximal portability, the @file{DESCRIPTION} file should be written
entirely in @acronym{ASCII} --- if this is not possible it must contain
an @samp{Encoding} field (see below).
Several optional fields take @emph{logical values}: these can be
specified as @samp{yes}, @samp{true}, @samp{no} or @samp{false}:
capitalized values are also accepted.
The @samp{Package}, @samp{Version}, @samp{License}, @samp{Description},
@samp{Title}, @samp{Author}, and @samp{Maintainer} fields are mandatory,
all other fields are optional. Fields @samp{Author} and
@samp{Maintainer} can be auto-generated from @samp{Authors@@R}, and may
be omitted if the latter is provided: however if they are not
@acronym{ASCII} we recommend that they are provided.
@c DESCRIPTION field Package
The mandatory @samp{Package} field gives the name of the package. This
should contain only (@acronym{ASCII}) letters, numbers and dot, have at
least two characters and start with a letter and not end in a dot. If
it needs explaining, this should be done in the @samp{Description} field
(and not the @samp{Title} field).
@c DESCRIPTION field Version
The mandatory @samp{Version} field gives the version of the package.
This is a sequence of at least @emph{two} (and usually three)
non-negative integers separated by single @samp{.} or @samp{-}
characters. The canonical form is as shown in the example, and a
version such as @samp{0.01} or @samp{0.01.0} will be handled as if it
were @samp{0.1-0}. It is @strong{not} a decimal number, so for example
@code{0.9 < 0.75} since @code{9 < 75}.
The mandatory @samp{License} field is discussed in the next subsection.
@c DESCRIPTION field Title
The mandatory @samp{Title} field should give a @emph{short} description
of the package. Some package listings may truncate the title to 65
characters. It should use @emph{title case} (that is, use capitals for
the principal words: @code{tools::toTitleCase} can help you with this),
not use any markup, not have any continuation lines, and not end in a
period (unless part of @dots{}). Do not repeat the package name: it is
often used prefixed by the name. Refer to other packages and external
software in single quotes, and to book titles (and similar) in double
quotes.
@c DESCRIPTION field Description
The mandatory @samp{Description} field should give a
@emph{comprehensive} description of what the package does. One can use
several (complete) sentences, but only one paragraph. It should be
intelligible to all the intended readership (e.g.@: for a @acronym{CRAN}
package to all @acronym{CRAN} users). It is good practice not to start
with the package name, `This package' or similar. As with the
@samp{Title} field, double quotes should be used for quotations
(including titles of books and articles), and single quotes for
non-English usage, including names of other packages and external
software. This field should also be used for explaining the package
name if necessary. URLs should be enclosed in angle brackets, e.g.@:
@samp{<https://www.r-project.org>}: see also @ref{Specifying URLs}.
@c DESCRIPTION field Author
@c DESCRIPTION field Authors@R
The mandatory @samp{Author} field describes who wrote @emph{the
package}. It is a plain text field intended for human readers, but not
for automatic processing (such as extracting the email addresses of all
listed contributors: for that use @samp{Authors@@R}). Note that all
significant contributors must be included: if you wrote an @R{} wrapper
for the work of others included in the @file{src} directory, you are not
the sole (and maybe not even the main) author.
@c DESCRIPTION field Maintainer
The mandatory @samp{Maintainer} field should give a @emph{single} name
followed by a @emph{valid} (RFC 2822) email address in angle brackets. It
should not end in a period or comma. This field is what is reported by
the @code{maintainer} function and used by @code{bug.report}. For a
@acronym{CRAN} package it should be a @emph{person}, not a mailing list
and not a corporate entity: do ensure that it is valid and will remain
valid for the lifetime of the package.
Note that the @emph{display name} (the part before the address in angle
brackets) should be enclosed in double quotes if it contains
non-alphanumeric characters such as comma or period. (The current
standard, RFC 5322, allows periods but RFC 2822 did not.)
Both @samp{Author} and @samp{Maintainer} fields can be omitted if a
suitable @samp{Authors@@R} field is given. This field can be used to
provide a refined and machine-readable description of the package
``authors'' (in particular specifying their precise @emph{roles}),
@emph{via} suitable @R{} code. It should create an object of class
@code{"person"}, by either a call to @code{person} or a series of calls
(one per ``author'') concatenated by @code{c()}: see the example
@file{DESCRIPTION} file above. The roles can include @samp{"aut"}
(author) for full authors, @samp{"cre"} (creator) for the package
maintainer, and @samp{"ctb"} (contributor) for other contributors,
@samp{"cph"} (copyright holder, which should be the legal name for an
institution or corporate body), among others. See @code{?person} for
more information. Note that no role is assumed by default.
Auto-generated package citation information takes advantage of this
specification. The @samp{Author} and @samp{Maintainer} fields are
auto-generated from it if needed when building@footnote{at least if this
is done in a locale which matches the package encoding.} or installing.
Note that for CRAN submissions, providing @samp{Authors@@R} is required,
and providing @abbr{ORCID} or @abbr{ROR} identifiers (see
@url{https://orcid.org/} and @url{https://ror.org/}) where possible is
strongly encouraged.
@findex COPYRIGHTS
@c DESCRIPTION field Copyright
An optional @samp{Copyright} field can be used where the copyright
holder(s) are not the authors. If necessary, this can refer to an
installed file: the convention is to use file @file{inst/COPYRIGHTS}.
@c DESCRIPTION field Date
The optional @samp{Date} field gives the @emph{release date} of the
current version of the package. It is strongly recommended@footnote{and
required by @acronym{CRAN}, so checked by @command{R CMD check
--as-cran}.} to use the @samp{yyyy-mm-dd} format conforming to the ISO
8601 standard.
The @samp{Depends}, @samp{Imports}, @samp{Suggests}, @samp{Enhances},
@samp{LinkingTo} and @samp{Additional_repositories} fields are discussed
in a later subsection.
@c DESCRIPTION field SystemRequirements
Dependencies external to the @R{} system should be listed in the
@samp{SystemRequirements} field, possibly amplified in a separate
@file{README} file. This includes specifying a non-default C++ standard
and the need for GNU @command{make}.
@c DESCRIPTION field URL
The @samp{URL} field may give a list of @acronym{URL}s
separated by commas or whitespace, for example the homepage of the
author or a page where additional material describing the software can
be found. These @acronym{URL}s are converted to active hyperlinks in
@acronym{CRAN} package listings. @xref{Specifying URLs}.
@c DESCRIPTION field BugReports
The @samp{BugReports} field may contain a single @acronym{URL} to which
bug reports about the package should be submitted. This @acronym{URL}
will be used by @code{bug.report} instead of sending an email to the
maintainer. A browser is opened for a @samp{http://} or @samp{https://}
@acronym{URL}. To specify another email address for bug reports, use
@samp{Contact} instead: however @code{bug.report} will try to extract an
email address (preferably from a @samp{mailto:} URL or enclosed in angle
brackets) from @samp{BugReports}.
@c DESCRIPTION field Priority
Base and recommended packages (i.e., packages contained in the @R{}
source distribution or available from @acronym{CRAN} and recommended to
be included in every binary distribution of @R{}) have a @samp{Priority}
field with value @samp{base} or @samp{recommended}, respectively. These
priorities must not be used by other packages.
@c DESCRIPTION field Collate
@c DESCRIPTION field Collate.unix
@c DESCRIPTION field Collate.windows
A @samp{Collate} field can be used for controlling the collation order
for the @R{} code files in a package when these are processed for
package installation. The default is to collate according to the
@samp{C} locale. If present, the collate specification must list
@emph{all} @R{} code files in the package (taking possible OS-specific
subdirectories into account, see @ref{Package subdirectories}) as a
whitespace separated list of file paths relative to the @file{R}
subdirectory.
@c % double quotes are not allowed in path names, for Windows
Paths containing white space or quotes need to be quoted. An
OS-specific collation field (@samp{Collate.unix} or
@samp{Collate.windows}) will be used in preference to @samp{Collate}.
@c DESCRIPTION field LazyData
@c DESCRIPTION field LazyLoad
The @samp{LazyData} logical field controls whether the @R{} datasets use
lazy-loading. A @samp{LazyLoad} field was used in versions prior to
2.14.0, but now is ignored.
@c DESCRIPTION field KeepSource
The @samp{KeepSource} logical field controls if the package code is sourced
using @code{keep.source = TRUE} or @code{FALSE}: it might be needed
exceptionally for a package designed to always be used with
@code{keep.source = TRUE}.
@c DESCRIPTION field ByteCompile
The @samp{ByteCompile} logical field controls if the package R code is to
be byte-compiled on installation: the default is to byte-compile. This
can be overridden by installing with flag @option{--no-byte-compile}.
@c DESCRIPTION field UseLTO
The @samp{UseLTO} logical field is used to indicate if source code in
the package@footnote{without a @file{src/Makefile*} file.} is to be
compiled with Link-Time Optimization (@pxref{Using Link-time
Optimization}) if @R{} was installed with @option{--enable-lto} (default
true) or @option{--enable-lto=R} (default false) (or on
Windows@footnote{@abbr{LTO} is not currently supported by the toolchain
used on @cputype{aarch64}.} if
@code{LTO_OPT} is set in @file{MkRules}). This can be overridden by the
flags @option{--use-LTO} and @option{--no-use-LTO}. @abbr{LTO} is said to give
most size and performance improvements for large and complex (heavily
templated) C++ projects.
@c DESCRIPTION field StagedInstall
The @samp{StagedInstall} logical field controls if package installation
is `staged', that is done to a temporary location and moved to the final
location when successfully completed. This field was introduced in @R{}
3.6.0 and is true by default: it is considered to be a temporary measure
which may be withdrawn in future.
@c DESCRIPTION field ZipData
The @samp{ZipData} logical field has been ignored since @R{} 2.13.0.
@c DESCRIPTION field Biarch
The @samp{Biarch} logical field is used on Windows to select the
@command{INSTALL} option @option{--force-biarch} for this package. Not
currently relevant.
@c DESCRIPTION field BuildVignettes
The @samp{BuildVignettes} logical field can be set to a false value to
stop @command{R CMD build} from attempting to build the vignettes, as
well as preventing@footnote{But it is checked for Open Source packages
by @command{R CMD check --as-cran}.} @command{R CMD check} from testing
this. This should only be used exceptionally, for example if the PDFs
include large figures which are not part of the package sources (and
hence only in packages which do not have an Open Source license).
@c DESCRIPTION field VignetteBuilder
The @samp{VignetteBuilder} field names (in a comma-separated list)
packages that provide an engine for building vignettes. These may
include the current package, or ones listed in @samp{Depends},
@samp{Suggests} or @samp{Imports}. The @pkg{utils} package is always
implicitly appended. See @ref{Non-Sweave vignettes} for details. Note
that if, for example, a vignette has engine @samp{knitr::rmarkdown},
then @CRANpkg{knitr} provides the engine but both @pkg{knitr} and
@CRANpkg{rmarkdown} are needed for using it, so @emph{both} these
packages need to be in the @samp{VignetteBuilder} field and at least
suggested (as @pkg{rmarkdown} is only suggested by @pkg{knitr}, and
hence not available automatically along with it). Many packages using
@CRANpkg{knitr} also need the package @CRANpkg{formatR} which it
suggests and so the user package needs to do so too and include this in
@samp{VignetteBuilder}.
@c This included latin2 but we now know musl does not support it.
@c DESCRIPTION field Encoding
If the @file{DESCRIPTION} file is not entirely in @acronym{ASCII} it
should contain an @samp{Encoding} field specifying an encoding. This is
used as the encoding of the @file{DESCRIPTION} file itself and of the
@file{R} and @file{NAMESPACE} files, and as the default encoding of
@file{.Rd} files. The examples are assumed to be in this encoding when
running @command{R CMD check}, and it is used for the encoding of the
@code{CITATION} file. Only encoding names @code{latin1} and
and @code{UTF-8} are known to be portable. (Do not specify an encoding
unless one is actually needed: doing so makes the package @emph{less}
portable. If a package has a specified encoding, you should run
@command{R CMD build} etc in a locale using that encoding.)
@c DESCRIPTION NeedsCompilation
The @samp{NeedsCompilation} field should be set to @code{"yes"} if the
package contains native code which needs to be compiled, otherwise @code{"no"} (when
the package could be installed from source on any platform without
additional tools). This is used by @code{install.packages(type =
"both")} in @R{} >= 2.15.2 on platforms where binary packages are the
norm: it is normally set by @command{R CMD build} or the repository
assuming compilation is required if and only if the package has a
@file{src} directory.
@c DESCRIPTION field OS_type
The @samp{OS_type} field specifies the OS(es) for which the
package is intended. If present, it should be one of @code{unix} or
@code{windows}, and indicates that the package can only be installed
on a platform with @samp{.Platform$OS.type} having that value.
@c DESCRIPTION field Type
The @samp{Type} field specifies the type of the package:
@pxref{Package types}.
@c DESCRIPTION field Classification/ACM
@c DESCRIPTION field Classification/ACM-2012
@c DESCRIPTION field Classification/JEL
@c DESCRIPTION field Classification/MSC
@c DESCRIPTION field Classification/MSC-2010
One can add subject classifications for the content of the package using
the fields @samp{Classification/ACM} or @samp{Classification/ACM-2012}
(using the Computing Classification System of the Association for
Computing Machinery, @uref{https://www.acm.org/publications/class-2012}; the former refers
to the 1998 version), @samp{Classification/JEL} (the Journal of Economic
Literature Classification System,
@uref{https://www.aeaweb.org/econlit/jelCodes.php}, or
@samp{Classification/MSC} or @samp{Classification/MSC-2010} (the
Mathematics Subject Classification of the American Mathematical Society,
@uref{https://mathscinet.ams.org/msc/msc2010.html}; the former refers to the 2000 version).
The subject classifications should be comma-separated lists of the
respective classification codes, e.g., @samp{Classification/ACM: G.4,
H.2.8, I.5.1}.
@c DESCRIPTION field Language
A @samp{Language} field can be used to indicate if the package
documentation is not in English: this should be a comma-separated list
of standard (not private use or grandfathered) @abbr{IETF} language tags as
currently defined by RFC 5646
(@uref{https://www.rfc-editor.org/rfc/rfc5646}, see also
@uref{https://en.wikipedia.org/wiki/IETF_language_tag}), i.e., use
language subtags which in essence are 2-letter ISO 639-1
(@uref{https://en.wikipedia.@/org/wiki/ISO_639-1}) or 3-letter ISO
639-3 (@uref{https://en.wikipedia.@/org/wiki/ISO_639-3}) language
codes.
@c DESCRIPTION field RdMacros
An @samp{RdMacros} field can be used to hold a comma-separated list of
packages from which the current package will import @file{Rd} macro
definitions. These package should also be listed in @samp{Imports}
(or @samp{Depends}). The macros in these packages will be
imported after the system macros, in the
order listed in the @samp{RdMacros} field, before any macro definitions
in the current package are loaded. Macro definitions in individual
@file{.Rd} files in the @file{man} directory are loaded last, and are
local to later parts of that file. In case of duplicates, the last
loaded definition will be used.@footnote{Duplicate definitions may
trigger a warning: see @ref{User-defined macros}.} Both @command{R CMD
Rd2pdf} and @command{R CMD Rdconv} have an optional flag
@option{--RdMacros=pkglist}. The option is also a comma-separated list
of package names, and has priority over the value given in
@file{DESCRIPTION}. Packages using @file{Rd} macros should depend on
@R{} 3.2.0 or later.
@c DESCRIPTION field Built
@c DESCRIPTION field Packaged
@quotation Note
There should be no @samp{Built} or @samp{Packaged} fields, as these are
added by the package management tools.
@end quotation
@c DESCRIPTION field Note
@c DESCRIPTION field Contact
@c DESCRIPTION field MailingList
There is no restriction on the use of other fields not mentioned here
(but using other capitalizations of these field names would cause
confusion). Fields @code{Note}, @code{Contact} (for contacting the
authors/developers@footnote{@code{bug.report} will try to extract an
email address from a @code{Contact} field if there is no
@code{BugReports} field.}) and @code{MailingList} are in common
use. Some repositories (including @acronym{CRAN} and R-forge) add their
own fields.
@node Licensing
@subsection Licensing
Licensing for a package which might be distributed is an important but
potentially complex subject.
It is very important that you include license information! Otherwise,
it may not even be legally correct for others to distribute copies of
the package, let alone use it.
The package management tools use the concept of
`free or open source software'
(@abbr{FOSS}, e.g., @uref{https://en.wikipedia.org/wiki/FOSS})
licenses: the idea being that some users of @R{} and its packages want
to restrict themselves to such software. Others need to ensure that
there are no restrictions stopping them using a package, e.g.@:
forbidding commercial or military use. It is a central tenet of @abbr{FOSS}
software that there are no restrictions on users nor usage.
Do not use the @samp{License} field for information on copyright
holders: if needed, use a @samp{Copyright} field.
@c DESCRIPTION field License
@c DESCRIPTION field License_is_FOSS
@c DESCRIPTION field License_restricts_use
The mandatory @samp{License} field in the @file{DESCRIPTION} file should
specify the license of the package in a standardized form. Alternatives
are indicated @emph{via} vertical bars. Individual specifications must
be one of
@itemize @bullet
@item
One of the ``standard'' short specifications
@example
GPL-2 GPL-3 LGPL-2 LGPL-2.1 LGPL-3 AGPL-3 Artistic-2.0
BSD_2_clause BSD_3_clause MIT
@end example
@noindent
as made available @emph{via} @uref{https://www.R-project.org/Licenses/} and
contained in subdirectory @file{share/licenses} of the @R{} source or home
directory.
@item
The names or abbreviations of other licenses contained in the license
data base in file @file{share/licenses/license.db} in the @R{} source or
home directory, possibly (for versioned licenses) followed by a version
restriction of the form @samp{(@var{op} @var{v})} with @samp{@var{op}} one of
the comparison operators @samp{<}, @samp{<=}, @samp{>}, @samp{>=},
@samp{==}, or @samp{!=} and @samp{@var{v}} a numeric version specification
(strings of non-negative integers separated by @samp{.}), possibly
combined @emph{via} @samp{,} (see below for an example). For versioned
licenses, one can also specify the name followed by the version, or
combine an existing abbreviation and the version with a @samp{-}.
Abbreviations @code{GPL} and @code{LGPL} are ambiguous and
usually@footnote{@acronym{CRAN} expands them to e.g.@: @code{GPL-2
| GPL-3}.} taken to mean any version of the license: but it is better
not to use them.
@item
One of the strings @samp{file LICENSE} or @samp{file LICENCE} referring
to a file named @file{LICENSE} or @file{LICENCE} in the package (source
and installation) top-level directory.
@item
The string @samp{Unlimited}, meaning that there are no restrictions on
distribution or use other than those imposed by relevant laws (including
copyright laws).
@end itemize
@noindent
Multiple licences can be specified separated by @samp{|} (surrounded by
spaces) in which case the user can choose any of the alternatives.
If a package license @emph{restricts} a base license (where permitted,
e.g., using GPL-3 or @I{AGPL-3} with an attribution clause), the additional
terms should be placed in file @file{LICENSE} (or @file{LICENCE}), and
the string @samp{+ file LICENSE} (or @samp{+ file LICENCE},
respectively) should be appended to the corresponding individual license
specification (preferably with the @samp{+} surrounded by spaces). Note
that several commonly used licenses do not permit restrictions: this
includes GPL-2 and hence any specification which includes it.
Examples of standardized specifications include
@example
License: GPL-2
License: LGPL (>= 2.0, < 3) | Mozilla Public License
License: GPL-2 | file LICENCE
License: GPL (>= 2) | BSD_3_clause + file LICENSE
License: Artistic-2.0 | AGPL-3 + file LICENSE
@end example
@noindent
Please note in particular that ``Public domain'' is not a valid license,
since it is not recognized in some jurisdictions.
Please ensure that the license you choose also covers any dependencies
(including system dependencies) of your package: it is particularly
important that any restrictions on the use of such dependencies are
evident to people reading your @file{DESCRIPTION} file.
Fields @samp{License_is_FOSS} and @samp{License_restricts_use} may be
added by repositories where information cannot be computed from the name
of the license. @samp{License_is_FOSS: yes} is used for licenses which
are known to be @abbr{FOSS}, and @samp{License_restricts_use} can have values
@samp{yes} or @samp{no} if the @file{LICENSE} file is known to restrict
users or usage, or known not to. These are used by, e.g.@:, the
@code{available.packages} filters.
@cindex LICENSE file
@cindex LICENCE file
The optional file @file{LICENSE}/@file{LICENCE} contains a copy of the
license of the package. To avoid any confusion only include such a file
if it is referred to in the @samp{License} field of the
@file{DESCRIPTION} file.
Whereas you should feel free to include a license file in your
@emph{source} distribution, please do not arrange to @emph{install} yet
another copy of the @acronym{GNU} @file{COPYING} or @file{COPYING.LIB}
files but refer to the copies on
@uref{https://www.R-project.org/Licenses/} and included in the @R{}
distribution (in directory @file{share/licenses}). Since files named
@file{LICENSE} or @file{LICENCE} @emph{will} be installed, do not use
these names for standard license files. To include comments about the
licensing rather than the body of a license, use a file named something
like @file{LICENSE.note}.
A few ``standard'' licenses are rather license templates which need
additional information to be completed @emph{via} @samp{+ file LICENSE}
(with the @samp{+} surrounded by spaces). Where the additional
information is @samp{COPYRIGHT HOLDER}, this must give the actual legal
entities (not something vague like `Name-of-package authors'): if more
than one they should be listed in decreasing order of contribution.
@node Package Dependencies
@subsection Package Dependencies
@c DESCRIPTION field Depends
The @samp{Depends} field gives a comma-separated list of package names
which this package depends on. Those packages will be attached before
the current package when @code{library} or @code{require} is called.
Each package name may be optionally followed by a comment in parentheses
specifying a version requirement. The comment should contain a
comparison operator, whitespace and a valid version number,
e.g.@: @samp{MASS (>= 3.1-20)}.
The @samp{Depends} field can also specify a dependence on a certain
version of @R{} --- e.g., if the package works only with @R{} version
4.0.0 or later, include @samp{R (>= 4.0)} in the @samp{Depends}
field. (As here, trailing zeroes can be dropped and it is recommended
that they are.) You can also require a certain SVN revision for R-devel
or R-patched, e.g.@: @samp{R (>= 2.14.0), R (>= r56550)} requires a
version later than R-devel of late July 2011 (including released
versions of 2.14.0).
It makes no sense to declare a dependence on @code{R} without a version
specification, nor on the package @pkg{base}: this is an @R{} package
and package @pkg{base} is always available.
A package or @samp{R} can appear more than once in the @samp{Depends}
field, for example to give upper and lower bounds on acceptable
versions.
It is inadvisable to use a dependence on @R{} with patch level (the third
digit) other than zero. Doing so with packages which others depend on
will cause the other packages to become unusable under earlier versions
in the series, and e.g.@: versions 4.x.1 are widely used throughout the
Northern Hemisphere academic year.
Both @code{library} and the @R{} package checking facilities use this
field: hence it is an error to use improper syntax or misuse the
@samp{Depends} field for comments on other software that might be
needed. The @R{} @command{INSTALL} facilities check if the version of
@R{} used is recent enough for the package being installed, and the list
of packages which is specified will be attached (after checking version
requirements) before the current package.
@c DESCRIPTION field Imports
The @samp{Imports} field lists packages whose namespaces are imported
from (as specified in the @file{NAMESPACE} file) but which do not need
to be attached. Namespaces accessed by the @samp{::} and @samp{:::}
operators must be listed here, or in @samp{Suggests} or @samp{Enhances}
(see below). Ideally this field will include all the standard packages
that are used, and it is important to include S4-using packages (as
their class definitions can change and the @file{DESCRIPTION} file is
used to decide which packages to re-install when this happens).
Packages declared in the @samp{Depends} field should not also be in the
@samp{Imports} field. Version requirements can be specified and are
checked when the namespace is loaded.
@c DESCRIPTION field Suggests
The @samp{Suggests} field uses the same syntax as @samp{Depends} and
lists packages that are not necessarily needed. This includes packages
used only in examples, tests or vignettes (@pxref{Writing package
vignettes}), and packages loaded in the body of functions. E.g.,
suppose an example@footnote{even one wrapped in @code{\donttest}.} from
package @pkg{foo} uses a dataset from package @pkg{bar}. Then it is not
necessary to have @pkg{bar} use @pkg{foo} unless one wants to execute
all the examples/tests/vignettes: it is useful to have @pkg{bar}, but
not necessary. Version requirements can be specified but should be
checked by the code which uses the package.
@c DESCRIPTION field Enhances
Finally, the @samp{Enhances} field lists packages ``enhanced'' by the
package at hand, e.g., by providing methods for classes from these
packages, or ways to handle objects from these packages (so several
packages have @samp{Enhances: chron} because they can handle datetime
objects from @CRANpkg{chron} even though they prefer @R{}'s native
datetime functions). Version requirements can be specified, but are
currently not used. Such packages cannot be required to check the
package: any tests which use them must be conditional on the presence
of the package. (If your tests use e.g.@: a dataset from another
package it should be in @samp{Suggests} and not @samp{Enhances}.)
The general rules are
@itemize @bullet
@item
A package should be listed in only one of these fields.
@item
Packages whose namespace only is needed to load the package using
@code{library(@var{pkgname})} should be listed in the @samp{Imports} field
and not in the @samp{Depends} field. Packages listed in @code{import}
or @code{importFrom} directives in the @file{NAMESPACE} file should
almost always be in @samp{Imports} and not @samp{Depends}.
@item
Packages that need to be attached to successfully load the package using
@code{library(@var{pkgname})} must be listed in the @samp{Depends}
field.
@item
All packages that are needed@footnote{This includes all packages
directly called by @code{library} and @code{require} calls, as well as
data obtained @emph{via} @code{data(theirdata, package = "somepkg")}
calls: @command{R CMD check} will warn about all of these. But there
are subtler uses which it may not detect: e.g.@: if package A uses
package B and makes use of functionality in package B which uses package
C which package B suggests or enhances, then package C needs to be in
the @samp{Suggests} list for package A. Nor will undeclared uses in
included files be reported, nor unconditional uses of packages listed
under @samp{Enhances}. @command{R CMD check --as-cran} will detect more
of the subtler uses.} to successfully run @code{R CMD check} on the
package must be listed in one of @samp{Depends} or @samp{Suggests} or
@samp{Imports}. Packages used to run examples or tests conditionally
(e.g.@: @emph{via} @code{if(require(@var{pkgname}))}) should be listed
in @samp{Suggests} or @samp{Enhances}. (This allows checkers to ensure
that all the packages needed for a complete check are installed.)
@item
Packages needed to use datasets from the package should be in
@samp{Imports}: this includes those needed to define S4 classes used.
@end itemize
@noindent
In particular, packages providing ``only'' data for examples or
vignettes should be listed in @samp{Suggests} rather than @samp{Depends}
in order to make lean installations possible.
Version dependencies in the @samp{Depends} and @samp{Imports} fields are
used by @code{library} when it loads the package, and
@code{install.packages} checks versions for the @samp{Depends},
@samp{Imports} and (for @code{dependencies = TRUE}) @samp{Suggests}
fields.
It is important that the information in these fields is complete and
accurate: it is for example used to compute which packages depend on an
updated package and which packages can safely be installed in parallel.
This scheme was developed before all packages had namespaces (@R{}
2.14.0 in October 2011), and good practice changed once that was in
place.