2

I'm trying to generate assessment reports based on student data. My idea was to create a RDS (nested tibble) to pass along to rmarkdown::render() using purrr::pwalk(). I'm relatively new to purrr and its map family of functions.

Here's the part of the code that works (try it out):

library(tidyverse)

# assignment tibble
assignments <- tribble(
~ FullName, ~Assignment, ~Type, ~Score, ~Due,
"Friendly, Officer", "First Assignment", "Assignment", 4, "2018-11-01",
"Friendly, Officer", "Second Assignment", "Assignment", 2, "2018-11-03",
"OLantern, Jack", "First Assignment", "Assignment", 3, "2018-11-01",
"OLantern, Jack", "Second Assignment", "Assignment", 3, "2018-11-03"
)

# personal data RDS
personal <- tribble(
~ FullName, ~ID, ~Phone, ~DOB,
"Friendly, Officer", 366529, "999-999-9990", "2000-03-04",
"OLantern, Jack", 586121, "999-999-9991", "2001-04-05"
) %>%
group_by(FullName) %>%
nest(.key = "personal")

# create an rds
myrds <- assignments %>%
group_by(FullName) %>%
nest(.key = "assignments")

# join assignment and personal data
myrds <- myrds %>%
inner_join(personal)

...which generates myrds that looks like:

> myrds
# A tibble: 2 x 3
  FullName          assignments      personal        
  <chr>             <list>           <list>          
1 Friendly, Officer <tibble [2 x 4]> <tibble [1 x 3]>
2 O'Lantern, Jack   <tibble [2 x 4]> <tibble [1 x 3]>

Now, I'm trying to generate a "report" object, then pass this along to rmarkdown::render and purrr::pwalk based on this part of Hadley Wickham's code in the following video:

https://www.youtube.com/watch?v=K-ss_ag2k9E&t=2339s

My desired output is different PDF files for each student, with the report having a student's personal data and their assignment data. Thanks for your help!

Update

Following camille's comment, and putting together pieces from the video I mentioned I came up with the following. For some reason tibble() complains about the length of name and filename ... but this part works for my original database , oddly enough (this is a separate issue). Here's my try:

loc <- dirname("~//")

reports <- tibble(
  name = unique(myrds$FullName),
  filename = paste0(dirname(loc),
                    "\\/",
                    stringr::str_extract(name, "\\w+"), 
                    "_", 
                    lubridate::ymd(Sys.Date()),
                    ".pdf"),
  params = map(myrds, ~ list(my_report = .))
)

Now to generate documents:

reports %>%
  select(output_file = filename, params) %>%
  pwalk(rmarkdown::render, input = "myReport.Rmd")

This gives me the error:

Error in knit_params_get(input_lines, params) : 
  render params not declared in YAML: my_report

It is the params part that I am having problems with. YAML complains about the parameters I'm sending it (I'm not surprised). In the original code (see video link above), the parameters are simple. How do I format the params = map(myrds, ~ list(my_report = .)) line above so I can pass on to rmarkdown::render the student data?

Marian Minar
  • 1,344
  • 10
  • 25
  • You can adapt [this post](https://stackoverflow.com/q/49301776/5325862) to use `pwalk` instead of `lapply` – camille Nov 29 '18 at 03:25
  • Thanks camille. I visited the post, and I don't think the OP's question was answered thoroughly. It appears that the "solution" only passes the names of the student, but it doesn't pass on the "sex" and "age". I think I understand how you would change param for YAML to accomplish this, but my problem is that I have an "assignment" tibble that contains many columns and rows for each student. How do I pass this on to YAML through param? (see my updated attempt above) – Marian Minar Nov 29 '18 at 18:46

0 Answers0