如何通过循环从rmd文件创建单独的摘要,如PDF报告?

时间:2017-09-28 05:18:33

标签: r pdf r-markdown

我是RMarkdown的新手,我正在尝试使用数据框架中的地图生成单独的pdf报告,但输出是基于给定的具体答案的评估问题和建议的形式。我试过寻找一些例子来帮助无济于事。

以下是我使用的数据框的子集,如下所示

      mill_name latitude longitude       theme section                          question               remark
      <chr>    <dbl>     <dbl>       <chr>   <dbl>                             <chr>                <chr>
1    Mill A 3.955042  102.7211 environment     1.1 Do you have issue 1 in your area? Refer to Guideline 1
2    Mill A 3.955042  102.7211 environment     1.2 Do you have issue 2 in your area? Refer to Guideline 2
3    Mill A 3.955042  102.7211      social     1.3 Do you have issue 3 in your area? Refer to Guideline 3
4    Mill A 3.955042  102.7211      social     1.4 Do you have issue 4 in your area? Refer to Guideline 4
5    Mill B 1.961030  103.4140 environment     1.1 Do you have issue 1 in your area? Refer to Guideline 1
6    Mill B 1.961030  103.4140 environment     1.2 Do you have issue 2 in your area? Refer to Guideline 2
7    Mill B 1.961030  103.4140      social     1.3 Do you have issue 3 in your area? Refer to Guideline 3
8    Mill B 1.961030  103.4140      social     1.4 Do you have issue 4 in your area? Refer to Guideline 4

我设法从我自己的研究中产生的rmarkdown代码如下所示

---
title: "Summary Report"
author: "Authors Name"
date: "September 19, 2015"
output: pdf_document

---

# Summary

This is a summary report for your mill

```{r, comment=NA, echo=FALSE}
# A table with data received from R script
# create reports on students from an Excel spreadsheet. 
library(readxl)
library(knitr)

setwd("C:/Users/Jason/Mill Summary Report")

# read in the excel file

data <- read_excel("dummy_pat_results.xlsx")

for (i in 1:nrow(data)) {
cat("Section:", data$section[i], "\n")
cat("Question: ",data$question[i], "\n")
cat("Remark: ",data$remark[i], "\n")
cat("\n")
}


```

我正在使用的R脚本如下所示

library("rmarkdown")

for (i in unique(data$mill_name)){
  subgroup <- data[data$mill_name == i,]
  rmarkdown::render("Test_Markdown_v2.Rmd", 
  output_file=paste0(i, ".pdf"))    
}

这就是我现在正在为我创建的2个pdf创建的内容,我们首先将它们分解为每个Mill的报告(Mill A和Mill B)

Current output

理想情况下,我想得的是这样的

enter image description here

我已经看到了一些为具有不同数据集的情节创建单独报告的示例,但没有像我需要的那样报告。对此有任何帮助非常感谢。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我认为你需要参数化的Rmarkdown文档。以下链接将带您进入一个非常简单的教程。我希望这能解决你的问题。

http://rmarkdown.rstudio.com/developer_parameterized_reports.html