Skip to content

Instantly share code, notes, and snippets.

View jacintak's full-sized avatar

Jacinta Kong jacintak

View GitHub Profile
@jacintak
jacintak / PDF_exam.R
Created January 1, 2023 15:38
[R] TeX templates and R script for generating PDF exam and solution with exams2pdf and package [exams]
# Creating PDF exam
# Write questions as Rmd, use latex symbols instead of HTML
# get files as list. Could pool questions as well.
questions <- as.list(list.files("<location>", pattern = "*.Rmd", full.names = TRUE))
exams::exams2pdf(qs,
# list of questions
n = 2,
# Number of unique copies
@jacintak
jacintak / cover.tex
Last active December 31, 2022 23:58
[rmarkdown] LaTeX {exam} template with integrated R
\begin{coverpages}
\begin{center}
% provide instructions and details
\fbox{\fbox{\parbox{5.5in}{\centering
Answer the questions in the spaces provided on the question sheets. \linebreak
If you run out of room for an answer, continue on the back of the page.}}}
\end{center}
\vspace{1in}
\makebox[0.9\textwidth]{Name:\enspace\hrulefill}
@jacintak
jacintak / name_repair.R
Created May 17, 2022 01:26
custom suffix for duplicated column names [tidyverse]
# For generating unique column names using a custon function for behaviours outside default name_repair options or make.unique
# tibble has duplicated column names (a, b), stop error without name repair
tibble::tibble(a = letters[1:5], b = LETTERS[1:5], a = letters[1:5], b = LETTERS[1:5],
.name_repair = function (x) ave(x, x, FUN = function(i) stringr::str_c(i, '_', seq_along(i))))
# str_c appends suffix in form "_1", "_2", "_i" for each duplicated columns grouped as indicated by ave; across all columns
# output: a_1, b_1, a_2, b_2
# Works for any tidyverse tibble that takes .name_repair, e.g. expand_grid for generating pairwise tables
# Can use paste0 instead of str_c