R Markdownの
オリジナルフォーマットを
作ろう

mini_document
floating_toccode_foldingcode_download (開発版)YAMLフロントマターに書くアレの
オリジナル版を作れるようになろう
#TokyoR のスライド作るにあたってR Markdownユーザーに聞きたい。YAMLフロントマターの
— atusy (@Atsushi776) May 14, 2020
output: html_document
のhtml_documentは関数だと
YAMLシンプルにしたいやん?
R Markdownの内部とテンプレート開発 by kazutan氏
によると
だいたいYAMLフロントマター指定すれば済むからフォーマット作らなくていいよ(意訳)
らしいが……
output:
html_document:
toc: TRUE
toc_float: TRUE
number_sections: TRUE
code_folding: show
code_download: TRUE参考: 2019年版Rパッケージ開発の手引き by uri氏
フルスクラッチが怖いなら
atusy/tokyor85down
を弄るところから始めよう
インストールするには
remotes::install_github("atusy/tokyor85down)
html_documentとかは関数!
ラッパー関数もoutputに指定可!
html_documentは関数だよYAML使わないなら↓な感じ。
output: tokyor85down::rich_html_document
で済むようになるぞ!!
tokyor85down::rich_html_document
#> function(# 後から無効にする可能性もあるなら、
#> #html_documentに渡す引数は
#> # bodyに直接書かず、引数として書いておくと良い
#> toc = TRUE,
#> toc_float = TRUE,
#> number_sections = TRUE,
#> code_folding = "show",
#> code_download = TRUE,
#> ...) {
#> rmarkdown::html_document(
#> toc = toc,
#> toc_float = toc_float,
#> number_sections = number_sections,
#> code_folding = code_folding,
#> code_download = code_download,
#> ...
#> )
#> }
#> <bytecode: 0x556eeaa24600>
#> <environment: namespace:tokyor85down>基本編(絶対パスOK)
HTMLでself_contained = FALSEを使わない限りは、基本編で十分なはず。
ではYAMLでreference_docxを指定しましょう。
コピペ撲滅委員会
「プロジェクトごとにファイルコピペとか許せん」
必要なファイルを
パッケージのinstディレクトリ以下の
好きなところに保存しておく
応用編(絶対パスNG)
たとえば
html_document関数にオリジナルの
cssを指定したい
self_contained: FALSEな時は
プロジェクトディレクトリにCSSを
自動でコピーして欲しい
パッケージ提供のCSSを無視してしまう
パッケージディレクトリ下にある
CSSを絶対パスで参照
依存したいCSSとかJavaScriptを
self_containedが
TRUEなら
FALSEなら
lib_dir引数で指定した場所にコピーしもっと自在に前処理とか後処理したくなったら
rmarkdown::output_format関数を使おう
詳しくは↓
チャンクを実行しても一切出力に反映しない
ジョークフォーマットに魔改造できる
fmt <- rmarkdown::output_format(NULL, NULL)
str(fmt)
#> List of 11
#> $ knitr : NULL
#> $ pandoc : NULL
#> $ keep_md : logi FALSE
#> $ clean_supporting : logi TRUE
#> $ df_print : NULL
#> $ pre_knit : NULL
#> $ post_knit : NULL
#> $ pre_processor : NULL
#> $ intermediates_generator: NULL
#> $ post_processor : NULL
#> $ on_exit : NULL
#> - attr(*, "class")= chr "rmarkdown_output_format"ただしbase_formatを除く
html_fmt <- html_document()
identical(nm_fmt, names(html_fmt))
#> [1] TRUE
str(html_fmt)
#> List of 11
#> $ knitr :List of 5
#> ..$ opts_knit : NULL
#> ..$ opts_chunk :List of 5
#> .. ..$ dev : chr "png"
#> .. ..$ dpi : num 96
#> .. ..$ fig.width : num 7
#> .. ..$ fig.height: num 5
#> .. ..$ fig.retina: num 2
#> ..$ knit_hooks : NULL
#> ..$ opts_hooks : NULL
#> ..$ opts_template: NULL
#> $ pandoc :List of 6
#> ..$ to : chr "html"
#> ..$ from : chr "markdown+autolink_bare_uris+tex_math_single_backslash+smart"
#> ..$ args : chr [1:10] "--email-obfuscation" "none" "--self-contained" "--standalone" ...
#> ..$ keep_tex : logi FALSE
#> ..$ latex_engine: chr "pdflatex"
#> ..$ ext : NULL
#> $ keep_md : logi FALSE
#> $ clean_supporting : logi TRUE
#> $ df_print : chr "default"
#> $ pre_knit :function (...)
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 115 5 117 5 5 5 4614 4616
#> .. ..- attr(*, "srcfile")=Classes 'srcfilealias', 'srcfile' <environment: 0x556ee8977758>
#> $ post_knit :function (...)
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 115 5 117 5 5 5 4614 4616
#> .. ..- attr(*, "srcfile")=Classes 'srcfilealias', 'srcfile' <environment: 0x556ee8977758>
#> $ pre_processor :function (...)
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 115 5 117 5 5 5 4614 4616
#> .. ..- attr(*, "srcfile")=Classes 'srcfilealias', 'srcfile' <environment: 0x556ee8977758>
#> $ intermediates_generator:function (original_input, intermediates_dir)
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 118 30 122 3 30 3 1793 1797
#> .. ..- attr(*, "srcfile")=Classes 'srcfilealias', 'srcfile' <environment: 0x556ee97f1e28>
#> $ post_processor :function (metadata, input_file, output_file, clean, verbose)
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 124 21 172 3 21 3 1799 1847
#> .. ..- attr(*, "srcfile")=Classes 'srcfilealias', 'srcfile' <environment: 0x556ee97f1e28>
#> $ on_exit :function ()
#> ..- attr(*, "srcref")= 'srcref' int [1:8] 182 3 185 3 3 3 4681 4684
#> .. ..- attr(*, "srcfile")=Classes 'srcfilealias', 'srcfile' <environment: 0x556ee8977758>
#> - attr(*, "class")= chr "rmarkdown_output_format"output_formatで改造するのか?html_document()がリストなら直接改造もできる
しかしoutput_formatは、改造した部分を良い感じに base_formatと結合してくれる
ただし、keep_mdとclean_supported(self_contained相当)を除く
keep_md引数とself_contained引数は、 base_formatではなく、output_formatに食わせること。rmarkdown:::merge_output_formatsの実装を見てネ。
File -> New File -> R Markdown...
から選べるテンプレを作るには
GOTO
参考
```を探すとか。Para関数CodeBlock関数などfunction Link(el)
-- targetが指定済みならそのまま
if el.attributes.target then
return(el)
end
-- 未指定なら_blankにする
el.attributes.target = "_blank"
return(el)
end本資料でも活躍
pandoc_args引数に指定
output:
html_document:
pandoc_args:
- "--lua-filter"
- "filters.lua"