gpt4 book ai didi

r - 针织/Rmd : Adding title page and text when converting to MS Word

转载 作者:行者123 更新时间:2023-12-01 21:57:39 26 4
gpt4 key购买 nike

我正在尝试使用 rmarkdown 包编写报告,但遗憾的是,我的现场报告习惯通常以 MS Word 文档形式提交。因此,我不能总是依赖 LaTeX 的强大功能,并且必须能够将我的 .Rmd 转换为 MS Word。现在,因为我希望能够从同一源文件创建 PDF 和 MS Word 文件,所以我正在尝试找到一种通用方法来执行此操作。我已经使用 apa6 LaTeX-document 类来处理 PDF。创建 Word 文件时,.Rmd 看起来像这样:

---
title: My title
abstract: This is the abstract.
author: John Doe
affiliation: Unknown
note: Nothing to say.

output:
word_document:
reference_docx: myreference.docx
---

Lorem ipsum.

我可以由此创建一个 Word 文档,但由于显而易见的原因,我的自定义 yaml 变量(例如摘要)将不会在文档中呈现。

基本上,我的问题如下:

创建 Word 文档时,如何在文档正文之前添加标题页(包括作者姓名、单位、作者注释等)和仅包含摘要的页面(“Lorem ipsum”)?这里的重点不是创建分页符(还有其他 open questions on this ),而是 **有没有办法让 pandoc 使用自定义 yaml 变量将它们放置在文档的开头并为其指定样式?

rmarkdown 包提供了 include() 函数,但它仅适用于 HTML 和 PDF 文档。

最佳答案

我发现可以在将 rmarkdown 生成的 Markdown 文件提交给 pandoc 之前对其内容进行自定义(例如添加和修改标题页)使用预处理器转换为 DOCX。假设我们正在尝试在摘要之前添加 YAML 参数 note 中指定的一些信息(同时对摘要的支持已添加到 pandoc 中)。

为此,我们首先需要一个预处理器函数,用于读取输入文件并解析 YAML 前面的内容,并自定义输入文件:

my_pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir, from) {

# Identify YAML front matter delimiters
input_text <- readLines(input_file, encoding = "UTF-8")
yaml_delimiters <- grep("^(---|\\.\\.\\.)\\s*$", input_text)

if(length(yaml_delimiters) >= 2 &&
(yaml_delimiters[2] - yaml_delimiters[1] > 1) &&
grepl("^---\\s*$", input_text[yaml_delimiters[1]])) {
yaml_params <- yaml::yaml.load(paste(input_text[(yaml_delimiters[1] + 1):(yaml_delimiters[2] - 1)], collapse = "\n"))
} else yaml_params <- NULL

# Modify title page
custom_lines <- c(
"NOTE:"
, metadata$note
, "\n\n"
, "# Abstract"
, "\n"
, metadata$abstract
, "\n"
)

## Add modified title page components after YAML front matter
augmented_input_text <- c(custom_lines, input_text[(yaml_delimiters[2] + 1):length(input_text)])

# Remove redundant default abstract
yaml_params$abstract <- NULL

# Add modifications to input file
augmented_input_text <- c("---", yaml::as.yaml(yaml_params), "---", augmented_input_text)
input_file_connection <- file(input_file, encoding = "UTF-8")
writeLines(augmented_input_text, input_file_connection)
close(input_file_connection)

NULL
}

现在我们需要定义一个利用预处理器的自定义格式:

my_word_document <- function(...) {
config <- rmarkdown::word_document(...)

# Preprocessor functions are adaptations from the RMarkdown package
# (https://github.com/rstudio/rmarkdown/blob/master/R/pdf_document.R)
pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir, from = .from) {
# save files dir (for generating intermediates)
saved_files_dir <<- files_dir

args <- my_pre_processor(metadata, input_file, runtime, knit_meta, files_dir, output_dir, from)
args
}

config$pre_processor <- pre_processor
config
}

现在,您可以在渲染 R Markdown 文档时使用自定义格式,如下所示:

rmarkdown::render("./foo/bar.Rmd", output_format = my_word_document())

关于r - 针织/Rmd : Adding title page and text when converting to MS Word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27744830/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com