Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create NMFS completion certificates & use quarto for certificates #146

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3cfc69d
Add NMFS certificate template
ateucher Dec 10, 2024
4b75f64
Convert standard certificate to qmd
ateucher Dec 10, 2024
4135f9c
nmfs-openscapes logo
ateucher Dec 10, 2024
54ba380
Update create_certificate for quarto
ateucher Dec 10, 2024
f561f77
Document; add .quarto/ dir to gitignore
ateucher Dec 10, 2024
89286c9
Add a test
ateucher Dec 10, 2024
15d2fc1
Remove nmfs prefix from output filename
ateucher Dec 10, 2024
d597b92
Fix url formatting
ateucher Dec 10, 2024
06697ec
Pass cohort_type to create_certificate(); format dates
ateucher Dec 10, 2024
cdff2e4
Use .env pronoun
ateucher Dec 10, 2024
976c779
Add NMFS certificate to vignette
ateucher Dec 10, 2024
4f0faeb
Don't use .env
ateucher Dec 10, 2024
5f5e909
Wrap certificate creation in tryCatch
ateucher Dec 10, 2024
e829de6
Inclued error message in alert from tryCatch
ateucher Dec 10, 2024
f554787
format cohort name
ateucher Dec 10, 2024
60baede
Missing comma in example
ateucher Dec 10, 2024
e71eaa1
Make sure format.Date works on Dates
ateucher Dec 10, 2024
ec6c043
tests for create_batch_certificates()
ateucher Dec 10, 2024
55f3a46
use dplyr::tibble since dplyr is in Imports
ateucher Dec 10, 2024
d74e48a
Do date formatting in create_certificate()
ateucher Dec 10, 2024
9768b7a
Dates in ISO 8061
ateucher Dec 10, 2024
db5568e
Date in correct format in vignette
ateucher Dec 10, 2024
0f02cff
rename nmfs certificate template qmd
ateucher Dec 10, 2024
0d7af2e
Improve create_certificates() documentation
ateucher Dec 10, 2024
57b9b80
More documentation
ateucher Dec 10, 2024
77a2a17
test create_batch_certificates for failure
ateucher Dec 10, 2024
b6fc16e
Add enum list for cohort_type in create_batch_certificates()
ateucher Dec 20, 2024
3ee1969
document
ateucher Dec 20, 2024
a9f242b
styler
ateucher Dec 20, 2024
d9e8fe0
Assign match.arg() 🤦
ateucher Dec 20, 2024
e64cb96
document()
ateucher Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Imports:
magrittr,
parsermd,
purrr,
quarto,
rmarkdown,
rstudioapi,
stringr
Expand Down
276 changes: 169 additions & 107 deletions R/create_certificate.R
Original file line number Diff line number Diff line change
@@ -1,67 +1,111 @@
#' Render certificates of completion for a participant in a cohort
#'
#' @param cohort_name The name of the cohort
#' @param first_name First name of participant
#' @param first_name First name of participant
#' @param last_name Last name of participant
#' @param start_date cohort start date
#' @param end_date cohort end date
#' @param start_date cohort start date. `Date` or character in a standard format
#' (eg., YYYY-MM-DD)
#' @param end_date cohort end date. `Date` or character in a standard format
#' (eg., YYYY-MM-DD)
#' @param cohort_website cohort website
#' @param cohort_type What kind of cohort are the certificates for? This will
#' choose the appropriate certificate template: `"standard"` (default) or
#' `"nmfs"`.
#' @param output_dir output directory for certificates. Default `"."`
#' @param quiet Suppress quarto warnings and other messages. Default `TRUE`.
#' Set to `FALSE` to help debug if any errors occur.
#' @param ... Other parameters passed on to [quarto::quarto_render()]
#'
#' @return Saves the file to your current working directory, and
#' returns the path to the file
#' @return Saves the file to the specified directory, and returns the path to
#' the file
#' @export
#'
#' @examples
#' \dontrun{
#' create_certificate(cohort_name = "2023-fred-hutch",
#' participant_name = "Name",
#' start_date = "Sep 19",
#' end_date = "Oct 19",
#' cohort_website = "https://openscapes.github.io/2023-fred-hutch/")
#' create_certificate(
#' cohort_name = "2023-fred-hutch",
#' first_name = "FirstName",
#' last_name = "LastName",
#' start_date = "2023-09-19",
#' end_date = "2023-10-19",
#' cohort_website = "https://openscapes.github.io/2023-fred-hutch/",
#' cohort_type = "standard"
#' )
#' }
create_certificate <- function(cohort_name,
first_name,
last_name,
start_date,
end_date,
cohort_website,
output_dir = ".") {
create_certificate <- function(
cohort_name,
first_name,
last_name,
start_date,
end_date,
cohort_website,
cohort_type = c("standard", "nmfs"),
output_dir = ".",
quiet = TRUE,
...) {
# adapted from https://bookdown.org/yihui/rmarkdown/params-knit.html


cohort_type <- match.arg(cohort_type)

start_date <- as.Date(start_date)
end_date <- as.Date(end_date)

template <- switch(cohort_type,
standard = system.file("certificate/certificate.qmd", package = "kyber"),
nmfs = system.file("certificate/certificate-nmfs.qmd", package = "kyber")
)

participant_name <- paste(first_name, last_name)
rmarkdown::render(
system.file("certificate/certificate.Rmd",package = "kyber"),
params = list(
cohort_name = cohort_name,
participant_name = participant_name,
start_date = start_date,
end_date = end_date,

outfile <- paste0(
"OpenscapesCertificate",
"_",
gsub("\\s+", "-", cohort_name),
"_",
gsub("\\s+", "-", participant_name),
".pdf"
)

cohort_name_formatted <- cohort_name |>
stringr::str_replace_all("[-_]+", " ") |>
stringr::str_to_title() |>
stringr::str_replace("[Nn][mM][fF][sS]", "NMFS")

quarto::quarto_render(
template,
output_format = "typst",
execute_params = list(
cohort_name = cohort_name_formatted,
participant_name = participant_name,
start_date = format(start_date, "%B %d, %Y"),
end_date = format(end_date, "%B %d, %Y"),
cohort_website = cohort_website
),
output_format = "pdf_document",
output_file = paste0(
"OpenscapesCertificate",
"_",
gsub("\\s+", "-", cohort_name),
"_",
gsub("\\s+", "-", participant_name),
".pdf"
),
output_dir = output_dir
output_file = outfile,
quiet = quiet,
...
)

if (output_dir != ".") {
fs::dir_create(output_dir)
fs::file_move(outfile, output_dir)
}

cli::cli_inform(
c("v" = "File written to {.path {fs::path(output_dir, outfile)}}")
)
}

#' Render a batch of certificates for a Champions Cohort
#'
#' Given a data.frame of Champions participants and a Cohort registry,
#'
#' Given a data.frame of Champions participants and a Cohort registry,
#' create a certificate for each participant
#'
#' @param registry `data.frame` from Google Sheet of Champions registry
#' @param registry `data.frame` from Google Sheet of Champions registry
#' (`"OpenscapesChampionsCohortRegistry"`)
#' @param participants `data.frame` from Google Sheet of Champions participants
#' (`"OpenscapesParticipantsMainList"`)
#' @param cohort_name Name of the cohort as it appears in `registry` and
#' @param cohort_name Name of the cohort as it appears in `registry` and
#' `participants`
#' @inheritParams create_certificate
#'
Expand All @@ -72,98 +116,116 @@ create_certificate <- function(cohort_name,
#' \dontrun{
#' registry <- read_sheet("path-to-registry")
#' participants <- read_sheet("path-to-participants")
#'
#'
#' create_batch_certificates(
#' registry,
#' particpants,
#' "2023-fred-hutch",
#' "2023-fred-hutch",
#' "~/Desktop/fred-hutch-certificates"
#' )
#' }
create_batch_certificates <- function(registry,
participants,
cohort_name,
output_dir = ".") {

create_batch_certificates <- function(
registry,
participants,
cohort_name,
cohort_type = c("standard", "nmfs", "pathways"),
output_dir = ".") {
if (!cohort_name %in% registry$cohort_name) {
stop("'cohort_name' is not a cohort in 'registry_sheet'", call. = FALSE)
}

if (!cohort_name %in% participants$cohort) {
stop("'cohort_name' is not a cohort in 'participant_sheet'", call. = FALSE)
}


cohort_type <- match.arg(cohort_type)

registry_cohort <- dplyr::filter(
registry,
.data$cohort_name == !!cohort_name
)

participants_cohort <- dplyr::filter(
participants,
.data$cohort == !!cohort_name
)

## Loop through each participant in list and create certificate for each

dir.create(output_dir, showWarnings = FALSE, recursive = TRUE)

for (row in seq_len(nrow(participants_cohort))) {
create_certificate(
cohort_name = registry_cohort$cohort_name,
first_name = participants_cohort$first[row],
last_name = participants_cohort$last[row],
start_date = registry_cohort$date_start,
end_date = registry_cohort$date_end,
cohort_website = registry_cohort$cohort_website,
output_dir = output_dir
tryCatch(
{
first_name <- participants_cohort$first[row]
last_name <- participants_cohort$last[row]

create_certificate(
cohort_name = cohort_name,
first_name = first_name,
last_name = last_name,
start_date = registry_cohort$date_start,
end_date = registry_cohort$date_end,
cohort_website = registry_cohort$cohort_website,
cohort_type = cohort_type,
output_dir = output_dir
)
},
error = function(e) {
cli::cli_inform(
c(
"x" = "Unable to create certificate for {.val {paste(first_name, last_name)}}",
"i" = paste(" Error:", e$message)
)
)
}
)

output_dir
}


output_dir
}
# Ideas for my original approach before Nick Tierney's help
# kyber::call_agenda(
# registry_url = "https://docs.google.com/spreadsheets/d/1Ys9KiTXXmZ_laBoCV2QWEm7AcnGSVQaXvm2xpi4XTSc/edit#gid=942365997",
# cohort_id = "2022-nasa-champions",
# call_number = 3)
# kyber_function(registry, ParticpantsList) %>%
# kyber::create_certificate <- function(registry, ParticpantsList) %>%
# 1. Read cohort_metadata from OpenscapesChampionsCohortRegistry gsheet
# Get values of these variables about the Cohort
# cohort_name_long
# cohort_website
# date_start to date_end
#
# 2. Get participant names from OpenscapesParticipantsMainList sheet
# Approach 1: copy names from sheet and use datapasta to paste as tribble, analogous to generating name.md files for GitHub Clinic
# library(stringr)
# library(datapasta) # install.packages("datapasta")
# library(kyber) ## remotes::install_github("openscapes/kyber")
# library(here)
# library(fs)
#
# ## use `datapasta` addin to vector_tribble these names formatted from the spreadsheet!
# cohort <- c(tibble::tribble(
# ~first, ~last,
# "_demo", "",
# "Erin", "Robinson",
# "Julie", "Lowndes"
# )
# )
#
# ## create .md files for each Champion
# kyber::short_names(cohort$first, cohort$last) |>
# create_github_clinic(here())
# Output as pdfs named openscapes_certificate_name.pdf, one per participant
# Upload pdfs to Cohort Folder
# In Closing of Final Cohort Call, add "get your certificate" and point to folder location


# Ideas for my original approach before Nick Tierney's help

# kyber::call_agenda(
# registry_url = "https://docs.google.com/spreadsheets/d/1Ys9KiTXXmZ_laBoCV2QWEm7AcnGSVQaXvm2xpi4XTSc/edit#gid=942365997",
# cohort_id = "2022-nasa-champions",
# call_number = 3)

# kyber_function(registry, ParticpantsList) %>%
# kyber::create_certificate <- function(registry, ParticpantsList) %>%

# 1. Read cohort_metadata from OpenscapesChampionsCohortRegistry gsheet
# Get values of these variables about the Cohort
# cohort_name_long
# cohort_website
# date_start to date_end
#
# 2. Get participant names from OpenscapesParticipantsMainList sheet
# Approach 1: copy names from sheet and use datapasta to paste as tribble, analogous to generating name.md files for GitHub Clinic
# library(stringr)
# library(datapasta) # install.packages("datapasta")
# library(kyber) ## remotes::install_github("openscapes/kyber")
# library(here)
# library(fs)
#
# ## use `datapasta` addin to vector_tribble these names formatted from the spreadsheet!
# cohort <- c(tibble::tribble(
# ~first, ~last,
# "_demo", "",
# "Erin", "Robinson",
# "Julie", "Lowndes"
# )
# )
#
# ## create .md files for each Champion
# kyber::short_names(cohort$first, cohort$last) |>
# create_github_clinic(here())


# Output as pdfs named openscapes_certificate_name.pdf, one per participant

# Upload pdfs to Cohort Folder

# In Closing of Final Cohort Call, add "get your certificate" and point to folder location
1 change: 1 addition & 0 deletions inst/certificate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
35 changes: 35 additions & 0 deletions inst/certificate/certificate-nmfs.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
format: typst
params:
cohort_name: "NMFS Openscapes Champions Program"
participant_name: "Name"
start_date: "October 8"
end_date: "December 4"
cohort_website: "https://nmfs-openscapes.github.io/2024-nmfs-champions/"
files: ["nmfs-openscapes-logo.png"]
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{=typst}
#set page(numbering: none)
```

```{=typst}
#align(center, text(17pt)[
*NMFS Openscapes Champions Program \ Certificate of Completion*
])
```

Certificate of Completion: `r params$cohort_name` Cohort
`r paste0("<", params$cohort_website, ">")`

Dates: `r params$start_date` to `r params$end_date`

This certifies that `r params$participant_name` has completed the NMFS Openscapes Champions Program, a training and mentorship program in open science tools and practices for NOAA’s National Marine Fisheries Service science teams and staff. It is a multi-month program that is designed to ignite incremental and sustainable change within research teams and beyond. It helps teams get their own work done and participate in open science, while building skills and community within the realities of their busy schedules, varying expertise and needs. This was supported as part of NOAA Fisheries' data, infrastructure and workflow modernization effort. Learn more at [nmfs-openscapes.github.io/champions](https://nmfs-openscapes.github.io/champions).

<https://doi.org/10.5281/zenodo.7407246>

![](nmfs-openscapes-logo.png){width=250 fig-align="center"}
Loading
Loading