forked from introRcpp/introRcpp.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-rcpp-chunks.r
77 lines (69 loc) · 1.99 KB
/
extract-rcpp-chunks.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
PROLOGUE <- "```{R echo = FALSE, warnings = FALSE}\nsource(\"include.cpp.r\")\nincludeCppPath <- \"%s/\"\n```"
extract.rcpp.chunks <- function(filename, newfile, output.dir = "src/", overwrite = FALSE) {
BEG <- "```{Rcpp"
END <- "```"
# extraction
x <- scan(filename, character(), sep = "\n", blank.lines.skip = FALSE)
w.BEG <- which(substring(x,1,8) == BEG)
c.END <- which(substring(x,1,3) == END)
CHUNKS <- list()
keep <- rep(TRUE, length(x))
for(i in w.BEG) {
j <- min( c.END[ c.END > i ] )
if(j == i+1) next # too short
CHUNKS <- c( CHUNKS, list(x[i:j]) )
keep[ (i+2):(j-1) ] <- FALSE
}
# sauvegarde
cnames <- character(length(CHUNKS))
k <- 0
for(i in seq_along(CHUNKS)) {
chunk <- CHUNKS[[i]]
name <- get.chunk.name(chunk)
cat("chunk name", name, "\n")
if(name == "") {
k <- k+1
filename <- sprintf("noname%d.cpp", k)
} else {
filename <- paste0(name, ".cpp")
}
cnames[i] <- filename
filename <- paste0(output.dir, "/", filename)
save.chunk(chunk, filename, overwrite)
}
# fichier modifié
if(!missing(newfile)) {
if(file.exists(newfile))
stop(newfile, "exists")
for(k in seq_along(w.BEG)) {
i <- w.BEG[k]
x[i] <- '```{R echo = FALSE, results = "asis"}'
x[i+1] <- sprintf("include.cpp('%s')", cnames[k])
}
x <- x[keep]
x <- c(sprintf(PROLOGUE, output.dir), x)
zz <- file(newfile, "w")
cat( paste(x, collapse = "\n"), file = zz )
close(zz)
}
}
get.chunk.name <- function(chunk) {
m <- regexec("^(\\w|:)+.*[[:blank:]+](\\w+)\\(", chunk)
m <- regmatches(chunk, m)
m <- m[ sapply(m, length) > 0 ]
if(length(m) == 0)
""
else
m[[1]][3]
}
save.chunk <- function(chunk, filename, overwrite = FALSE) {
if(file.exists(filename)) {
warning(filename, "exists")
if(!overwrite) return;
}
zz <- file(filename, "w")
chunk <- chunk[-1]
chunk <- chunk[-length(chunk)]
cat(paste(chunk, collapse = "\n"), "\n", file = zz)
close(zz)
}