【问题标题】:Custom footer using R Markdown 2 and YAML for an HTML file使用 R Markdown 2 和 YAML 的 HTML 文件的自定义页脚
【发布时间】:2014-09-23 14:57:01
【问题描述】:

使用R Markdown 2 和YAML include 语句,我可以分别使用in_headerbefore_bodyafter_body 轻松自定义标题部分、正文部分的开头和结尾,如@中所述987654321@.

---
title: "Habits"
output:
  html_document:
    includes:
      in_header: header.html
      before_body: doc_prefix.html
      after_body: doc_suffix.html
---

如何对footer 部分执行相同操作? (输出应该是 HTML)

here 已针对 PDF 文件提出了类似的问题,而对于 PDF 文件here,还有另一个答案。尽管如此,由于我对 pandoc 的了解太有限,我无法将解决方案转移到 HTML 页脚。

感谢您的宝贵时间!

【问题讨论】:

    标签: r yaml r-markdown


    【解决方案1】:

    这是一种使用 R 手动将 HTML 注入页脚的解决方法。在生成自定义页脚后,我在所有文档中运行它以获取自定义页脚。根据要求,YAML-Pandoc 解决方案仍然是可取的。

    library(stringr)
    library(shiny)      # for HTML function
    

    函数inject_code_after_body_tag 将读取 HTML 文件并在最终正文标记后添加页脚部分。

    inject_code_after_body_tag <- function(file, include) 
    {
      file.lines <- readLines(file, warn = FALSE, encoding = "UTF-8")
      include.lines <- readLines(include, warn = FALSE, encoding = "UTF-8")
      inc <- HTML(paste(include.lines, collapse = "\r\n"))
      l <- str_replace(file.lines,
                       perl("(?<=</body>)"),
                       inc)
      HTML(paste(l, collapse = "\r\n"))
    }
    

    现在,读入文件并从 footer.html 注入代码

    code <- inject_code_after_body_tag(file = "index.html", include = "include/footer.html")  
    

    最后,将新代码写回到index.html文件中

    writeLines(code, "index.html")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多