感谢您阅读此问题。我有一个包含5个参与者/行和8个变量/列的数据集。我能够将每个参与者/数据集的行导出到一个单独的pdf文件中,并根据参与者的名称来命名导出pdf文件的名称(感谢@Jilber Urbina)。我需要有关如何在表格前添加文本(格式正确)的帮助,该文本应显示以下内容:“亲爱的客户,此报告提供了有关性能测试的分数摘要”随附的解释性代码解释了此问题
# Creating a dataset for explanation:
df <- data.frame(matrix(ncol = 8, nrow = 5))
x <- c("Participants", "V1","V2", "V3", "V4","V5", "V6","V7")
colnames(df) <- x
df$Participants= c("Elizabeth",
"Emily",
"Cristie",
"Orville",
"Janetta")
df$V1 = runif(5, min=0, max=10)
df$V2 = runif(5, min=0, max=10)
df$V3 = runif(5, min=0, max=10)
df$V4 = runif(5, min=0, max=10)
df$V5 = runif(5, min=0, max=10)
df$V6 = runif(5, min=0, max=10)
df$V7 = runif(5, min=0, max=10)
summary(df)
# Printing each participant/row in individual report:
library(gridExtra)
apply(df, 1, function(x){
pdf(paste0(x[1], ".pdf"), width=10)
grid.table(data.frame(t(x)))
dev.off()
})
如何在每个报告中添加文本/消息,以便客户可以阅读文本/消息并查看其性能得分?非常感谢
答案 0 :(得分:1)
您可以呈现rmarkdown文档,而不是使用pdf设备。拿这个R脚本
# Creating a dataset for explanation:
df <- data.frame(matrix(ncol = 8, nrow = 5))
x <- c("Participants", "V1","V2", "V3", "V4","V5", "V6","V7")
colnames(df) <- x
df$Participants <- c("Elizabeth", "Emily", "Cristie", "Orville","Janetta")
df$V1 = runif(5, min=0, max=10)
df$V2 = runif(5, min=0, max=10)
df$V3 = runif(5, min=0, max=10)
df$V4 = runif(5, min=0, max=10)
df$V5 = runif(5, min=0, max=10)
df$V6 = runif(5, min=0, max=10)
df$V7 = runif(5, min=0, max=10)
summary(df)
# Printing each participant/row in individual report:
apply(df, 1, function(x){
print(x)
fname <- paste0(x[1], ".pdf")
rmarkdown::render(input="template.Rmd",
output_file = fname,
output_format = "pdf_document",
params = list(id=x[1], data=x[-1]))
})
使用不同的参数呈现rmarkdown文档 template.Rmd 。它的内容是
---
output: pdf_document
params:
id: NULL
data: NULL
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Evaluation
Dear `r params$id`, This report provides a summary of your scores on the performance test.
```{r, echo=F}
knitr::kable(params$data)
```
您可以根据需要设置输出的样式。这个简单的模板为您提供