如何在R-Markdown中立即制作多个针织?

发布于 2025-01-29 05:19:24 字数 1528 浏览 1 评论 0原文

我仍在寻找您可以提供的任何帮助:)

我正在制作一个充满工作统计数据的Onepager。我得到了70种不同的工作,这意味着我需要制作70个OnePagers。如何自动化降级以一次制作70个?

我只用两个基本图制作了简化的降价版本。我该如何做到这一点,以便在我编织3个作业的示例中,我会在每个onepager上获得3个带有正确统计数据的OnePagers,并将其保存到“ C:/users/desktop/onepagers/”中,将作业作为PDF名称。例如:Analyst.pdf,Doctor.pdf和Pilot.pdf

第1页上的第一个DF是:

Job     GENDER     PEOPLE
Analyst   M         500
Analyst   F         1000
Doctor    M         2500
Doctor    F         2000
Pilot     M         50
Pilot     F         10

第2页上的第二df是:

JOB      AGE      PEOPLE
Analyst Under 30   400
Analyst 31-40      700
Analyst 41-50      300
Analyst Over 50    100
Doctor Under 30    500
Doctor 31-40       1500
Doctor 41-50       1500
Doctor Over 50     1000
Pilot  Under 30    10
Pilot  31-40       25
Pilot  41-50       20
Pilot  Over 50     5
---
title: "Name of job"
output: pdf_document
---

```{r setup, include=FALSE}
library(tidyverse)
library(readxl)
options(scipen = 100)
knitr::opts_chunk$set(echo = TRUE)
```


```{r, echo=FALSE, fig.width=4, fig.height=2.5}

GENDER <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=1)

GENDER <- filter(GENDER, JOB == "Analyst")

ggplot(GENDER,aes(y=GENDER, x=PEOPLE))+
  geom_bar(stat = 'identity')+
  theme_minimal()

```

```{r, echo=FALSE, fig.width=3.7, fig.height=2.5}

AGE <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=2)

AGE <- filter(AGE, JOB == "Analyst")

ggplot(AGE,aes(y=AGE, x=PEOPLE))+
  geom_bar(stat = 'identity')+
  theme_minimal()

```

I'm still looking for any help you can provide :)

I'm making a onepager that is full of different stats on jobs. I got 70 different jobs that means i need to make 70 onepagers. How can i automate markdown to make all 70 at once?

I made a simplified markdown version with just two basic plots. How can i make it so that in my example with 3 jobs when i knit i get 3 onepagers with the correct stats on each onepager and save them to "C:/Users/Desktop/Onepagers/" with the job as the pdf name. For example: Analyst.pdf , Doctor.pdf and Pilot.pdf

First DF on sheet 1 is:

Job     GENDER     PEOPLE
Analyst   M         500
Analyst   F         1000
Doctor    M         2500
Doctor    F         2000
Pilot     M         50
Pilot     F         10

Second DF on sheet 2 is:

JOB      AGE      PEOPLE
Analyst Under 30   400
Analyst 31-40      700
Analyst 41-50      300
Analyst Over 50    100
Doctor Under 30    500
Doctor 31-40       1500
Doctor 41-50       1500
Doctor Over 50     1000
Pilot  Under 30    10
Pilot  31-40       25
Pilot  41-50       20
Pilot  Over 50     5
---
title: "Name of job"
output: pdf_document
---

```{r setup, include=FALSE}
library(tidyverse)
library(readxl)
options(scipen = 100)
knitr::opts_chunk$set(echo = TRUE)
```


```{r, echo=FALSE, fig.width=4, fig.height=2.5}

GENDER <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=1)

GENDER <- filter(GENDER, JOB == "Analyst")

ggplot(GENDER,aes(y=GENDER, x=PEOPLE))+
  geom_bar(stat = 'identity')+
  theme_minimal()

```

```{r, echo=FALSE, fig.width=3.7, fig.height=2.5}

AGE <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=2)

AGE <- filter(AGE, JOB == "Analyst")

ggplot(AGE,aes(y=AGE, x=PEOPLE))+
  geom_bar(stat = 'identity')+
  theme_minimal()

```

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

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

发布评论

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

评论(1

鸠书 2025-02-05 05:19:24

有几个步骤可以实现这一目标。不过,远不远胜于自己创建70个文件!

您有两个不同的数据集。在您的帖子中,一个具有job;另一个具有作业。我将它们都更改为job。在您的帖子中,作业标题以相同的方式记录。例如-

Analyst = Analyst≠Analyst≠Analyst

如果这是始终 true,这将起作用。如果并非总是如此,仍然可以做到这一点,但需要修改。

您将创建一个RMD 模板和一个使用该模板的R脚本。我建议您首先为一个工作领域创建RMD。格式化它是您喜欢所有文件的样子。然后将文件修改为模板(或复制)。一旦它是模板,您将无法在没有R文件的情况下编织RMD文件。

YAML具有基于职位的标题,但这仅仅是因为您已经表明这是需要的。但是,呼叫参数:具有非常具体的信息。您可以在引号中更改内容,但是参数:必须保留,并且您要迭代的变量,例如job,必须完全按原样编写出现在数据中。

---
title: "`r params$JOB`"
output: pdf_document
params:
  JOB: ""
---

库和选项(无更改)

```{r setup, include=FALSE}
library(tidyverse)
library(readxl)
options(scipen = 100)
knitr::opts_chunk$set(echo = TRUE)
```

块的代码 - 指出删除特定作业和内容params $ job。这将来自R文件。

```{r p1, echo=FALSE, fig.width=4, fig.height=2.5}
GENDER <- read.table(header = T, text = "
                     JOB     GENDER     PEOPLE
Analyst   M         500
Analyst   F         1000
Doctor    M         2500
Doctor    F         2000
Pilot     M         50
Pilot     F         10")

# GENDER <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=1)
# GENDER <- filter(GENDER, JOB == "Analyst")
GENDER <- filter(GENDER, JOB == params$JOB)

ggplot(GENDER, aes(y = GENDER, x = PEOPLE)) +
  geom_bar(stat = 'identity') +
  theme_minimal()

```


```{r p2, echo=FALSE, fig.width=3.7, fig.height=2.5}
AGE <- read.table(header = T, text = "
                  JOB      AGE      PEOPLE
Analyst Under-30   400
Analyst 31-40      700
Analyst 41-50      300
Analyst Over-50    100
Doctor Under-30    500
Doctor 31-40       1500
Doctor 41-50       1500
Doctor Over-50     1000
Pilot  Under-30    10
Pilot  31-40       25
Pilot  41-50       20
Pilot  Over-50     5
")

# AGE <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=2)
# AGE <- filter(AGE, JOB == "Analyst")
AGE <- filter(AGE, JOB == params$JOB)

ggplot(AGE,aes(y = AGE, x = PEOPLE)) +
  geom_bar(stat = 'identity') +
  theme_minimal()

```

保存此RMD文件,但不要打扰编织;它行不通。记下该文件的名称和位置;您需要它的R文件。

我的RMD名为pdfgraphs.rmd(我知道如此原始)。我将R文件保存在与RMD相同的目录中(不过,这不是必需的)。但是,它们没有保存在当前的工作目录中,因此我仍然需要这条路径。

现在,当我运行此代码时, R文件的内容

library(tidyverse)
library(rmarkdown)
library(glue)

# only one of the two tables is needed here
###  ASSUMING that the call for filtering is using the exact same values
####  for example, Analyst = Analyst ≠ analyst ≠ ANALYST 
#### as long as it's the same word, same spelling, same capitalization
GENDER <- read.table(header = T, text = "
                     JOB     GENDER     PEOPLE
Analyst   M         500
Analyst   F         1000
Doctor    M         2500
Doctor    F         2000
Pilot     M         50
Pilot     F         10")

walk(.x = unique(GENDER$JOB),
     ~render(input = "./snippets/pdfGraphs.Rmd", # path via working directory
             output_file = glue({.x}, ".pdf"), # file name is job; i.e., Analyst.pdf
             params = list(JOB = {.x})))       # set's the param variable

将创建一个名为“作业名称”的PDF,就像这样。

​不需要。 (感谢GD)

There are a few steps to make this happen. Far less than creating the 70 files yourself, though!

You have two different data sets. In your post, one has Job; the other has JOB. I changed them both to JOB. In your post, the job titles are documented the same way. For example—

Analyst = Analyst ≠ analyst ≠ ANALYST

If this is always true, this will work. It can still be done if it isn't always true, but it would require modifications.

You'll create an RMD template and an R script that uses that template. I would suggest that you first create the an RMD for one job field. Format it exactly how you like all of the files to look. Then modify the file to be the template (or copy it). Once it's the template, you will not be able to knit the RMD file without the R file.

The YAML has a title based on the job title, but only because you had indicated this was desired. However, the call params: has very specific information. You can change the content in the quotes, but params: must remain as is, and the variable you're iterating over, like JOB here, must be written exactly as it appears in the data.

---
title: "`r params$JOB`"
output: pdf_document
params:
  JOB: ""
---

The libraries and options (no changes)

```{r setup, include=FALSE}
library(tidyverse)
library(readxl)
options(scipen = 100)
knitr::opts_chunk$set(echo = TRUE)
```

The code for the chunks—note the removal of a specific job and the content params$JOB. This is going to come from the R file.

```{r p1, echo=FALSE, fig.width=4, fig.height=2.5}
GENDER <- read.table(header = T, text = "
                     JOB     GENDER     PEOPLE
Analyst   M         500
Analyst   F         1000
Doctor    M         2500
Doctor    F         2000
Pilot     M         50
Pilot     F         10")

# GENDER <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=1)
# GENDER <- filter(GENDER, JOB == "Analyst")
GENDER <- filter(GENDER, JOB == params$JOB)

ggplot(GENDER, aes(y = GENDER, x = PEOPLE)) +
  geom_bar(stat = 'identity') +
  theme_minimal()

```


```{r p2, echo=FALSE, fig.width=3.7, fig.height=2.5}
AGE <- read.table(header = T, text = "
                  JOB      AGE      PEOPLE
Analyst Under-30   400
Analyst 31-40      700
Analyst 41-50      300
Analyst Over-50    100
Doctor Under-30    500
Doctor 31-40       1500
Doctor 41-50       1500
Doctor Over-50     1000
Pilot  Under-30    10
Pilot  31-40       25
Pilot  41-50       20
Pilot  Over-50     5
")

# AGE <- read_xlsx("C:/Users/Desktop/TEST_MARKDOWN.xlsx", sheet=2)
# AGE <- filter(AGE, JOB == "Analyst")
AGE <- filter(AGE, JOB == params$JOB)

ggplot(AGE,aes(y = AGE, x = PEOPLE)) +
  geom_bar(stat = 'identity') +
  theme_minimal()

```

Save this RMD file, but don't bother knitting; it won't work. Take note of the name and location of this file; you'll need it for the R file.

My RMD was named pdfGraphs.Rmd (so original, I know). I saved the R file in the same directory as the RMD (this isn't required, though). However, they aren't saved in the current working directory, so I still needed the path.

The contents of the R file

library(tidyverse)
library(rmarkdown)
library(glue)

# only one of the two tables is needed here
###  ASSUMING that the call for filtering is using the exact same values
####  for example, Analyst = Analyst ≠ analyst ≠ ANALYST 
#### as long as it's the same word, same spelling, same capitalization
GENDER <- read.table(header = T, text = "
                     JOB     GENDER     PEOPLE
Analyst   M         500
Analyst   F         1000
Doctor    M         2500
Doctor    F         2000
Pilot     M         50
Pilot     F         10")

walk(.x = unique(GENDER$JOB),
     ~render(input = "./snippets/pdfGraphs.Rmd", # path via working directory
             output_file = glue({.x}, ".pdf"), # file name is job; i.e., Analyst.pdf
             params = list(JOB = {.x})))       # set's the param variable

Now when I run this code, it will create a pdf named the name of the JOB, like so.

enter image description here

enter image description here

FYI, I updated this content by removing the library epoxy. It's not needed. (Thanks GD)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文