R-MAR​​KDOWN:从数据框架和打印列值读取到Word文档的名称

发布于 2025-02-08 23:53:14 字数 732 浏览 1 评论 0原文

我有一个.csv文件,如下所示。我想在我在r-markdown中创建的Word文档中编写ID(ID1,ID2,ID3)值。每次手动过滤行很容易(filter(name ==“ andrew”)),但是我该如何通过代码进行操作,这样我就不必每次手动过滤每个名称。我想为每个名称创建一个Word文档。

data.csv:
Name Country id1 id2 id3
Andrew Germany 23 253 81
Tim Peru 28 789 1
JK Spain 45 678 22
Nina Korea 53 67 89

My code:
```{r, warning=FALSE, echo=FALSE, message=FALSE}
library(dplyr)
data<-read.csv("data.csv")
mydata<-data %>% filter(Name == "Andrew")

```

```{r, warning=FALSE, echo=FALSE, message=FALSE, comment='\t'}

cat(sprintf("The employee %s from %s with %s is associated with number %s",mydata$Name, mydata$country, mydata$id1, mydata$id2))


```

然后我只是编织文档。 如何为每个名称创建一个单独的Word文档(在这种情况下为4个文档),其中包含该名称的详细信息?

提前致谢

I have a .csv file like below. I want to write the id (id1, id2, id3) values in the word document that I am creating in R-markdown. It is easy to filter the row manually each time (filter(Name == "Andrew"))but how do I do it through code so that I do not have to filter manually each time for each name. I want to create a word document for each name.

data.csv:
Name Country id1 id2 id3
Andrew Germany 23 253 81
Tim Peru 28 789 1
JK Spain 45 678 22
Nina Korea 53 67 89

My code:
```{r, warning=FALSE, echo=FALSE, message=FALSE}
library(dplyr)
data<-read.csv("data.csv")
mydata<-data %>% filter(Name == "Andrew")

```

```{r, warning=FALSE, echo=FALSE, message=FALSE, comment='\t'}

cat(sprintf("The employee %s from %s with %s is associated with number %s",mydata$Name, mydata$country, mydata$id1, mydata$id2))


```

And then I just knit the document.
How do I create a separate word document for each name (So 4 documents in this case)containing details for that name only?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2025-02-15 23:53:14

这可以通过参数化报告来实现。为此,您可以使用将名称作为参数的报告模板:

---
output: word_document
params:
  name: Andrew
---

```{r, warning=FALSE, echo=FALSE, message=FALSE}
library(dplyr)
data <- read.csv("data.csv")
mydata <- data %>%
  filter(Name == params$name)
```

```{r, warning=FALSE, echo=FALSE, message=FALSE, comment='\t', results='asis'}
cat(sprintf("The employee %s from %s with %s is associated with number %s", mydata$Name, mydata$Country, mydata$id1, mydata$id2))
```

之后,您可以使用Render :: rmarkDown在所需名称的向量上循环以为每个名称创建一个类似名称的报告:

names <- c("Andrew", "Tim", "JK", "Nina")

lapply(names, function(x) rmarkdown::render("report-template.Rmd", output_file = paste0("report-for-", x), params = list(name = x)))

This could be achieved via a parametrized report. To this end you could use a report template which takes the name as a parameter:

---
output: word_document
params:
  name: Andrew
---

```{r, warning=FALSE, echo=FALSE, message=FALSE}
library(dplyr)
data <- read.csv("data.csv")
mydata <- data %>%
  filter(Name == params$name)
```

```{r, warning=FALSE, echo=FALSE, message=FALSE, comment='\t', results='asis'}
cat(sprintf("The employee %s from %s with %s is associated with number %s", mydata$Name, mydata$Country, mydata$id1, mydata$id2))
```

Afterwards you could use render::rmarkdown to loop over a vector of desired names to create a report for each name like so:

names <- c("Andrew", "Tim", "JK", "Nina")

lapply(names, function(x) rmarkdown::render("report-template.Rmd", output_file = paste0("report-for-", x), params = list(name = x)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文