make Cookbook
2020-03-07
- 使用 RMarkdown 的
child
参数,进行文档拼接。 - 这样拼接以后的笔记方便复习。
- 相关问题提交到 Issue
1 配置
参考 https://stackoverflow.com/questions/32127524/how-to-install-and-use-make-in-windows
Everything 查询 make.exe
,发现在 C:\Rtools\bin\make.exe
,先配置好环境变量就可以用了。
2 示例
参考 https://github.com/pavopax/gists/blob/master/makefile-quick-start.md
all: data process output
data:
Rscript get_datasets.R
Rscript get_extra_files.R
process:
Rscript process.R
output:
Rscript write_plots.R
Rscript write_tables.R
python app.py
type
make data
to run your “get data” scripts typemake output
to only run your “output” script(s) typemake all
to rerun the entire analysis, which consists of the pieces specified in the first line (data
,process
,output
)all
,data
,process
,output
, etc are called “targets”
参考 https://github.com/pavopax/gists/blob/master/assets/makefile-example-1.bash
all: clean data process
clean:
rm -rf data
rm -rf data.tar
rm -rf data.tar.gz
data: clean
scp user@unix:/file/location/data.tar.gz .
gunzip data.tar.gz
tar -xvf data.tar
process:
mkdir -p code/logs
Rscript code/process.R
load:
python app.py
tweak:
git commit -m 'Tweak'
git push
这是一个模板,其实这个过程可以在 R 里面实现,但是 R 要用其他语言的时候,要用system()
,有点麻烦。
https://stat545.com/automation-overview.html
可以参考 https://github.com/JiaxiangBU/learn_rmd/blob/master/Makefile
3 usethis use_make
usethis::use_make()
# h/t to @jimhester and @yihui for this parse block:
# https://github.com/yihui/knitr/blob/dc5ead7bcfc0ebd2789fe99c527c7d91afb3de4a/Makefile#L1-L4
# Note the portability change as suggested in the manual:
# https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Writing-portable-packages
PKGNAME = `sed -n "s/Package: *\([^ ]*\)/\1/p" DESCRIPTION`
PKGVERS = `sed -n "s/Version: *\([^ ]*\)/\1/p" DESCRIPTION`
all: check
build:
R CMD build .
check: build
R CMD check --no-manual $(PKGNAME)_$(PKGVERS).tar.gz
install_deps:
Rscript \
-e 'if (!requireNamespace("remotes") install.packages("remotes")' \
-e 'remotes::install_deps(dependencies = TRUE)'
install: install_deps build
R CMD INSTALL $(PKGNAME)_$(PKGVERS).tar.gz
clean:
@rm -rf $(PKGNAME)_$(PKGVERS).tar.gz $(PKGNAME).Rcheck
4 更新 R 项目
直接可以再其他 git bash (RStudio里面的) 就可以更新了。 或者说不需要打开 RStudio 项目直接用 make 去更新。
5 q() 失效
Rscript -e "q(save = 'no')"
加在 make 里面没用,因为在 Bash 中,这是新开了一个进程,不影响当前的 RStudio。
6 missing separator
参考 https://blog.csdn.net/u010299133/article/details/83895299
在相应的行前tab键,很多时候可能是加了空格键。
7 处理乱码
不要 .R
写文字,会乱码,用 Rmd
。
8 单独调用 make 命令测试
一条条测试,这样比较快。 这主要是一个思路问题。
8.1 检查 make 执行结果
[1] "toc:"
[2] ""
[3] "\tRScript analysis/build_toc.R"
[4] ""
[5] "render:"
[6] ""
[7] "\tRScript analysis/build_readme.R"
[8] ""
[9] "clean:"
[10] ""
[11] "\trm README.html"
[12] ""
[13] "push:"
[14] "\tgit add ."
[15] "\tgit commit -m 'update, use `push` in Makefile'"
[16] "\tgit push"
[17] ""
[18] "README: toc render clean push"
[19] ""
[20] "all: README"
[21] ""
[22] "copy:"
[23] ""
[24] "\tRScript code/copy-md.R"
README: toc render clean push
先不执行 push
,手动执行
make toc render clean
查看结果。
看下目录更新的效果,再选择make push
附录
8.2 示例
run_model:
RScript code/write_report.R
RScript code/run-dtm-data.R
jupyter nbconvert --to script doc2vec_slice_kmeans.ipynb
python doc2vec_slice_kmeans.py
rm doc2vec_slice_kmeans.py
RScript code/run-dtm-output.R
RScript code/data-validation.R
RScript code/write_report_final.R
rm copy.Rmd
rm analysis/dtm-data.md
rm analysis/data-validation.md
rm analysis/data-validation.html
mv copy.docx output/report.docx
readme:
Rscript code/build-readme.R
push:
Rscript code/push.R
copy:
Rscript code/copy-md.R