【问题标题】:knitr/Rmd: Adding title page and text when converting to MS Wordknitr/Rmd:在转换为 MS Word 时添加标题页和文本
【发布时间】:2015-02-28 22:37:21
【问题描述】:

我正在尝试使用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 文档。

【问题讨论】:

  • 您是否考虑过与en.nothingisreal.com/wiki/GPP 一起破解某些东西(不幸的是,仅限 Linux,MacOS)
  • 嗨,本,感谢您的提示;我将仔细研究 GPP。不过,最终,我更喜欢适用于所有操作系统的解决方案。
  • 我认为这很难——你要么最终在 R 中重新实现东西以生成定制的降价(即重新实现 YAML 标记的处理),要么找到一些破解 Word 文件的 pandoc 生成的方法。 (这可能更像是一个pandoc 问题......)stackoverflow.com/questions/15937631/… ; surefoss.org/publishing-publizieren/…
  • pandoc,因此 rmarkdown,let you specify a template file,您可以在其中定义自己的格式。我不确定这是否可以将内容放在单独的页面上
  • 我认为你不能指望 custom 变量在这个阶段起作用,因为模板当前被忽略(超出样式)。然而,那些已经定义的参数(抽象应该是其中之一)应该可以工作。如果没有,请检查您是否拥有最新版本的 pandoc,并尝试直接通过 pandoc 运行转换以查看问题是否与 rmarkdown 相关。

标签: r ms-word knitr pandoc r-markdown


【解决方案1】:

我发现可以自定义 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())

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2015-11-14
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多