使用用 R Markdown 的 YAML 编写的代码添加徽标图像

Adding a logo image with code written in R Markdown's YAML

我有一个 R Markdown 文件(a.k.a。主文件)。我想通过仅在主文件的 YAML 中编写代码或在 YAML 中引用第二个文件来在页面顶部添加徽标(例如,“logo.html”是第二个文件在主文件的 YAML 中引用)。我不想要块中或 body 中的任何解决方案。徽标代码不能通过将代码放在标题中来破解。我更喜欢用 R、HTML 和 CSS 编码的解决方案。徽标不能掩盖标题。徽标将在多种尺寸的显示器上显示,因此请避免 position:absolute。由于数据 table 中有大量列,因此可能会出现大量横向滚动。假设所有文件都在 1 个文件夹工作目录中。

这是我目前为止尝试过的方法。

第一次尝试:

Main file contains this YAML --
---
title: "test_knit"
output: 
  html_document:
    includes:
      before_body: logo.html
---

Second file named logo.html contains --
<img src="logo.png" style="position:relative;top:50%"/>

Third file is the logo in PNG format.

我的第二次尝试:

Main file contains this YAML --
---
title: "test_knit"
output: 
  html_document:
    includes:
      before_body: logo.html
---

Second file named logo.html is an html file that was converted from a PNG. 

我尝试渲染 R Markdown 文件并能够成功编织,但它们不显示徽标。我可以看到 tables、评论和图表,但徽标没有出现。

以下对我有用,在 Windows 和(类似)也在 Linux 上。我还遇到了找不到带有徽标的文件的问题,所以我将 html 页眉、页脚和图像放在绝对路径中(在 Windows 上),并在 Linux:

---
pagetitle: My page title
lang: en
output:
  html_document:
    include:
      before_body: /resources/header.html
      after_body:  /resources/footer.html
runtime: shiny
---

我必须补充一点,这适用于闪亮的运行时。

编辑

刚刚测试:没有闪亮的服务器,所有部分都可以在工作文件夹中,includeincludes 都可以工作:

---
pagetitle: My page title
lang: en
output:
  html_document:
    include:
      before_body: header.html
      after_body:  footer.html
---

这里有一个示例 header.html 文件:

<body>
    <p style="background-color: #002557; height: 80px;">
      <a href="https://foo.de/">
        <img src="left_logo.png" alt="Left Logo"
          title="Left Logo" style="width: 125px; height: 36px;  margin-top: 22px; margin-left: 4%;">
      </a>
      
       <a href="https://foo.de/bar/">
        <img src="right_logo.png" alt="Right Logo"
          title="Right Logo" style="height: 48px;  margin-top: 16px; float: right; margin-right: 4%;">
      </a>
    </p>
</body>

欢迎改进。