1\name{reprompt}
2\alias{reprompt}
3\title{Update the documentation of a topic}
4\description{Examine the documentation of functions, methods or classes and
5  update it with additional arguments, aliases, methods or slots, as
6  appropriate. This is an extention of the promptXXX() family of functions.
7}
8\usage{
9reprompt(object, infile = NULL, Rdtext = NULL, final = TRUE,
10         type = NULL, package = NULL, methods = NULL, verbose = TRUE,
11         filename = NULL, sec_copy = TRUE, edit = FALSE, \dots)
12}
13\arguments{
14  \item{object}{the object whose documentation is to be updated, such as
15    a string, a function, a help topic, a parsed Rd object, see
16    `Details'.}
17  \item{infile}{a file name containing Rd documentation, see `Details'.}
18  \item{Rdtext}{a character string with Rd formatted text, see `Details'.}
19  \item{final}{logical, if \code{TRUE} modifies the output of
20    \code{prompt} so that the package can be built.}
21  \item{type}{type of topic, such as \code{"methods"} or
22    \code{"class"}, see `Details'.}
23  \item{package}{package name; document only objects defined in this
24    package, especially useful for methods, see `Details'.}
25  \item{methods}{
26    used for documentation of S4 methods only, rarely needed even for
27    them.  This argument is passed on to
28    \code{\link[methods]{promptMethods}}, see its documentation for
29    details.
30  }
31  \item{verbose}{if \code{TRUE} print messages on the screen.}
32  \item{filename}{name of the file for the generated Rd content; if
33    \code{NULL}, a suitable file name is generated, if \code{TRUE} it
34    will be set to \code{infile},
35    if \code{FALSE} the Rd text is returned, see `Details'.}
36  \item{\dots}{currently not used.}
37  \item{sec_copy}{if \code{TRUE} copy Rd contents of unchanged sections
38    in the result, see Details.}
39  \item{edit}{if \code{TRUE} call \code{file.edit} just before
40    returning. This argument is ignored if \code{filename} is \code{FALSE}.}
41}
42\details{
43  The typical use of \code{reprompt} is with one argument, as in
44  \preformatted{
45    reprompt(infile = "./Rdpack/man/reprompt.Rd")
46    reprompt(reprompt)
47    reprompt("reprompt")
48  }
49  \code{reprompt} updates the requested documentation and writes the new
50  Rd file in the current working directory. When argument \code{infile}
51  is used, the descriptions of all objects described in the input file
52  are updated. When an object or its name is given, \code{reprompt}
53  looks first for installed documentation and processes it in the same
54  way as in the case of \code{infile}. If there is no documentation for
55  the object, \code{reprompt} creates a skeleton Rd file similar to the
56  one produced by the base R functions from the \code{prompt} family (if
57  \code{final = TRUE}, the default, it modifies the output of
58  \code{prompt()}, so that the package can be built).
59
60  To document a function, say \code{myfun}, in an existing Rd file, just
61  add \code{myfun()} to the usage section in the file and call
62  \code{reprompt()} on that file. Put quotes around the function name if
63  it is non-syntactic.  For replacement functions (functions with names
64  ending in \code{<-}) \code{reprompt()} will insert the proper usage
65  statement. For example, if the signature of \code{xxx<-} is \code{(x,
66  ..., value)} then both, \code{"xxx<-"()} and \code{xxx() <- value}
67  will be replaced by \code{xxx(x, ...) <- value}.
68
69  For S4 methods and classes the argument "package" is often needed to
70  restrict the output to methods defined in the package of interest.
71  \preformatted{
72    reprompt("myfun-methods")
73
74    reprompt("[<--methods", package = "mypackage")
75    reprompt("[<-", type = "methods", package = "mypackage") # same
76
77    reprompt("myclass", type = "class", package = "mypackage")
78    reprompt("myclass-class", package = "mypackage")         # same
79  }
80  Without the "package" argument the reprompt for \code{"[<-"} would give
81  all methods defined by loaded packages at the time of the call.
82
83
84  Currently \code{reprompt} functionality is not implemented for topic
85  "package" but if \code{object} has the form "name-package" (or the
86  equivalent with argument \code{topic}) and there is no documentation
87  for \code{package?name}, \code{reprompt} calls
88  \code{\link{promptPackageSexpr}} to create the required shell.
89  Note that the shell produced by \code{promptPackageSexpr} does not
90  need `reprompting' since the automatically generated information is
91  included by \verb{\Sexpr}'s, not literal strings.
92
93
94
95  Below are the details.
96
97  Typically, only one of \code{object}, \code{infile}, and \code{Rdtext}
98  is supplied. Warning messages are issued if this is not the case.
99
100  The object must have been made available by the time when
101  \code{reprompt()} is issued. If the object is in a package this is
102  typically achieved by a \code{library()} command.
103
104  \code{object} may be a function or a name, as accepted by the \code{?}
105  operator. If it has the form "name-class" and "name-methods" a
106  documentation shell for class "name" or the methods for generic function
107  "name" will be examined/created. Alternatively, argument \code{type}
108  may be set to "class" or "methods" to achieve the same effect.
109
110  \code{infile} specifies a documentation file to be updated. If it
111  contains the documentation for one or more functions, \code{reprompt}
112  examines their usage statements and updates them if they have
113  changed. It also adds arguments to the "arguments" section if not all
114  arguments in the usage statements have entries there. If \code{infile}
115  describes the methods of a function or a class, the checks made are as
116  appropriate for them. For example, new methods and/or slots are added
117  to the corresponding sections.
118
119  It is all too easy in interactive use to forget to name the
120  \code{infile} argument, compare\cr
121  \code{reprompt("./man/reprompt.Rd")} vs.
122  \code{reprompt(infile = "./man/reprompt.Rd")}).\cr
123  A convenience feature is that if \code{infile} is missing
124  and \code{object} is a character string ending in ".Rd" and containing
125  a forward slash (i.e. it looks like Rd file in a directory), then it
126  is taken to be \code{infile}.
127
128  \code{Rdtext} is similar to \code{infile} but the Rd content is taken
129  from a character vector.
130
131  If Rd content is supplied by \code{infile} or \code{Rdtext},
132  \code{reprompt} uses it as a basis for comparison. Otherwise it tries
133  to find installed documentation for the object or topic requested.
134  If that fails, it resorts to one of the \code{promptXXX} functions to
135  generate a documentation shell. If that also fails, the requested
136  object or topic does not exist.
137
138  If the above succeeds, the function examines the current definition of
139  the requested object(s), methods, or class and amends the
140  documentation with any additional items it finds.
141
142  For Rd objects describing one or more functions, the usage expressions
143  are checked and replaced, if they have changed. Arguments appearing in
144  one or more usage expressions and missing from section "Arguments" are
145  amended to it with content "Describe ..."  and a message is
146  printed. Arguments no longer in the usage statements are NOT removed
147  but a message is issued to alert the user. Alias sections are inserted
148  for any functions with "usage" but without "alias" section.
149
150
151  If \code{filename} is a character string, it is used as is, so any
152  path should be included in the string.  Argument \code{filename}
153  usuallly is omitted since the automatically generated file name is
154  suitable in most cases. Exceptions are functions with non-standard
155  names (such as replacement functions whose names end in \code{"<-"})
156  for which the generated file names may not be acceptable on some
157  operating systems.
158
159  If \code{filename} is missing or \code{NULL}, a suitable name is
160  generated as follows. If \code{infile} is supplied, \code{filename} is
161  set to a file with the same name in the current working directory
162  (i.e. any path information in \code{infile} is dropped). Otherwise,
163  \code{filename} is obtained by appending the name tag of the Rd object
164  with \code{".Rd"}.
165
166  If \code{filename} is \code{TRUE}, it is set to \code{infile} or, if
167  \code{infile} is missing or \code{NULL}, a suitable name is generated
168  as above. This can be used to change \code{infile} in place.
169
170  If \code{filename} is \code{FALSE}, the Rd text is returned as a
171  character vector and not written to a file.
172
173  If \code{edit} is \code{TRUE}, the reprompted file is opened in an
174  editor, see also \code{\link{ereprompt}} (`e' for `edit') which is
175  like \code{reprompt} but has as default \code{edit = TRUE} and some
176  other related settings.
177
178  \code{file.edit()} is used to call the editor.  Which editor is opened
179  depends on the OS and on the user configuration. RStudio users will
180  probably prefer the 'Reprompt' add-in or the underlying function
181  \code{\link{RStudio_reprompt}}.  Emacs users would normally have set
182  up \code{emacsclient} as their editor and this is automatically done
183  by EMACS/ESS (even on Windows).
184
185  If argument \code{sec_copy} is \code{TRUE} (the default),
186  \code{reprompt} will, effectively, copy the contents of (some)
187  unchanged sections, thus ensuring that they are exactly the same as in
188  the original.  This needs additional work, since parsing an Rd file
189  and then exporting the created Rd object to an Rd file does not
190  necessarilly produce an identical Rd file (some escape sequences may
191  be changed in the process, for example).  Even though the new version
192  should be functionally equivalent to the original, such changes are
193  usually not desirable. For example, if such changes creep into the
194  Details section (which \code{reprompt} never changes) the user may be
195  annoyed or worried.
196
197}
198\value{
199  if \code{filename} is a character string or \code{NULL}, the name of
200  the file to which the updated shell was written.
201
202  Otherwise, the Rd text is returned as a character vector.
203}
204
205\author{Georgi N. Boshnakov}
206\note{
207
208  The arguments of \code{reprompt} are similar to prompt, with some
209  additions. As in \code{prompt}, \code{filename} specifies the output
210  file. In \code{reprompt} a new argument, \code{infile}, specifies the
211  input file containing the Rd source.
212
213  When \code{reprompt} is used to update sources of Rd documentation for
214  a package, it is best to supply the original Rd file in argument
215  \code{infile}. Otherwise, if the original Rd file contains
216  \verb{\Sexpr} commands, \code{reprompt} may not be able to recover the
217  original Rd content from the installed documentation. Also, the fields
218  (e.g. the keywords) in the installed documentation may not be were you
219  expect them to be. (This may be addressed in a future revision.)
220
221
222  While \code{reprompt} adds new items to the documentation, it never
223  removes existing content. It only issues a suggestion to remove an
224  item, if it does not seem necessary any more (e.g. a removed argument
225  from a function definition).
226
227  \code{reprompt} handles usage statements for S3 and S4 methods
228  introduced with any of the macros \verb{\method}, \verb{\S3method} and
229  \verb{\S4method}, as in
230  \verb{\method{fun}{class}(args...)}. \code{reprompt} understands also
231  subsetting ans subassignment operators.  For example, suppose that the
232  \code{\\arguments} section of file "bracket.Rd" contains these
233  directives (or any other wrong signatures):
234
235\preformatted{
236    \method{[}{ts}()
237    \method{[[}{Date}()
238}
239
240Then \code{reprompt("./bracket.Rd")} will change them to
241
242\preformatted{
243    \method{[}{ts}(x, i, j, drop = TRUE)
244    \method{[[}{Date}(x, \dots, drop = TRUE)
245}
246
247  This works for the assignment operators and functions, as well. For
248  example, any of these
249
250\preformatted{
251    \method{`[<-`}{POSIXlt}()
252    \method{[}{POSIXlt}(x, j) <- value
253}
254
255will be converted by \code{reprompt} to the standard form
256
257\preformatted{
258    \method{[}{POSIXlt}(x, i, j) <- value
259}
260
261Note that the quotes in \code{`[<-`} above.
262
263  Usage statements for functions are split over two or more lines if
264  necessary. The user may edit them afterwards if the automatic
265  splitting is not appropriate, see below.
266
267  The usage section of Rd objects describing functions is left intact if
268  the usage has not changed. To force \code{reprompt} to recreate the
269  usage section (e.g. to reformat long lines), invalidate the usage of
270  one of the described functions by removing an argument from its usage
271  expression.  Currently the usage section is recreated completely if
272  the usage of any of the described functions has changed. Manual
273  formatting may be lost in such cases.
274}
275
276%% ~Make other sections like Warning with \section{Warning }{....} ~
277\seealso{
278    \code{\link{ereprompt}} which by default calls the editor on the
279    original file
280}
281\examples{
282## note: usage of reprompt() is simple.  the examples below are bulky
283##       because they simulate various usage scenarios with commands,
284##       while in normal usage they would be due to editing.
285
286## change to a temporary directory to avoid clogging up user's
287cur_wd <- getwd()
288tmpdir <- tempdir()
289setwd(tmpdir)
290
291## as for prompt() the default is to save in current dir as "seq.Rd".
292## the argument here is a function, reprompt finds its doc and
293## updates all objects described along with `seq'.
294## (In this case there is nothing to update, we have not changed `seq'.)
295
296fnseq <- reprompt(seq)
297
298## let's parse the saved Rd file (the filename is returned by reprompt)
299rdoseq <- tools::parse_Rd(fnseq)   # parse fnseq to see the result.
300Rdo_show(rdoseq)
301
302## we replace usage statements with wrong ones for illustration.
303## (note there is an S3 method along with the functions)
304dummy_usage <- char2Rdpiece(paste("seq()", "\\\\method{seq}{default}()",
305                   "seq.int()", "seq_along()", "seq_len()", sep="\n"),
306                   "usage")
307rdoseq_dummy <- Rdo_replace_section(rdoseq, dummy_usage)
308Rdo_show(rdoseq_dummy)  # usage statements are wrong
309
310reprompt(rdoseq_dummy, file = "seqA.Rd")
311Rdo_show(tools::parse_Rd("seqA.Rd"))  # usage ok after reprompt
312
313## define function myseq()
314myseq <- function(from, to, x){
315    if(to < 0) {
316        seq(from = from, to = length(x) + to)
317    } else seq(from, to)
318}
319
320## we wish to describe  myseq() along with seq();
321##    it is sufficient to put myseq() in the usage section
322##    and let reprompt() do the rest
323rdo2 <- Rdo_modify_simple(rdoseq, "myseq()", "usage")
324reprompt(rdo2, file = "seqB.Rd")  # updates usage of myseq
325
326## show the rendered result:
327Rdo_show(tools::parse_Rd("seqB.Rd"))
328
329## Run this if you wish to see the Rd file:
330##   file.show("seqB.Rd")
331
332reprompt(infile = "seq.Rd", filename = "seq2.Rd")
333reprompt(infile = "seq2.Rd", filename = "seq3.Rd")
334
335## Rd objects for installed help may need some tidying for human editing.
336#hseq_inst <- help("seq")
337#rdo <- utils:::.getHelpFile(hseq_inst)
338rdo <- Rdo_fetch("seq", "base")
339rdo
340rdo <- Rdpack:::.Rd_tidy(rdo)          # tidy up (e.g. insert new lines
341                                       #          for human readers)
342reprompt(rdo) # rdo and rdoseq are equivalent
343all.equal(reprompt(rdo), reprompt(rdoseq)) # TRUE
344
345## clean up
346unlink("seq.Rd")         # remove temporary files
347unlink("seq2.Rd")
348unlink("seq3.Rd")
349unlink("seqA.Rd")
350unlink("seqB.Rd")
351
352setwd(cur_wd)            # restore user's working directory
353unlink(tmpdir)
354}
355% Add one or more standard keywords, see file 'KEYWORDS' in the
356% R documentation directory.
357\keyword{Rd}
358