r - 如何在 rmarkdown 中打印 git 历史记录?

标签 r git knitr r-markdown

我正在使用 rmarkdown 编写分析报告,并希望有一个“文档版本”部分,其中我将指出文档的不同版本和所做的更改。

我没有手动记下来,而是考虑使用 git history 并将其自动插入到 markdown 文档中(将其格式化为表格)。

我该怎么做?可能吗?

最佳答案

安装 git2r, https://github.com/ropensci/git2r然后你可以做这样的事情:

> r = repository(".")
> cs = commits(r)
> cs[[1]]
[02cf9a0] 2017-02-02: uses Rcpp attributes instead of inline

所以现在我有一个关于这个 repo 的所有提交的列表。您可以根据自己的需要从每个提交和格式中获取信息到您的文档中。

> summary(cs[[1]])
Commit:  02cf9a0ff92d3f925b68853374640596530c90b5
Author:  barryrowlingson <b.rowlingson@gmail.com>
When:    2017-02-02 23:03:17

     uses Rcpp attributes instead of inline

11 files changed, 308 insertions, 151 deletions
DESCRIPTION           | -  0 +  2  in 2 hunks
NAMESPACE             | -  0 +  2  in 1 hunk
R/RcppExports.R       | -  0 + 23  in 1 hunk
R/auxfunctions.R      | -  1 +  1  in 1 hunk
R/skewt.r             | -  0 +  3  in 1 hunk
R/update_params.R     | -  1 +  1  in 1 hunk
R/update_params_cpp.R | -149 +  4  in 2 hunks
src/.gitignore        | -  0 +  3  in 1 hunk
src/RcppExports.cpp   | -  0 + 76  in 1 hunk
src/hello_world.cpp   | -  0 + 13  in 1 hunk
src/update_params.cpp | -  0 +180  in 1 hunk

因此,如果您只想要时间和提交消息,那么您可以从对象中获取它。

> cs[[3]]@message
[1] "fix imports etc\n"
> cs[[3]]@committer@when
2017-01-20 23:26:20

我不知道是否有适合这些的访问器函数,而不是使用 @-notation 来获取插槽。需要多读一点文档...

您可以通过这种方式从您的提交中创建一个数据框:

> do.call(rbind,lapply(cs,function(cs){as(cs,"data.frame")}))

将日期转换为 POSIXct 对象,这很好。从数据框创建 Markdown 表应该是微不足道的!

关于r - 如何在 rmarkdown 中打印 git 历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42038262/

相关文章:

r - 更改 R 中数据框列表中的列名称

git - 为什么我在源文件中出现 CRLF 换行符,尽管我在使用 Git 和 IDEA 的纯 Mac+Linux 环境中?

git - 如何让 -p 成为 git stash show 的默认设置?

latex - 通过 rmarkdown 交叉引用章节编号或名称

r - 如何在 knitr 文档中设置图形参数 (par()) 和结构选项 (strOptions())?

r - 当我使用 knitr 和 RStudio 在 Windows 上生成 pdf 文件时无法安装 framed.sty

如果列中没有日期,则 read.xlsx 读取日期错误

R 以交互方式选择数据框的行

r - ggplot2 增加堆叠文本标签之间的间距

linux - 创建一个 git 预接收 Hook