使用循环在 Markdown 中参数化作者和标题 [英] Parameterize both Author and Title in Markdown using a loop

查看:58
本文介绍了使用循环在 Markdown 中参数化作者和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要参数化 Markdown 报告的标题和作者.我能够设置标题并从一个循环生成报告,该循环对我的数据集进行子集化,但我无法弄清楚如何将标题与我在数据集中的相应作者相匹配.

例如,我有一组由不同教师教授的课程.我可以得到课程的标题作为降价报告的标题:

for (c in unique((na.omit(data$NAME_OF_THE_COURSE)))){rmarkdown::render('LOCATION_WHATEVER',参数 = 列表(set_title= c),output_file = paste("report_post_", c, '_', Sys.Date(), ".html", sep=''),output_dir = 'LOCATION_WHATEVER')}

但是我无法设置作者,因为我需要另一个循环

for (author in unique((na.omit(data$NAME_OF_INSTRUCTUR))))

有什么建议吗?

解决方案

我几乎总是避免 for 循环,因为 R 的优点之一是你可以

I need to parameterize both the title and the author of a Markdown report. I am able to set the title and generate reports from a loop that subset my dataset but I can't figure out how to match the title with a corresponding author that I've in my dataset.

For instance, I've a set of courses taught by different instructors. I can get the title of the course as the title of the markdown report:

for (c in unique((na.omit(data$NAME_OF_THE_COURSE)))){
 rmarkdown::render('LOCATION_WHATEVER',
                   params = list(set_title= c),
                   output_file =  paste("report_post_", c, '_', Sys.Date(), ".html", sep=''), 
                   output_dir = 'LOCATION_WHATEVER')
}

But I am not able to set the author since I would need another loop where

for (author in unique((na.omit(data$NAME_OF_INSTRUCTUR)))) 

Any suggestion?

解决方案

I almost always avoid for loops, since one of the beauties of R is that you can work over vectors. Instead, I'd use an apply family function from base R or, my preference, a map family function from purrr/tidyverse.

You can do this a few ways, but I went with a nested list. It's a list of course info, where each course is a list of the professor's name and the class name. Using walk, you map over the outer list, take the names from each class, and use those as parameters for render.

Here's the dummy Rmarkdown, with the filename dummy_rmd.Rmd:

Edit: You can use inline R code inside your yaml to set the title and author of the document, as explained in this answer. The items with the inline code need to be after params, so there's something defined already.

---
output: html_document
params:
  prof: "Person 1"
  class: "Class A"
title: "`r params$class`"
author: "`r params$prof`"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
prof <- params$prof
class <- params$class

plot(1:5, runif(5), main = sprintf("Plot for %s taught by %s", class, prof))
```

Then in a script in the same directory:

class_list <- list(
    list(prof = "Person 1", class = "Class A"),
    list(prof = "Person 2", class = "Class B"),
    list(prof = "Person 3", class = "Class C")
)

purrr::walk(class_list, function(class_info) {
    prof <- class_info$prof
    class <- class_info$class
    rmarkdown::render(
        input = "dummy_rmd.Rmd", 
        output_file = sprintf("output_%s_%s.html", prof, class),
        params = list(prof = prof, class = class)
        )
})

This gives me html files, one for each course, named accordingly. HTML output looks like:

这篇关于使用循环在 Markdown 中参数化作者和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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