Using R markdown and knitr: Possible to get R objects interpreted in YAML

霸气de小男生 提交于 2019-12-07 11:57:17

问题


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.

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?

A brief example:

---
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
```

回答1:


It's not exactly YAML-related, and limited to latex, but maybe this helps. Sorry, the thread is in German, but maybe the code is clear enought.

You can pick items for the abstract as you create your report; the items are written out to a temporary file similar to method use in bibtex and biblatex, and merged to the start and end (for appendix) in a second step.

As a package, you can download it from here .




回答2:


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)

Create Variables

Separate the variables into a new file (e.g., 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?

Create Script

Create a script to load knitr, yaml, and the variables (e.g., knitr.sh), when given a file in Markdown format:

#!/bin/bash

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

Reference Variables

Use R statements (inline or otherwise) to reference the source document variables (document.md):

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

Run Script

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

./knitr.sh document.md

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

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.



来源:https://stackoverflow.com/questions/25813420/using-r-markdown-and-knitr-possible-to-get-r-objects-interpreted-in-yaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!