简体   繁体   中英

How to make multiple knits at once in r-markdown?

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()

```

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.

在此处输入图像描述

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM