-
Notifications
You must be signed in to change notification settings - Fork 8
/
R-help (update Sublime Peek Help).r
94 lines (77 loc) · 2.77 KB
/
R-help (update Sublime Peek Help).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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
## script to get R help files
# This script generates R help files for all installed packages for SublimePeek
# and moves the style file R.css to the correct location.
# Help files for the base packages and ggplot2 are also available at
# https://github.com/jlegewie/SublimePeek-R-help
## USER PARAMETERS ##
# set the loc variable to the ST2 Packages Folder (don't forget the / at the end)
loc = '~/Dropbox/lib/Sublime Text 2/Packages'
# the default setting uses all installed packages. Alternative: vector of packages
pkg.user = rownames(installed.packages())
# function to get html formated help
# adopted from stackoverflow
getHTMLhelp = function(...) {
h = help(...)
if (length(h)>0) {
out=capture.output(
tools:::Rd2HTML(utils:::.getHelpFile(h[1]))
)
} else {
out=""
}
# return(htmlspecialchars(out))
return(paste(out,"\n",sep=""))
}
# deal with special HTML/XML characters
htmlspecialchars <- function(string) {
# x = c("&", '"', "<", ">")
# subx = c("&", """, "<", ">")
x = c("&")
subx = c("&")
for (i in seq_along(x)) {
string = gsub(x[i], subx[i], string, fixed = TRUE)
}
string
}
# create folder for R help files
dir.create(paste(loc,"SublimePeek-R-help/",sep=""),showWarnings=FALSE)
# get main packages
pkg.main = names(na.omit(installed.packages()[, 'Priority']))
# all packages
pkg.all = unique(c(pkg.main,pkg.user))
pkg.all = pkg.all[!(pkg.all %in% c("survival","tcltk","cem","debug"))]
# these packages crashed my R so I excluded them...
# load packages
sapply(pkg.all, library, character.only = TRUE)
# loaded packages
pkg.loaded=(.packages())
k = length(pkg.loaded)
# iterate through packages
n.f = 0
n.p = 0
for(pkg in pkg.loaded) {
n.p = n.p+1
cat("creating help files for '", pkg,"' (",n.p,"/",k,")...",sep="")
# name of package environment
env =paste("package",pkg,sep=":")
# get functions in packages environment
fn.pkg =ls(env, all.names = FALSE)
# remove functions
idx = grep("[<>&@:\\?\\{\\(\\)\\[\\^\\*\\$!%/\\|\\+=~\\-]", fn.pkg)
if (length(idx)) fn.pkg = fn.pkg[-idx]
idx <- grep("^(\\._|\\.\\.)", fn.pkg)
if (length(idx)) fn.pkg = fn.pkg[-idx]
# iterate through functions in package
for(fn in fn.pkg) {
obj = get(fn, envir = as.environment(env))
if (mode(obj) == "function") {
n.f = n.f+1
cat(getHTMLhelp(fn),file= paste(loc,"SublimePeek-R-help/",fn,".html",sep=""))
}
}
cat(" done!\n")
}
# copy style file (R.css) to folder with R help files
file.copy(paste(loc,"SublimePeek/css/R.css",sep=""),paste(loc,"SublimePeek-R-help/R.css",sep=""))
# end of file ggplot
cat("\n\nHelp files from ",n.f," functions in ", n.p," packages saved to JSON.\n")