YAML 中用于 rmarkdown 的内联 R 代码不运行 [英] Inline R code in YAML for rmarkdown doesn't run

查看:149
本文介绍了YAML 中用于 rmarkdown 的内联 R 代码不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在让 rmarkdown 运行文件之前,我试图在 YAML 前端运行内联 R 代码.但是它对我不起作用.举个例子:

I'm trying to run inline R code in the YAML front matter before getting rmarkdown to run the file. However it isn't working for me. Here's an example:

---
title: "**Title**"
classoption: xcolor=dvipsnames
output:
  beamer_presentation:
      slide_level: 2
      pandoc_args: [
        "--bibliography", "`r paste('path/to/bib')`"
        ]
---

<!-- slide 1 -->
## Intro ##

抛出错误:

pandoc-citeproc: could not find `r paste('path/to/bib')`

这是一个简单的例子,但突出了我的主要问题.如何让 rmarkdown 在 YAML 前端运行内联 R 代码?

This is a simple example, but highlights my main problem. How do I get rmarkdown to run the inline R code in the YAML front matter?

这是一个与这些问题类似的问题:

It is a similar problem to these questions:

推荐答案

我就是这样解决的.我从 RStudio 编织.奇怪的是,我不得不对日期和 csl 字段使用一种解决方案,对参考书目字段使用不同的解决方案.!expr 在 date 或 csl 行中不起作用(对我而言).并且引用的 r 代码在参考书目行中不起作用(对我而言).我在一个包(inst/docs 文件夹)中有参考书目和 csl 文件.rmarkdown 文件(不属于该包的一部分)使用这些文件.

This is how I solved this. I knit from RStudio. Curiously, I had to use one solution for the date and csl fields and a different solution for the bibliography field. !expr did not work in the date or csl lines (for me). And quoted r code didn't work in the bibliography line (for me). I have the bibliography and csl files in a package (inst/docs folder). rmarkdown files, which are not part of that package, use those.

---
title: "Title"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output: html_document
bibliography: !expr system.file("docs", "my.bib", package = "MyPackage")
csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
---

# Introduction

Yada yada [@MyRef04].

# References

my.bib 是带有 MyRef04 的 BibTex 文件.csl 是样式文件

my.bib is the BibTex file with MyRef04. csl is the style file

在这种情况下,一个人维护一个包含数据、代码、参考书目等的包.包编写者可能不知道的其他人从 GitHub 安装该包并编写或运行使用该包的 rmarkdown 文件.用户几乎肯定不使用 Git 或 GitHub,我不希望他们在从 GitHub 安装包后必须下载任何额外的文件.

This is a situation where one person maintains a package which has data, code, bibliography, etc. Others, potentially unknown to the package writer, install that package from GitHub and write or run rmarkdown files that use the package. The users almost certainly do not use Git or GitHub and I don't want them to have to download any extra files after installing the package from GitHub.

更新:发布上述内容后,我碰巧从GitHub安装了markdown,因为我需要开发版本中的一些东西.使用 GitHub 上 rmarkdown 的1.7.5"版本,您可以在参考书目行中使用 r 代码:

Update: After posting the above, I happened to install markdown from GitHub because I needed something in the development version. With version ‘1.7.5’ of rmarkdown on GitHub you can use r code in the bibliography line:

---
title: "Title"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output: html_document
bibliography: '`r system.file("docs", "my.bib", package = "MyPackage")`'
csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
---

从 GitHub 安装 rmarkdown

To install rmarkdown from GitHub

library(devtools)
install_github("rstudio/rmarkdown")

这篇关于YAML 中用于 rmarkdown 的内联 R 代码不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆