Skip to content

Commit 301d6ca

Browse files
committed
overhaul 2019
1 parent a185117 commit 301d6ca

File tree

209 files changed

+549
-4451
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+549
-4451
lines changed

.gitignore

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ _static
44
# TODO: convert this to git-lfs
55
/data
66

7-
_R/md
8-
_R/figures
9-
_R/run.Rout
10-
rst
7+
*.md
8+
*.html
119
*.Rout
1210

11+
source/*/figures/
12+
source/*/txt/
13+
14+
source/analysis/_R/wc10/
15+
test.*
16+
cropped-landsat.*
17+
1318
# Do no keep local data directories
1419
source/*/_R/data/
1520

R/check_errors.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
ff <- list.files("build/html", patt='\\.html$', recursive=TRUE, full=TRUE)
3+
for (f in ff) {
4+
x <- readLines(f, warn=FALSE)
5+
i <- grep("## Error", x)
6+
if (length(i) > 0) {
7+
print(f)
8+
print(head(x[i]))
9+
cat("----\n\n")
10+
}
11+
}

R/make_site.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
unlink("build")
3+
shell("make html")
4+
5+
f <- list.files("source", patt='\\.md\\.txt$', recursive=TRUE, full=TRUE)
6+
f <- grep("/txt/", f, value=TRUE)
7+
g <- gsub("txt/", "", f)
8+
g <- gsub("source/", "", g)
9+
h <- file.path("build/html/_sources", g)
10+
x <- file.remove(h)
11+
y <- file.copy(f, h, overwrite=TRUE)
12+
13+

R/requirements.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
libfun <- function(x) {
3+
d <- readLines(x)
4+
i <- grep('^library\\(', d)
5+
j <- grep('^require\\(', d)
6+
d[unique(c(i,j))]
7+
}
8+
9+
f <- list.files("source", patt='\\.rmd$', recursive=TRUE, full=TRUE, ignore.case=TRUE)
10+
pkgs <- unique(unlist(sapply(f, libfun)))
11+
pkgs <- pkgs[nchar(pkgs) < 100]
12+
pkgs <- gsub("library\\(", "", pkgs)
13+
pkgs <- trimws(gsub(")", "", pkgs))
14+
pkgs <- sort(unique(c(pkgs, c("devtools", "animation", "deldir","dismo","fields","formatR","geosphere","gstat","jsonlite","kernlab","knitr", "latticeExtra","maptools","randomForest", "rasterVis","rgdal","rgeos","rgl", "rJava", "rpart","spatstat","spdep","spgwr", "XML", "sf"))))
15+
pkgs <- pkgs[!(pkgs=='rspatial')]
16+
17+
ipkgs <- rownames(installed.packages())
18+
for (pk in pkgs) {
19+
if (!(pk %in% ipkgs)) {
20+
print(paste("installing", pk))
21+
install.packages(pkgs=pk, repos="https://cran.cnr.berkeley.edu/", quiet=TRUE)
22+
library(pk, character.only=TRUE)
23+
}
24+
}
25+
26+

R/run.R

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/Rscript
2+
3+
args <- commandArgs(TRUE)
4+
5+
if (length(args) < 2) {
6+
print(args)
7+
stop("there must be at least two arguments")
8+
}
9+
10+
cmd <- args[1]
11+
stopifnot(cmd %in% c("knit", "build"))
12+
13+
chapters <- c("rosu", "intr", "spatial", "analysis", "cases", "sdm", "sphere", "rs")
14+
chapter <- tolower(args[2])
15+
if (chapter == "all") {
16+
# this is problematic as there is a lot of function hiding
17+
chapter <- chapters
18+
} else {
19+
stopifnot(chapter %in% chapters)
20+
}
21+
22+
option <- ifelse(length(args) > 2, args[3], "")
23+
24+
oldpath <- getwd()
25+
26+
27+
28+
do_build <- function(option) {
29+
if (option=="pdf"){
30+
shell("make latexpdf")
31+
return()
32+
} else if (option=="clean"){
33+
unlink("_build", recursive=TRUE)
34+
}
35+
shell("make html")
36+
ff1 <- list.files("txt", pattern="md\\.txt$", full=TRUE)
37+
ff2 <- paste0("_build/html/_sources/", basename(ff1))
38+
file.copy(ff1, ff2, overwrite=TRUE)
39+
}
40+
41+
do_knit <- function(option) {
42+
if (option=="nocache"){
43+
unlink("cache", recursive=TRUE)
44+
} else if (option=="nodata"){
45+
unlink("cache", recursive=TRUE)
46+
unlink("data", recursive=TRUE)
47+
} else if (option=="clean"){
48+
file.remove(list.files(pattern="\\.md"))
49+
file.remove(list.files("txt", pattern="\\.md", full=TRUE))
50+
}
51+
ff <- list.files("_R", pattern='.Rmd$', ignore.case=TRUE, full.names=TRUE)
52+
md <- list.files(".", pattern='.md')
53+
54+
#Do we need this check if we're using caching?
55+
if (length(md) > 0 ) {
56+
stime <- file.info(ff)
57+
stime <- data.frame(f=raster::extension(basename(rownames(stime)), ""), stime = stime$mtime, stringsAsFactors=FALSE)
58+
59+
btime <- file.info(md)
60+
btime <- data.frame(f=raster::extension(basename(rownames(btime)), ""), btime = btime$mtime, stringsAsFactors=FALSE)
61+
62+
m <- merge(stime, btime, by=1, all.x=TRUE)
63+
m[is.na(m$btime), 'btime'] <- as.POSIXct(as.Date('2000-01-01'))
64+
65+
i <- which ( m$btime < m$stime )
66+
ff <- ff[i]
67+
}
68+
69+
if (length(ff) > 0) {
70+
library(knitr)
71+
# Check that folders for output exist
72+
# dir.create('_R/figures/', recursive=TRUE, showWarnings=FALSE)
73+
dir.create('figures/', showWarnings=FALSE)
74+
dir.create('txt/', showWarnings=FALSE)
75+
md <- raster::extension(basename(ff), '.md')
76+
77+
opts_chunk$set(
78+
dev = 'png',
79+
fig.path = 'figures/',
80+
fig.width = 6,
81+
fig.height = 6,
82+
collapse = TRUE
83+
)
84+
85+
for (i in 1:length(ff)) {
86+
print(paste(" ", basename(ff[i])))
87+
knit(ff[i], md[i], envir = new.env(), encoding = 'UTF-8', quiet = TRUE)
88+
purl(ff[i], paste0("txt/", gsub("md$", "md.txt", md[i])), quiet=TRUE)
89+
}
90+
}
91+
}
92+
93+
94+
95+
for (ch in chapter) {
96+
path <- file.path(oldpath, 'source', ch)
97+
setwd(path)
98+
if (cmd == "build") {
99+
do_build(option)
100+
} else {
101+
do_knit(option)
102+
}
103+
print(getwd())
104+
}
105+
setwd(oldpath)
106+
107+

README.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

clean.R

Lines changed: 0 additions & 5 deletions
This file was deleted.

copyRcode.R

Lines changed: 0 additions & 9 deletions
This file was deleted.

copydata.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

knit.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)