-
Notifications
You must be signed in to change notification settings - Fork 25
/
generate_docs.R
68 lines (60 loc) · 1.7 KB
/
generate_docs.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
60
61
62
63
64
65
66
67
68
unlink("man", recursive = TRUE)
devtools::document(roclets = c('rd', 'collate', 'namespace'))
library(glue)
library(tidylog)
library(dplyr)
library(tidyr)
library(stringr)
# get tidylog functions (not very elegant...)
conn <- file("NAMESPACE")
namespace <- readLines(conn)
close(conn)
functions <- namespace[grepl("export", namespace)]
functions <- sub("export\\(", "", functions)
functions <- sub("\\)", "", functions)
functions_dplyr <- lsf.str("package:dplyr")
functions_tidyr <- lsf.str("package:tidyr")
template <- "\\name{<f>}
\\alias{<f>}
\\title{Wrapper around <package>::<f>
that prints information about the operation}
\\usage{
<usage>
}
\\arguments{
<args>
}
\\value{
see \\link[<package><topic>]{<f>}
}
\\description{
Wrapper around <package>::<f>
that prints information about the operation
}"
tidylog_env <- environment(tidylog::tidylog)
html_links <- tools::findHTMLlinks(level = 0:5)
for (f in functions) {
if (f %in% functions_dplyr) {
package <- "dplyr"
} else if (f %in% functions_tidyr) {
package <- "tidyr"
} else {
next
}
# find topic
topic <- html_links[str_detect(html_links, package) & names(html_links) == f]
topic <- str_match(topic, "/([^/]*)\\.html")[, 2]
if (length(topic) == 0) {
topic <- ""
} else {
topic <- paste0(":", topic)
}
args_list <- names(formals(f, tidylog_env))
usage <- glue("{f}({paste(args_list, collapse = ', ')})")
args <- glue("\\item{<args_list>}{see \\link[<package><topic>]{<f>}}", .open = "<", .close = ">")
args <- paste0(args, collapse = "\n\n")
doc <- glue(template, .open = "<", .close = ">")
conn <- file(glue("man/{f}.Rd"))
writeLines(doc, conn)
close(conn)
}