Skip to content

Commit 1119965

Browse files
8Apr2016
1 parent bb5977a commit 1119965

7 files changed

+940
-265
lines changed

debugfunc.R

+5-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ debugfunc$wtf_is <- function(x) {
111111
print(mode(x))
112112
cat("\n4. names():\n")
113113
print(names(x))
114-
cat("\n5. str():\n")
114+
cat("\n5. slotNames():\n")
115+
print(slotNames(x))
116+
cat("\n6. attributes():\n")
117+
print(attributes(x))
118+
cat("\n7. str():\n")
115119
print(str(x))
116120
}
117121

misclang.R

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ misclang$load_or_run_function <- function(varname, file, fn, ..., forcerun=FALSE
4343
return(get(varname))
4444
}
4545

46+
#==============================================================================
47+
# Factors
48+
#==============================================================================
49+
50+
misclang$numeric_factor_to_numeric <- function(f) {
51+
# http://stackoverflow.com/questions/3418128
52+
as.numeric(levels(f))[f]
53+
}
54+
4655
#==============================================================================
4756
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
4857
#==============================================================================

miscmath.R

+72
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,78 @@ miscmath$harmonic_mean <- function(x) {
2424
1 / mean(1/x)
2525
}
2626

27+
# =============================================================================
28+
# Logarithmic sequence
29+
# =============================================================================
30+
31+
miscmath$log_sequence <- function(pow10low, pow10high,
32+
minimum=NA, maximum=NA) {
33+
# http://stackoverflow.com/questions/23901907
34+
x <- c(2:10 %o% 10^(pow10low:pow10high))
35+
if (!is.na(minimum)) {
36+
x <- x[which(x >= minimum)]
37+
}
38+
if (!is.na(maximum)) {
39+
x <- x[which(x <= maximum)]
40+
}
41+
return(x)
42+
}
43+
44+
# =============================================================================
45+
# Formatting numbers
46+
# =============================================================================
47+
48+
miscmath$format_dp <- function(x, k) {
49+
# http://stackoverflow.com/questions/3443687/formatting-decimal-places-in-r
50+
format(round(x, k), nsmall=k)
51+
}
52+
53+
miscmath$format_sf <- function(x, k = 3,
54+
scientific = FALSE,
55+
big.mark = ",", big.interval = 3,
56+
small.mark = "", small.interval = 3,
57+
drop0trailing = TRUE) {
58+
# http://stackoverflow.com/questions/3443687/formatting-decimal-places-in-r
59+
format(signif(x, k),
60+
scientific = scientific,
61+
big.mark = big.mark,
62+
big.interval = big.interval,
63+
small.mark = small.mark,
64+
small.interval = small.interval,
65+
drop0trailing = drop0trailing)
66+
}
67+
68+
miscmath$describe_p_value <- function(p, boundary_NS = 0.1, ns_val = "NS",
69+
boundary_scientific = 0.0001) {
70+
ifelse(
71+
p > boundary_NS,
72+
paste("p >", boundary_NS),
73+
ifelse(
74+
p == 0, # unusual!
75+
"p = 0", # better than "p = 0e0+00"
76+
ifelse(
77+
p >= boundary_scientific,
78+
paste("p =", miscmath$format_sf(p, scientific = FALSE,
79+
small.mark = "")),
80+
paste("p =", miscmath$format_sf(p, scientific = TRUE))
81+
)
82+
)
83+
)
84+
}
85+
86+
miscmath$p_stars <- function(p) {
87+
# Simply a convention!
88+
# http://stats.stackexchange.com/questions/29158/do-you-reject-the-null-hypothesis-when-p-alpha-or-p-leq-alpha
89+
ifelse(p <= 0.001, "***",
90+
ifelse(p <= 0.01, "**",
91+
ifelse(p < 0.05, "*",
92+
"NS")))
93+
}
94+
95+
miscmath$describe_p_value_with_stars <- function(p) {
96+
paste(miscmath$p_stars(p), ", ", miscmath$describe_p_value(p), sep = "")
97+
}
98+
2799
# =============================================================================
28100
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
29101
# =============================================================================

miscplot.R

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# miscplot.R
22

3-
requireNamespace("grid") # for gpar
3+
requireNamespace("grid") # for gpar, etc.
44
requireNamespace("ggplot2")
55

6+
library(gridExtra)
7+
#library(extrafont) # install with sudo. Then (with sudo R) run font_import() then loadfonts(). Then view with fonts() or fonttable(). See https://github.com/wch/extrafont
8+
library(Cairo)
9+
library(ggplot2)
10+
611
#==============================================================================
712
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
813
#==============================================================================
@@ -105,7 +110,7 @@ element_grob.theme_border <- function(
105110
element_gp <- grid::gpar(lwd = len0_null(element$size * .pt),
106111
col = element$colour,
107112
lty = element$linetype)
108-
polylineGrob(
113+
grid::polylineGrob(
109114
x = xlist, y = ylist, id = idlist, ..., default.units = "npc",
110115
gp = modifyList(element_gp, gp),
111116
)
@@ -132,7 +137,7 @@ element_grob.theme_L_border <- function(
132137
element_gp <- grid::gpar(lwd = len0_null(element$size * .pt),
133138
col = element$colour,
134139
lty = element$linetype)
135-
polylineGrob(
140+
grid::polylineGrob(
136141
x = c(x+width, x, x), y = c(y,y,y+height), ..., default.units = "npc",
137142
gp = modifyList(element_gp, gp),
138143
)
@@ -159,7 +164,7 @@ element_grob.theme_bottom_border <- function(
159164
element_gp <- grid::gpar(lwd = len0_null(element$size * .pt),
160165
col = element$colour,
161166
lty = element$linetype)
162-
polylineGrob(
167+
grid::polylineGrob(
163168
x = c(x, x+width), y = c(y,y), ..., default.units = "npc",
164169
gp = modifyList(element_gp, gp),
165170
)
@@ -186,7 +191,7 @@ element_grob.theme_left_border <- function(
186191
element_gp <- grid::gpar(lwd = len0_null(element$size * .pt),
187192
col = element$colour,
188193
lty = element$linetype)
189-
polylineGrob(
194+
grid::polylineGrob(
190195
x = c(x, x), y = c(y, y+height), ..., default.units = "npc",
191196
gp = modifyList(element_gp, gp),
192197
)
@@ -248,7 +253,7 @@ element_grob.theme_border_numerictype <- function(
248253
element_gp <- grid::gpar(lwd = len0_null(element$size * .pt),
249254
col = element$colour,
250255
lty = element$linetype)
251-
polylineGrob(
256+
grid::polylineGrob(
252257
x = xlist, y = ylist, id = idlist, ..., default.units = "npc",
253258
gp = modifyList(element_gp, gp),
254259
)
@@ -327,11 +332,6 @@ miscplot$multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
327332

328333
# http://cairographics.org/manual/cairo-FreeType-Fonts.html
329334

330-
library(gridExtra)
331-
#library(extrafont) # install with sudo. Then (with sudo R) run font_import() then loadfonts(). Then view with fonts() or fonttable(). See https://github.com/wch/extrafont
332-
library(Cairo)
333-
library(ggplot2)
334-
335335
miscplot$which_cairo_fonts_available <- function() {
336336
CairoFontMatch(":", sort=TRUE)
337337
}
@@ -384,7 +384,7 @@ miscplot$save_grob_to_pdf <- function(g, filename, width_mm, height_mm,
384384
title=title)
385385
# Cairo automatically embeds fonts:
386386
# http://cran.r-project.org/web/packages/Cairo/Cairo.pdf
387-
grid.draw(g) # not: print(g)
387+
grid::grid.draw(g) # not: print(g)
388388
dev.off()
389389
}
390390
}
@@ -395,7 +395,7 @@ miscplot$A4_LARGE_MM <- 297
395395
miscplot$POINTS_PER_INCH <- 72
396396
miscplot$POINTS_PER_MM <- miscplot$POINTS_PER_INCH * miscplot$INCHES_PER_MM
397397

398-
miscplot$BLANK_GROB <- rectGrob(gp=grid::gpar(fill="white", alpha=0))
398+
miscplot$BLANK_GROB <- grid::rectGrob(gp=grid::gpar(fill="white", alpha=0))
399399
# ... alpha=0 makes it invisible
400400
miscplot$NOLEGEND <- theme(legend.position="none")
401401
miscplot$MOVELEGEND_BOTTOMLEFT <- theme(legend.justification=c(0,0),

0 commit comments

Comments
 (0)