使用R markdown和knitr:可以在YAML中解释R对象 [英] Using R markdown and knitr: Possible to get R objects interpreted in YAML

查看:235
本文介绍了使用R markdown和knitr:可以在YAML中解释R对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用knitrR markdown和自定义的LaTeX文件编写手稿.摘要被设置为YAML内容的一部分.这很有效,除了我希望包含在摘要中的结果在文档的后面生成.

I am using knitr, R markdown, and a custom LaTeX file to write a manuscript. The abstract is set as part of the YAML frontmatter. This works great, except that the results that I would like to include in the abstract are generated later in the document.

对我来说,在YAML中包含摘要是很有意义的,我宁愿保持这种方式.关于采用摘要的方式有什么想法,可以利用本文稍后进行的计算吗?

Having the abstract in the YAML makes some sense to me and I would prefer to keep it this way. Any thoughts on a way to have the abstract take advantage of calculations that happen later in the document?

一个简单的例子:

---
title: 'How do I interpret R objects in YAML that are created in the document?'
author: Jeff Hollister
output:
  pdf_document:
    fig_caption: yes
    keep_tex: yes
    number_sections: yes
  html_document: null
  word_document: null
fontsize: 11pt
documentclass: article
abstract:  This is a test.  Is this `r mean(x)` working?
---

This is a test of outputing a pdf, with R code interpretted in the YAML front mater.  Use case 
for this would be a manuscript, with abstract as part of YAML, and some of the results in the     
abstract being calculated in the body of the Rmd.  For instance:

```{r}
x<-rnorm(100)
mn<-mean(x)
mn
```

推荐答案

要求

安装R,knitr和yaml:

Requirements

Install R, knitr, and yaml:

sudo su -
apt-get install pandoc
apt-get install r
r
url <- "http://cran.us.r-project.org"
install.packages('knitr', repos=url)
install.packages('yaml', repos=url)

创建变量

将变量分成一个新文件(例如,v.yaml):

title: 'How do I interpret R objects in YAML that are created in the document?'
author: Jeff Hollister
output:
  pdf_document:
    fig_caption: yes
    keep_tex: yes
    number_sections: yes
  html_document: null
  word_document: null
fontsize: 11pt
documentclass: article
abstract:  This is a test.  Is this `r mean(x)` working?

创建脚本

在以Markdown格式提供文件时,创建一个脚本以加载knitr,yaml和变量(例如knitr.sh):

#!/bin/bash

R -e \
"library(knitr); library(yaml); v <- yaml.load_file('v.yaml'); knit('$1')"

参考变量

使用R语句(内联或其他)引用源文档变量(document.md):

# `r v$title`
Author: `r v$author`

运行脚本

通过运行以下脚本来生成经编织程序处理的Markdown文件:

Run Script

Generate a knitr-processed Markdown file by running the script:

./knitr.sh document.md

查看输出

文件document.txt(重新命名是留给读者的问题)应包含以下Markdown:

View Output

The file document.txt (renaming is a problem left for the reader) should contain the following Markdown:

# How do I interpret R objects in YAML that are created in the document?
Jeff Hollister

附录

如果需要pandoc处理变量,则在shell脚本magick中将变量插入到生成文件的顶部是相当琐碎的.如果将YAML变量用于pandoc的模板,建议您跳过该路由,而改用R变量.

Addendum

It is fairly trivial with shell script magick to insert the variables at the top of the generated file, should they be needed for processing by pandoc. If using YAML variables for pandoc's templates, I'd recommend skipping that route and using R variables instead.

这篇关于使用R markdown和knitr:可以在YAML中解释R对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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