-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkeywords.R
59 lines (55 loc) · 1.55 KB
/
keywords.R
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
#' List valid keywords for R man pages
#'
#' List valid keywords for R man pages
#'
#' If \code{topic} is provided, return a list of the keywords associated with
#' \code{topic}. Otherwise, display the list of valid R keywords from the R
#' doc/KEYWORDS file.
#'
#' @param topic object or man page topic
#' @author Gregory R. Warnes \email{greg@@warnes.net}
#' @seealso \code{\link[utils]{help}}
#' @keywords documentation
#' @examples
#'
#' ## Show all valid R keywords
#' \dontrun{
#' keywords()
#'
#' ## Show keywords associated with the 'merge' function
#' keywords(merge)
#' keywords("merge")
#' }
#' @importFrom stats na.omit
#' @importFrom utils help.search
#' @export
keywords <- function(topic) {
file <- file.path(R.home("doc"), "KEYWORDS")
if (missing(topic)) {
file.show(file)
}
else {
kw <- scan(file = file, what = character(), sep = "\n", quiet = TRUE)
kw <- grep("&", kw, value = TRUE)
kw <- gsub("&[^&]*$", "", kw)
kw <- gsub("&+", " ", kw)
kw <- na.omit(trimws(kw))
ischar <- tryCatch(is.character(topic) && length(topic) ==
1L, error = identity)
if (inherits(ischar, "error")) {
ischar <- FALSE
}
if (!ischar) {
topic <- deparse(substitute(topic))
}
item <- paste("^", topic, "$", sep = "")
topics <- function(k) {
matches <- help.search(keyword = k)$matches
matches[, match("topic", tolower(colnames(matches)))]
}
matches <- lapply(kw, topics)
names(matches) <- kw
tmp <- unlist(lapply(matches, function(m) grep(item, m, value = TRUE)))
names(tmp)
}
}