Skip to content

Commit fae85a0

Browse files
committed
Improving support for R Markdown.
1. Adding `tools/chunk-options.R` with support for Pandoc-based conversion of R Markdown to plain Markdown. 2. Modifying `Makefile` to handle any R Markdown files that are present. Changes have been checked by @jdblischak in the `r-novice-inflammation` project.
1 parent a005071 commit fae85a0

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

Makefile

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Files.
2-
MARKDOWN = $(wildcard *.md)
3-
EXCLUDES = README.md LAYOUT.md FAQ.md DESIGN.md
4-
SRC_PAGES = $(filter-out $(EXCLUDES), $(MARKDOWN))
5-
DST_PAGES = $(patsubst %.md,%.html,$(SRC_PAGES))
1+
# R Markdown files.
2+
SRC_RMD = $(wildcard ??-*.Rmd)
3+
DST_RMD = $(patsubst %.Rmd,%.md,$(SRC_RMD))
64

7-
# Pandoc filters
8-
FILTERS = $(wildcard tools/filters/*.py)
5+
# All Markdown files (hand-written and generated).
6+
SRC_MD = $(wildcard *.md) $(DST_RMD)
7+
DST_HTML = $(patsubst %.md,%.html,$(SRC_MD))
8+
9+
# All outputs.
10+
DST_ALL = $(DST_HTML)
911

1012
# Inclusions.
1113
INCLUDES = \
@@ -14,11 +16,14 @@ INCLUDES = \
1416
-Vfooter="$$(cat _includes/footer.html)" \
1517
-Vjavascript="$$(cat _includes/javascript.html)"
1618

19+
# Chunk options for knitr (used in R conversion).
20+
R_CHUNK_OPTS = tools/chunk-options.R
21+
1722
# Default action is to show what commands are available.
1823
all : commands
1924

2025
## preview : Build website locally for checking.
21-
preview : $(DST_PAGES)
26+
preview : $(DST_ALL)
2227

2328
# Pattern for slides (different parameters and template).
2429
motivation.html : motivation.md _layouts/slides.html
@@ -27,36 +32,38 @@ motivation.html : motivation.md _layouts/slides.html
2732
-o $@ $<
2833

2934
# Pattern to build a generic page.
30-
%.html : %.md _layouts/page.html $(FILTERS)
35+
%.html : %.md _layouts/page.html
3136
pandoc -s -t html \
3237
--template=_layouts/page \
3338
--filter=tools/filters/blockquote2div.py \
3439
--filter=tools/filters/id4glossary.py \
3540
$(INCLUDES) \
3641
-o $@ $<
3742

38-
# Pattern to convert R Markdown to Markdown.
39-
%.md: %.Rmd $(R_CHUNK_OPTS)
40-
Rscript -e "knitr::knit('$$(basename $<)', output = '$$(basename $@)')"
41-
4243
## unittest : Run unit test (for Python 2 and 3)
4344
unittest: tools/check.py tools/validation_helpers.py tools/test_check.py
4445
cd tools/ && python2 test_check.py
4546
cd tools/ && python3 test_check.py
4647

48+
# Pattern to convert R Markdown to Markdown.
49+
%.md: %.Rmd $(R_CHUNK_OPTS)
50+
Rscript -e "knitr::knit('$$(basename $<)', output = '$$(basename $@)')"
51+
4752
## commands : Display available commands.
4853
commands : Makefile
4954
@sed -n 's/^##//p' $<
5055

5156
## settings : Show variables and settings.
5257
settings :
53-
@echo 'SRC_PAGES:' $(SRC_PAGES)
54-
@echo 'DST_PAGES:' $(DST_PAGES)
58+
@echo 'SRC_RMD:' $(SRC_RMD)
59+
@echo 'DST_RMD:' $(DST_RMD)
60+
@echo 'SRC_MD:' $(SRC_MD)
61+
@echo 'DST_HTML:' $(DST_HTML)
5562

5663
## clean : Clean up temporary and intermediate files.
5764
clean :
5865
@rm -rf $$(find . -name '*~' -print)
5966

6067
# very-clean : Remove generated HTML.
6168
very-clean :
62-
@rm -f $(DST_PAGES)
69+
@rm -f $(DST_MD)

tools/chunk-options.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# These settings control the behavior of all chunks in the novice R materials.
2+
# For example, to generate the lessons with all the output hidden, simply change
3+
# `results` from "markup" to "hide".
4+
# For more information on available chunk options, see
5+
# http://yihui.name/knitr/options#chunk_options
6+
7+
library("knitr")
8+
opts_chunk$set(tidy = FALSE, results = "markup", comment = NA,
9+
fig.align = "center")
10+
11+
# The hooks below add html tags to the code chunks and their output so that they
12+
# are properly formatted when the site is built with jekyll.
13+
hook_in <- function(x, options) {
14+
stringr::str_c("\n\n~~~{.r}\n",
15+
paste0(x, collapse="\n"),
16+
"\n~~~\n\n")
17+
}
18+
19+
hook_out <- function(x, options) {
20+
stringr::str_c("\n\n~~~{.output}\n",
21+
paste0(x, collapse="\n"),
22+
"\n~~~\n\n")
23+
}
24+
25+
knit_hooks$set(source = hook_in, output = hook_out, warning = hook_out,
26+
error = hook_out, message = hook_out)

0 commit comments

Comments
 (0)