R Markdown带有嵌入字段的可重复纯文本

时间:2018-03-10 13:20:54

标签: r markdown r-markdown reporting

我正在制作一份结构化报告。这就是我希望输出包含一些嵌入了R的模板文本。示例部分:

*ID:* **`r bl$ID[41]`**  *CR Name:* **`r bl$Name[41]`**  
*Business Unit:* **`r bl$BusinessUnit[41]`**  
*Business POC:* **`r bl$BusinessContact[41]`**   
*Current State:* **`r bl$Status[41]`**  
*Analyst:* **`r bl$Analyst[41]`**  
*Developer:* **`r bl$Developer[41]`**  
*QA Tester:* **`r bl$QATester[41]`**  
*UAT Tester:* **`r bl$UAT[41]`**  

*Test Environment:* **`r bl$TestBed[41]`**  

**Description:**  
`r bl$Description[41]`  

**Comments:**  
`r bl$Comments[41]`  

以这种方式插入单个数据元素是一件简单的事情(在方括号中对值进行硬编码),但我一直在寻找和关闭几天,以便为每个结构产生多个不同值的方法模板。

也就是说,我正在寻找一种方法,在改变方括号中的值时重复模板文本。

生成表格有很多材料,但我无法弄清楚如何应用这种情况。我错过了一些明显的东西吗你有没有做过这样的事情?

感谢您的关注。

这是我的代码。当然,必须调整setwd()。我提供了一行数据和标题。

---
title: "TableExperimental"
output: 
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
setwd("~/Library/Mobile Documents/com~apple~CloudDocs/Documents")
```

```{r status_chart, echo=FALSE, warning=FALSE, output="asis"}
library("data.table")
library("glue")

bl <- fread("dummyCRs.tab")

bl$DateCreated <- as.Date(bl$DateCreated, "%Y-%m-%d")
bl$BacklogRank[is.na(bl$BacklogRank)] <- "Not Yet Ranked"
bl$Priority <- factor(bl$Priority, levels = c("Not Yet Ranked", "5","4","3","2","1"))
bl$Status <- factor(bl$Status, levels = c("unknown", "draft", "analysis", "development","unit test", "QA", "UAT", "deployed", "closed", "withdrawn"))
bl$BusinessUnit <- factor(bl$BusinessUnit)
bl$TestBed <- factor(bl$TestBed)
bl$CurrentSprint <- factor(bl$CurrentSprint)
bl$Developer <- factor(bl$Developer)
bl$Analyst <- factor(bl$Analyst)
bl$QATester <- factor(bl$QATester)
bl$UATtester <- factor(bl$QATester)    

glue('ID: {bl$ID[41]}   Name: {bl$Name[41]}')
glue('Date created: {bl$DateCreated[41]}')
glue('Business Unit: {bl$BusinessUnit[41]}  Contact: {bl$BusinessContact[41]}')
glue('Analyst: {bl$Analyst[41]}')
glue('Developer: {bl$Developer[41]}')
glue('QA Tester: {bl$QATester[41]}')
glue('UAT: {bl$UAT[41]}')
glue('')
glue('Description:')
glue('{bl$Description[41]}')
glue('Comments:')
glue('{bl$Comments[41]}')
glue('')
glue('')
```


*ID:* **`r bl$ID[41]`**  *CR Name:* **`r bl$Name[41]`** 
<!-- *Business Unit:* **`r bl$BusinessUnit[41]`**
<!-- *Business POC:* **`r bl$BusinessContact[41]`**
<!-- *Current State:* **`r bl$Status[41]`**
<!-- *Analyst:* **`r bl$Analyst[41]`**
<!-- *Developer:* **`r bl$Developer[41]`**
<!-- *QA Tester:* **`r bl$QATester[41]`**
<!-- *UAT Tester:* **`r bl$UAT[41]`**

<!-- *Test Environment:* **`r bl$TestBed[41]`** 

<!-- **Description:**
<!-- `r bl$Description[41]`

<!-- **Comments:**
<!-- `r bl$Comments[41]`

最小数据集:

ID  DateCreated Name    CRDocURL    Status  Description BusinessUnit    CurrentSprint   BacklogRank Comments    BusinessContact Developer   Analyst QATester    UAT TestBed
525 2017-09-03  CR525_ponvds    CR Doc  development At vel nobis comprehensam, pro ea agam intellegat repudiandae, no indoctum partiendo usu. Expetendis reformidans no quo, te autem propriae sed. Duo fierent qualisque omittantur ad,    Accounting  TRUE        Quocumque igitur a uobis deieci oculos, partim ignaua segnities partim callidus liuor occurrit, ut contumeliam uideatur diuinis tractatibus inrogare    McGoo, Quincy   Neff, Walter    Dietrichson, Phyllis    Schmoe, Joe Keyes, Bart DVLP10

1 个答案:

答案 0 :(得分:0)

您可以使用glue_data()功能。这是一个使用最小数据集的示例。

---
title: "TableExperimental"
output: word_document
---

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

```{r status_chart, echo=FALSE, warning=FALSE, results="asis"}
library(glue)
library(magrittr)

bl <- data.frame(
  ID = 525L, 
  DateCreated = "2017-09-03", 
  Name = "CR525_ponvds", 
  CRDocURL = "CR Doc", 
  Status = "development", 
  Description = "At vel nobis comprehensam, pro ea agam intellegat repudiandae, no indoctum partiendo usu. Expetendis reformidans no quo, te autem propriae sed. Duo fierent qualisque omittantur ad,", 
  BusinessUnit = "Accounting", 
  CurrentSprint = TRUE, 
  BacklogRank = NA, 
  Comments = "Quocumque igitur a uobis deieci oculos, partim ignaua segnities partim callidus liuor occurrit, ut contumeliam uideatur diuinis tractatibus inrogare", 
  BusinessContact = "McGoo, Quincy", Developer = "Neff, Walter", 
  Analyst = "Dietrichson, Phyllis", QATester = "Schmoe, Joe", 
  UAT = "Keyes, Bart", 
  TestBed = "DVLP10",
  stringsAsFactors = FALSE
)

bl %>%
  glue_data(
    'ID: {ID}   Name: {Name}  \n',
    'Date created: {DateCreated}  \n',
    'Business Unit: {BusinessUnit}  Contact: {BusinessContact}  \n', 
    'Analyst: {Analyst}  \n', 
    'Developer: {Developer}  \n', 
    'QA Tester: {QATester}  \n', 
    'UAT: {UAT}  \n  \n', 
    'Description:  \n', 
    '{Description}  \n', 
    'Comments:  \n', 
    '{Comments}'
  )
```