Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2701 views
1
.ess_weave <- function(command, file, encoding = NULL){
2
cmd_symb <- substitute(command)
3
if(grepl('knit|purl', deparse(cmd_symb))) require(knitr)
4
od <- getwd()
5
on.exit(setwd(od))
6
setwd(dirname(file))
7
frame <- parent.frame()
8
if(is.null(encoding))
9
eval(bquote(.(cmd_symb)(.(file))), envir = frame)
10
else
11
eval(bquote(.(cmd_symb)(.(file), encoding = .(encoding))), envir = frame)
12
}
13
14
.ess_knit <- function(file, output = NULL){
15
library(knitr)
16
frame <- parent.frame()
17
od <- getwd()
18
on.exit(setwd(od))
19
setwd(dirname(file))
20
## this bquote is really needed for data.table := operator to work correctly
21
eval(bquote(knit(.(file), output = .(output))), envir = frame)
22
}
23
24
.ess_sweave <- function(file, output = NULL){
25
od <- getwd()
26
frame <- parent.frame()
27
on.exit(setwd(od))
28
setwd(dirname(file))
29
eval(bquote(Sweave(.(file), output = .(output))), envir = frame)
30
}
31
32
## Users might find it useful. So don't prefix with .ess.
33
htsummary <- function (x, hlength = 4, tlength = 4, digits = 3)
34
{
35
## fixme: simplify and generalize
36
snames <- c("mean", "sd", "min", "max", "nlev", "NAs")
37
d <- " "
38
num_sumr <- function(x){
39
c(f(mean(x, na.rm = TRUE)),
40
f(sd(x, na.rm = TRUE)),
41
f(min(x, na.rm = TRUE)),
42
f(max(x, na.rm = TRUE)),
43
d,
44
f(sum(is.na(x), na.rm = TRUE)))
45
}
46
f <- function(x) format(x, digits = digits)
47
48
if (is.data.frame(x) | is.matrix(x)) {
49
if (nrow(x) <= tlength + hlength){
50
print(x)
51
} else {
52
if (is.matrix(x))
53
x <- data.frame(unclass(x))
54
## conversion needed, to avoid problems with derived classes suchs as data.table
55
h <- as.data.frame(head(x, hlength))
56
t <- as.data.frame(tail(x, tlength))
57
for (i in 1:ncol(x)) {
58
h[[i]] <- f(h[[i]])
59
t[[i]] <- f(t[[i]])
60
}
61
## summaries
62
sumr <- sapply(x, function(c){
63
if(is.logical(c))
64
## treat logical as numeric; it's harmless
65
c <- as.integer(c)
66
if(is.numeric(c))
67
num_sumr(c)
68
else if(is.factor(c)) c(d, d, d, d, nlevels(c), sum(is.na(c)))
69
else rep.int(d, length(snames))
70
})
71
sumr <- as.data.frame(sumr)
72
row.names(sumr) <- snames
73
dots <- rep("...", ncol(x))
74
empty <- rep.int(" ", ncol(x))
75
lines <- rep.int(" ", ncol(x))
76
df <- rbind(h, ...= dots, t, `_____` = lines, sumr, ` ` = empty)
77
print(df)
78
}
79
} else {
80
cat("head(", hlength, "):\n", sep = "")
81
print(head(x, hlength))
82
if(length(x) > tlength + hlength){
83
cat("\ntail(", tlength, "):\n", sep = "")
84
print(tail(x, tlength))
85
}
86
cat("_____\n")
87
if(is.numeric(x) || is.logical(x))
88
print(structure(num_sumr(x), names = snames), quote = FALSE)
89
else if(is.factor(x)){
90
cat("NAs: ", sum(is.na(x), na.rm = TRUE), "\n")
91
cat("levels: \n")
92
print(levels(x))
93
}
94
}
95
invisible(NULL)
96
}
97
98
.ess_vignettes <- function(){
99
vs <- unclass(browseVignettes())
100
vs <- vs[sapply(vs, length) > 0]
101
102
mat2elist <- function(mat){
103
if(!is.null(dim(mat))){
104
apply(mat, 1, function(r)
105
sprintf("(list \"%s\")",
106
paste0(gsub("\"","\\\\\"",
107
as.vector(r[c("Title", "Dir", "PDF", "File", "R")])),
108
collapse = "\" \"")))
109
}
110
}
111
cat("(list \n",
112
paste0(mapply(function(el, name) sprintf("(list \"%s\" %s)",
113
name,
114
paste0(mat2elist(el), collapse = "\n")),
115
vs, names(vs)), collapse = "\n"), ")\n")
116
}
117
118