-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.r
127 lines (95 loc) · 3.03 KB
/
template.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
## template function
## for now just returning an object of class "template".
# I had a different way in mind to include the journal styles. If we keep that function rather neutral,
# it would be possible to call it later with a particular function par.template().
template <- function(..., author = NULL, journal = NULL, date = NULL) {
out <- list()
## I propose we stock all the par argments, the object will then be readily settable !
out$par <- par(no.readonly=T)[]
args <- list(...)
## arguments we change
ext <- charmatch(names(args), names(out$par))
out$par[ext] <- args
if(!is.null(author)) out$author <- author
if(!is.null(journal)) out$journal <- journal
if(!is.null(date)) {out$date <- as.POSIXct(date)} else { out$date <- as.POSIXlt(Sys.time(), "") }
class(out) <- c("template")
return(out)
}
# collection of templates
collection.template <- function(){
science <- template(
family = "sans",
tck = 0.02,
bty = "n",
journal = "Science",
author = "Florian Schneider"
)
nature <- template(
family = "sans",
tck = 0.018,
bty = "o",
journal = "Nature",
author = "Kévin Cazellesr"
)
return(list(science, nature))
}
gettemplates <- function() {
objects <- ls( envir = .GlobalEnv)[sapply(ls(envir = .GlobalEnv), function(x) eval(parse(text=paste("class(", x, ")", sep = "")) ) ) == "template"]
names <- sapply(objects, function(x) eval(parse(text=paste(x, "$journal"))) )
authors <- sapply(objects, function(x) eval(parse(text=paste(x, "$author"))) )
return( data.frame(objects = objects, names = names, authors = authors ) )
}
gettemplates()$objects
# convertor
as.template <- function(...){
}
# getter functions
gjournal <- function(x) {return(x$journal)}
gauthor <- function(x) {return(x$authors)}
#
par.template <- function(x=NULL, journal = NULL, verbose=TRUE) {
# We must find a way to easily stock templates
if (!is.null(journal)){
Template <- collection.template()
journals <- unlist(lapply(Template, gjournal))
w <- charmatch(tolower(journal),tolower(journals))
if (is.na(w)) stop(" --> The journal style that you specified does not exist.")
journal <- Template[[w]]
print(journal)
}
x.names <- names(x$par)
j.names <- names(journal$par)
m.names <- sort(unique(c(x.names, j.names)))
parms <- sapply(m.names, function(i) {
if (i %in% j.names) journal$par[[i]]
else x$par[[i]]
}, simplify = FALSE)
if (verbose==TRUE){
#cat(paste(" --> Journal's template selected:", journals[w],"\n"))
cat(" --> Plot is ready")
}
par(parms)
}
# application
example.template <- function(){
# random scatter plot
x <- runif(30, 0.2, 0.9)
y <- 0.5 + x*3.1 + rnorm(30, 0, 0.2)
plot(y ~ x)
# Unsing template nature and extra arguments
own <- template(
pch = 20,
las = 1,
bty = "l",
tck = -0.02,
family = "serif",
cex.lab = 2,
mar = c(5,5,2,2)+0.1,
author = "Flo Schneider"
)
par.template(own)
plot(y ~ x)
par.template(journal = "scien")
plot(y ~ x)
}