Skip to content

Commit 0c4353f

Browse files
author
hornik
committed
Experiment with auto-generating citations and references in Rd files.
git-svn-id: https://svn.r-project.org/R/trunk@88462 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 3bde0ee commit 0c4353f

File tree

7 files changed

+186
-40
lines changed

7 files changed

+186
-40
lines changed

share/Rd/macros/system.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,7 @@
5858

5959
% Experimental:
6060
\newcommand{\bibcite}{#1}
61-
\newcommand{\bibshow}{#1}
61+
% \newcommand{\bibshow}{#1}
62+
\newcommand{\bibcitep}{\Sexpr[results=rd,stage=build]{tools:::Rd_expr_bibcite("#1", FALSE)}}
63+
\newcommand{\bibcitet}{\Sexpr[results=rd,stage=build]{tools:::Rd_expr_bibcite("#1", TRUE)}}
64+
\newcommand{\bibshow}{\Sexpr[results=rd,stage=build]{tools:::Rd_expr_bibshow("#1")}}

src/library/base/man/Random.Rd

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ set.seed(seed, kind = NULL, normal.kind = NULL, sample.kind = NULL)
142142
(\emph{\I{L'Ecuyer}}, 2007).
143143
}
144144
\item{\code{"L'Ecuyer-CMRG"}:}{
145-
A \sQuote{combined multiple-recursive generator} from \I{L'Ecuyer}
146-
(1999), each element of which is a feedback multiplicative
145+
A \sQuote{combined multiple-recursive generator} from
146+
%% <FIXME: bibcite>
147+
%% \I{L'Ecuyer} (1999),
148+
\bibcitet{R:L_Ecuyer:1999},
149+
%% </FIXME>
150+
each element of which is a feedback multiplicative
147151
generator with three integer elements: thus the seed is a (signed)
148152
integer vector of length 6. The period is around
149153
\eqn{2^{191}}{2^191}.
@@ -301,6 +305,10 @@ set.seed(seed, kind = NULL, normal.kind = NULL, sample.kind = NULL)
301305
\url{https://simul.iro.umontreal.ca/testu01/tu01.html} or also
302306
\url{https://github.com/umontreal-simul/TestU01-2009}.
303307
308+
%% <FIXME bibcite>
309+
%% \bibshow{R:Simard+L_Ecuyer:2007}
310+
%% </FIXME>
311+
304312
Marsaglia, G. (1997).
305313
\emph{A random number generator for C}.
306314
Discussion paper, posting on Usenet newsgroup \code{sci.stat.math} on
@@ -329,6 +337,10 @@ set.seed(seed, kind = NULL, normal.kind = NULL, sample.kind = NULL)
329337
\emph{Applied Statistics}, \bold{31}, 188--190; Remarks:
330338
\bold{34}, 198 and \bold{35}, 89.
331339
\doi{10.2307/2347988}.
340+
341+
%% <FIXME bibcite>
342+
\bibshow{*}
343+
%% </FIXME>
332344
}
333345
\author{of RNGkind: Martin Maechler. Current implementation, B. D. Ripley
334346
with modifications by Duncan Murdoch.}

src/library/base/man/sort.Rd

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,21 @@ sort.int(x, partial = NULL, na.last = NA, decreasing = FALSE,
7878
are more than 10.) Names are discarded for partial sorting.
7979

8080
Method \code{"shell"} uses Shellsort (an \eqn{O(n^{4/3})} variant from
81-
\bibcite{Sedgewick (1986)}). If \code{x} has names a stable modification is
81+
%% <FIXME bibcite>
82+
%% \bibcite{Sedgewick (1986)}).
83+
\bibcitet{R:Sedgewick:1986}.
84+
%% </FIXME>
85+
If \code{x} has names a stable modification is
8286
used, so ties are not reordered. (This only matters if names are
8387
present.)
8488

85-
Method \code{"quick"} uses Singleton (1969)'s implementation of
86-
\I{Hoare}'s Quicksort method and is only available when \code{x} is
89+
Method \code{"quick"} uses
90+
%% FIXME bibcite
91+
%% Singleton (1969)'s implementation of \I{Hoare}'s Quicksort method
92+
the implementation of \I{Hoare}'s Quicksort method from
93+
\bibcitet{R:Singleton:1969}
94+
%% </FIXME>
95+
and is only available when \code{x} is
8796
numeric (double or integer) and \code{partial} is \code{NULL}. (For
8897
other types of \code{x} Shellsort is used, silently.) It is normally
8998
somewhat faster than Shellsort (perhaps 50\% faster on vectors of
@@ -146,33 +155,49 @@ sort.int(x, partial = NULL, na.last = NA, decreasing = FALSE,
146155
refers to element numbers \emph{after removal of \code{NA}s}: see
147156
\code{\link{order}} if you want the original element numbers.
148157

149-
All attributes are removed from the return value (see Becker
150-
\abbr{et al.}, 1988, p.146) except names, which are sorted. (If
158+
All attributes are removed from the return value
159+
%% <FIXME bibcite>
160+
%% (see Becker \abbr{et al.}, 1988, p.146)
161+
\bibcitep{R:Becker+Chambers+Wilks:1988>p.146}
162+
%% </FIXME>
163+
except names, which are sorted. (If
151164
\code{partial} is specified even the names are removed.) Note that
152165
this means that the returned value has no class, except for factors
153166
and ordered factors (which are treated specially and whose result is
154167
transformed back to the original class).
155168
}
156169

157170
\references{
158-
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988).
159-
\emph{The New S Language}.
160-
Wadsworth & Brooks/Cole.
161-
162-
Knuth, D. E. (1998).
163-
\emph{The Art of Computer Programming, Volume 3: Sorting and
164-
Searching}, 2nd ed.
165-
Addison-Wesley.
171+
%% <FIXME bibcite>
172+
\bibshow{*, R:Knuth:1998}
173+
%% </FIXME>
166174

167-
Sedgewick, R. (1986).
168-
A new upper bound for Shellsort.
169-
\emph{Journal of Algorithms}, \bold{7}, 159--173.
170-
\doi{10.1016/0196-6774(86)90001-5}.
171-
172-
Singleton, R. C. (1969).
173-
Algorithm 347: an efficient algorithm for sorting with minimal storage.
174-
\emph{Communications of the ACM}, \bold{12}, 185--186.
175-
\doi{10.1145/362875.362901}.
175+
%% <FIXME bibcite>
176+
%% Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988).
177+
%% \emph{The New S Language}.
178+
%% Wadsworth & Brooks/Cole.
179+
%% </FIXME>
180+
181+
%% <FIXME bibcite>
182+
%% Knuth, D. E. (1998).
183+
%% \emph{The Art of Computer Programming, Volume 3: Sorting and
184+
%% Searching}, 2nd ed.
185+
%% Addison-Wesley.
186+
%% </FIXME>
187+
188+
%% <FIXME bibcite>
189+
%% Sedgewick, R. (1986).
190+
%% A new upper bound for Shellsort.
191+
%% \emph{Journal of Algorithms}, \bold{7}, 159--173.
192+
%% \doi{10.1016/0196-6774(86)90001-5}.
193+
%% </FIXME>
194+
195+
%% <FIXME bibcite>
196+
%% Singleton, R. C. (1969).
197+
%% Algorithm 347: an efficient algorithm for sorting with minimal storage.
198+
%% \emph{Communications of the ACM}, \bold{12}, 185--186.
199+
%% \doi{10.1145/362875.362901}.
200+
%% </FIXME>
176201
}
177202
\seealso{
178203
\sQuote{\link{Comparison}} for how character strings are collated.

src/library/grDevices/man/embedFonts.Rd

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ embedGlyphs(file, glyphInfo, outfile = file, options = character())
8282
\code{\link{postscriptFonts}},
8383
\code{\link{Devices}}.
8484

85-
\bibshow{
86-
Paul Murrell and Brian Ripley (2006).
87-
\dQuote{Non-standard fonts in PostScript and PDF graphics.}
88-
\emph{R News}, \bold{6}(2), 41--47.
89-
\url{https://www.r-project.org/doc/Rnews/Rnews_2006-2.pdf}.}
85+
%% <FIXME bibcite>
86+
%% \bibshow{
87+
%% Paul Murrell and Brian Ripley (2006).
88+
%% \dQuote{Non-standard fonts in PostScript and PDF graphics.}
89+
%% \emph{R News}, \bold{6}(2), 41--47.
90+
%% \url{https://www.r-project.org/doc/Rnews/Rnews_2006-2.pdf}.}
91+
\bibshow{R:Murrell+Ripley:2006}
92+
%% </FIXME>
9093
}
9194
\keyword{device}

src/library/grDevices/man/pdf.Rd

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,14 @@ pdf(file = if(onefile) "Rplots.pdf" else "Rplot\%03d.pdf",
443443
More details of font families and encodings and especially handling
444444
text in a non-Latin-1 encoding and embedding fonts can be found in
445445

446-
\bibshow{
447-
Paul Murrell and Brian Ripley (2006).
448-
\dQuote{Non-standard fonts in PostScript and PDF graphics.}
449-
\emph{R News}, \bold{6}(2), 41--47.
450-
\url{https://www.r-project.org/doc/Rnews/Rnews_2006-2.pdf}.}
446+
%% <FIXME bibcite>
447+
%% \bibshow{
448+
%% Paul Murrell and Brian Ripley (2006).
449+
%% \dQuote{Non-standard fonts in PostScript and PDF graphics.}
450+
%% \emph{R News}, \bold{6}(2), 41--47.
451+
%% \url{https://www.r-project.org/doc/Rnews/Rnews_2006-2.pdf}.}
452+
\bibshow{R:Murrell+Ripley:2006}
453+
%% </FIXME>
451454
}
452455
\examples{
453456
\donttest{

src/library/grDevices/man/postscript.Rd

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,14 @@ postscript(file = if(onefile) "Rplots.ps" else "Rplot\%03d.ps",
274274
More details of font families and encodings and especially handling
275275
text in a non-Latin-1 encoding and embedding fonts can be found in
276276

277-
\bibshow{
278-
Paul Murrell and Brian Ripley (2006).
279-
\dQuote{Non-standard fonts in PostScript and PDF graphics.}
280-
\emph{R News}, \bold{6}(2), 41--47.
281-
\url{https://www.r-project.org/doc/Rnews/Rnews_2006-2.pdf}.}
277+
%% <FIXME bibcite>
278+
%% \bibshow{
279+
%% Paul Murrell and Brian Ripley (2006).
280+
%% \dQuote{Non-standard fonts in PostScript and PDF graphics.}
281+
%% \emph{R News}, \bold{6}(2), 41--47.
282+
%% \url{https://www.r-project.org/doc/Rnews/Rnews_2006-2.pdf}.}
283+
\bibshow{R:Murrell+Ripley:2006}
284+
%% </FIXME>
282285
}
283286

284287
\author{

src/library/tools/R/RdHelpers.R

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,100 @@ function(x)
144144
u, x)
145145
)
146146
}
147+
148+
R_bibliographies_dir <-
149+
function()
150+
file.path(R.home("share"), "bibliographies")
151+
152+
R_bibentries <-
153+
function()
154+
{
155+
bib <- readRDS(file.path(R_bibliographies_dir(), "R.rds"))
156+
bib[lengths(bib$key) > 0L]
157+
}
158+
159+
update_R_bibentries <-
160+
function(dir = NULL)
161+
{
162+
if(is.null(dir))
163+
dir <- file.path(.R_top_srcdir_from_Rd(),
164+
"share", "bibliographies")
165+
bibfiles <- Sys.glob(file.path(dir, "*.R"))
166+
bibentries <-
167+
do.call(c, lapply(bibfiles,
168+
utils::readCitationFile,
169+
list(Encoding = "UTF-8")))
170+
saveRDS(bibentries, file.path(dir, "R.rds"))
171+
}
172+
173+
Rd_expr_bibshow <-
174+
function(x)
175+
{
176+
x <- trimws(x)
177+
if(!nzchar(x)) {
178+
## Provide a way to clear the keys cited cache.
179+
Rd_expr_bibcite_keys_cited(NULL)
180+
return(x)
181+
}
182+
bib <- R_bibentries()
183+
## <FIXME>
184+
## Would be nice to have a common reader for possibly multi-line
185+
## comma separated values ...
186+
keys <- strsplit(x, ",[[:space:]]*")[[1L]]
187+
if(any(keys == "*")) {
188+
keys <- c(keys, Rd_expr_bibcite_keys_cited())
189+
Rd_expr_bibcite_keys_cited(NULL)
190+
}
191+
y <- sort(unique(bib[bib$key %in% keys]))
192+
paste(sprintf("\\if{html}{\\out{<span id=\"reference+%s\">}}%s\\if{html}{\\out{</span>}}",
193+
string2id(unlist(y$key, use.names = FALSE)),
194+
toRd(y)),
195+
collapse =
196+
"\\if{html}{\\out{</p>}}\n\n\\if{html}{\\out{<p>}}")
197+
}
198+
199+
Rd_expr_bibcite_keys_cited <- local({
200+
.keys <- NULL
201+
function(new, add = FALSE) {
202+
if(!missing(new))
203+
.keys <<- unique(c(if(add) .keys, new))
204+
else
205+
.keys
206+
}
207+
})
208+
209+
Rd_expr_bibcite <-
210+
function(x, textual = FALSE)
211+
{
212+
x <- trimws(x)
213+
bib <- R_bibentries()
214+
keys <- strsplit(x, ",[[:space:]]*")[[1L]]
215+
## Allow b<k>a to specify before b and after a.
216+
## Could also use
217+
## regmatches(keys, regexec("(.*<)?(.*)(>.*)?", keys))
218+
before <- after <- rep_len("", length(keys))
219+
if(any(ind <- grepl("<", keys))) {
220+
before[ind] <- sub("<.*", "", keys[ind])
221+
keys[ind] <- sub(".*<", "", keys[ind])
222+
}
223+
if(any(ind <- grepl(">", keys))) {
224+
after[ind] <- sub(".*>", "", keys[ind])
225+
keys[ind] <- sub(">.*", "", keys[ind])
226+
}
227+
ind <- keys %in% unlist(bib$key)
228+
if(!all(ind)) {
229+
## <FIXME>
230+
## Should warn about keys not in the bibentries
231+
before <- before[ind]
232+
after <- after[ind]
233+
keys <- keys[ind]
234+
}
235+
Rd_expr_bibcite_keys_cited(keys, TRUE)
236+
## <FIXME>
237+
## This really needs a vectorized version of cite() ...
238+
before <- sprintf("\\if{html}{\\out{<a href=\"#reference+%s\">}}%s",
239+
string2id(keys), before)
240+
after <- sprintf("%s\\if{html}{\\out{</a>}}", after)
241+
utils::cite(keys, bib, textual, before, after)
242+
}
243+

0 commit comments

Comments
 (0)