I am endeavouring to create a statistics class exam with the R/exams package. So I am creating a series of Rmd files to create the questions as per the requirements of the package. My issue is with creating tables that appear nicely in PDF format once using the exam2nops format.
I have a small dataframe in R called Freqtable1
> Freqtable1
# A tibble: 6 × 5
`Political Parties (x)` `Absolute Frequency (f)` Relative freque…¹ Perce…² Cumul…³
<chr> <lgl> <lgl> <lgl> <lgl>
1 Green Party NA NA NA NA
2 Bloc Québécois NA NA NA NA
3 Conservative Party NA NA NA NA
4 Liberal Party NA NA NA NA
5 NDP NA NA NA NA
6 Total NA NA NA NA
# … with abbreviated variable names ¹`Relative frequency (2 decimals)`,
# ²`Percent (%) (2 decimals)`, ³`Cumulative Frequency (2 decimals)`
I then convert this into a kable called FT1 with the following command:
FT1 <-
kable(Freqtable1, format = "latex", align = "c", caption = "Frequency table") %>%
kable_styling(latex_options = c("hold_position"),font_size = 16) %>%
kable_paper(full_width = F) %>%
column_spec(1, width="4.0cm",border_left = TRUE) %>%
column_spec(2:4, width="1.8cm") %>%
column_spec(5,width="1.8cm",border_right = TRUE)%>%
kable_styling("bordered", font_size = 11)
This produces the following latex code
\begin{table}[!h]
\caption{\label{tab:Freqtable Qs2}Frequency table}
\centering
\fontsize{16}{18}\selectfont
\fontsize{11}{13}\selectfont
\begin{tabular}[t]{|>{\centering\arraybackslash}p{4.0cm}|>{\centering\arraybackslash}p{1.8cm}|>{\centering\arraybackslash}p{1.8cm}|>{\centering\arraybackslash}p{1.8cm}|>{\centering\arraybackslash}p{1.8cm}|}
\hline
Political Parties (x) & Absolute Frequency (f) & Relative frequency (2 decimals) & Percent (\%) (2 decimals) & Cumulative Frequency (2 decimals)\\
\hline
Green Party & & & & \\
\hline
Bloc Québécois & & & & \\
\hline
Conservative Party & & & & \\
\hline
Liberal Party & & & & \\
\hline
NDP & & & & \\
\hline
Total & & & & \\
\hline
\end{tabular}
\end{table}
When I run this code through exams2nops using the following code:
exams::exams2nops(QMT1, n=1, dir = "output", edir = "exercises",date="2023-02-19", reglength = 7,
blank = 1,
duplex = F,
envir=.GlobalEnv,
texengine = "pdflatex",
title = "QM First Test",
samepage = TRUE,
header = list(
Date = "2023-02-19",
# Name = function(i) formatC(Students$Name[i])
ID = function(i) formatC(i, width = 11, flag = "0")),
institution = "MySchol",logo ="my.png")
it produces the following error.
! Undefined control sequence.
<argument> |>{\centering \arraybackslash
}p{4.0cm}|>{\centering \arraybacksl...
l.13 ...cm}|>{\centering\arraybackslash}p{1.8cm}|}
Error: LaTeX failed to compile QA-First-Test solution1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See QA-First-Test solution1.log for more info.
I have manually removed the \centering\arraybackslash
and pasted the code into the Rmd file to create the pdf which works, but this completely fails to meet the standard of automating the questions.
I am running the latest version of R-studio and R as well as all the packages, as of 27-02-2023.
I am using the following packages including R/Exams library(tidyverse) library(readxl) library(knitr) library(kableExtra)
I would infinitely prefer using flextable package instead of knitr but the conversion to pdf seems to totally fail!
here is the contents of the .Rmd
---
output:
html_document:
df_print: paged
pdf_document:
fig_width: 4
latex_engine: pdflatex
---
Question
========
```{r Freqtable Qs, echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE}
library(tidyverse)
library(readxl)
library(flextable)
library(knitr)
library(kableExtra)
library(htmltools)
# knitr::opts_chunk$set(ft.tabcolsep=0, ft.latex.float = "none")
# flextable::set_flextable_defaults(fonts_ignore=TRUE)
cp <- sample(1:99,1)
ndp<- sample(1:99,1)
bq<- sample(1:99,1)
lpc<- sample(1:99,1)
gp<- sample(1:99,1)
tp <- cp+ndp+bq+lpc+gp
Freqtable1 <- readxl::read_excel("~/Library/CloudStorage/OneDrive-Personal/Aaron/Champlain/Resources/R Homework/QM Test 1/exercises/Freqtable1.xlsx")
options(knitr.kable.NA = '')
# FT1 <-
# flextable::flextable(Freqtable1) %>%
# flextable::theme_box() %>%
# save_as_image(path="~/Library/CloudStorage/OneDrive-Personal/Aaron/Champlain/Resources/R Homework/QM Test 1/exercises/bob.png")
# save the flextable as an HTML file
# include the HTML file in the PDF
# includeHTML(ft1_html)
#ft_image <- knitr::include_graphics(flextable::as_raster(FT1))
# FT1AP <- flextable::flextable(FT1A) %>% flextable::theme_box() %>% save_as_image(path="~/Library/CloudStorage/OneDrive-Personal/Aaron/Champlain/Resources/R Homework/QM Test 1/exercises/bobAnswer.png")
```
On the first day of class, a statistics professor asked all `r tp` students to report their favourite Canadian political party from a list. The data is distributed as follows: Conservative Party = `r cp` students, New Democratic Party (NDP) = `r ndp` students, Liberal Party = `r lpc` students, Green Party = `r gp` students, and Bloc Québécois = `r bq` students. (8pts total).
Fill in the frequency table for these data. (4pts)
```{r Freqtable Qs2, echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE}
FT1 <-
kable(Freqtable1, format = "latex", align = "c", caption = "Frequency table") %>%
kable_styling(latex_options = c("hold_position"),font_size = 16) %>%
kable_paper(full_width = F) %>%
column_spec(1, width="4.0cm",border_left = TRUE) %>%
column_spec(2:4, width="1.8cm") %>%
column_spec(5,width="1.8cm",border_right = TRUE)%>%
kable_styling("bordered", font_size = 11)
```
`r FT1`
What is the third favourite political party among these students?
Answerlist
----------
```{r echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE}
FT1A <- Freqtable1
FT1A[,2] <- c(gp,bq,cp,lpc,ndp,tp)
FT1A[,3] <- c(round(gp/tp,2),round(bq/tp,2),round(cp/tp,2),round(lpc/tp,2),round(ndp/tp,2),tp/tp)
FT1A[,4] <- c(round(gp/tp*100,2),round(bq/tp*100,2),round(cp/tp*100,2),round(lpc/tp*100,2),round(ndp/tp*100,2),tp/tp*100)
FT1A[,5] <- c(round(gp/tp*100,2),round((gp+bq)/tp*100,2),round((gp+bq+cp)/tp*100,2),round((gp+bq+cp+lpc)/tp*100,2),round((gp+bq+cp+lpc+ndp)/tp*100,2),tp/tp*100)
FT1AP <-
kable(FT1A, format = "latex", booktabs = FALSE,align = "c", caption = "Frequency table: Solutions") %>%
kable_styling(latex_options = c("scale_down","hold_position")) %>%
kable_classic(full_width = F) %>%
kable_styling() %>%
# column_spec(1, width="4.0cm",border_left = TRUE) %>%
column_spec(2:4, width="1.8cm") %>%
column_spec(5,width="1.8cm",border_right = TRUE)%>%
kable_styling("bordered", font_size = 11)
popp <- dplyr::arrange(FT1A,desc(FT1A[,2]))
pop1 <- popp[2,1]
pop2<- popp[3,1]
pop3<- popp[4,1]
pop4<- popp[5,1]
pop5<- popp[6,1]
```
* `r pop1`
* `r pop2`
* `r pop3`
* `r pop4`
* `r pop5`
Solution
========
```{r }
```
`r FT1AP`
Answerlist
----------
* False. `r pop1`
* False. `r pop2`
* True. `r pop3`
* False. `r pop4`
* False. `r pop5`
Meta-information
================
exname: PopularParties
extype: mchoice
exsolution: 00100
exshuffle: 4
expoints: 2
Thanks so much! The exams2nops(..., usepackage = "array")
was the solution