This is related to another post where I had problems including a ggplot together with a regression table in a R-exam generated pdf; fixed the ggplot part, so splitting out the regression table issue.
modelsummary generated table without problems with knitr.exams does not seem to run the modelsummary call for some reason. What's going on?First the Rmd.
```{r, results = "hide"}
library(modelsummary)
# library(exams) # uncomment for simple knit to pdf
questions <- list()
questions[[1]] <- "is 1 true?"
questions[[2]] <- "is 2 true?"
solution = list()
solution[[1]] = TRUE
solution[[2]] = FALSE
```
Question
========
Here is some output
```{r}
m = lm(speed ~ dist, cars)
m
```
```{r}
modelsummary(m,output = "latex")
```
```{r questionlist, echo = FALSE, results = "asis"}
answerlist(questions, markup = "markdown")
```
Solution
========
```{r solutionlist, echo = FALSE, results = "asis"}
answerlist(solution, markup = "markdown")
```
Meta-information
================
extype: mchoice
exsolution: `r mchoice2string(solution)`
exname: test
running this with exams2pdf yields the following intermediate tex tile in the temp folder:
library(exams)
exams2pdf("test.Rmd",texdir = "textemp",verbose = TRUE)
# same result with additional arg
# usepackage = c("tabularray", "float","graphicx","codehigh", "ulem", "booktabs","siunitx")
# does not seem a latex error
# exercise1.tex
\begin{question}
Here is some output
\begin{verbatim}
m = lm(speed ~ dist, cars)
m
\end{verbatim}
\begin{verbatim}
##
## Call:
## lm(formula = speed ~ dist, data = cars)
##
## Coefficients:
## (Intercept) dist
## 8.2839 0.1656
\end{verbatim}
\begin{verbatim}
modelsummary(m,output = "latex")
\end{verbatim}
\begin{answerlist}
\item is 1 true?
\item is 2 true?
\end{answerlist}
\end{question}
\begin{solution}
\begin{answerlist}
\item TRUE
\item FALSE
\end{answerlist}
\end{solution}
Here we see that while the output of printing m gets correctly captured and displayed in the tex, there is nothing visible for the modelsummary(m) call. seems it never ran. Consequently the produced pdf does not have any regression table in it.
Now plain knitr to pdf: works.
what's going on here? if it's not supported, what is the recommended way to display several regressions next to each other in an R-exam? Thanks for any help!
Ok got it. Went into the weeds of looking at all the templates, but actually that's not necessary. exams2nops will not allow any custom template anyway, but additional latex args need to be given via the header argument. I learned the following:
in the Rmd, the modelsummary object needs to be printed or you won't see it. This is unlike any other object that you just want to show by calling it's name.
in the examsnops call we need to give all the required latex comments for the modelsummary object to compile correctly.
here is an example Rmd that works for me:
```{r, results = "hide"}
library(modelsummary)
questions <- list()
questions[[1]] <- "Model (1) does have a squared term"
questions[[2]] <- "Model (2) does have a squared term"
solution = list()
solution[[1]] = FALSE
solution[[2]] = TRUE
```
Question
========
Here is some output which is displayed as verbatim code:
```{r}
ms = list()
ms[["(1)"]] = lm(speed ~ dist, cars)
ms[["(2)"]] = lm(speed ~ dist + I(dist^2), cars)
```
plots work without `fig.cap`:
```{r}
plot(speed ~ dist, cars)
```
`modelsummary` output needs to be `print`ed.
```{r,results='asis'}
print(modelsummary(ms,output = "latex",gof_omit = "Adj.|AIC|BIC|F|Log.Lik.|RMSE"))
```
Now, given all that, are the following statements true?
```{r questionlist, echo = FALSE, results = "asis"}
answerlist(questions, markup = "markdown")
```
Solution
========
```{r solutionlist, echo = FALSE, results = "asis"}
answerlist(solution, markup = "markdown")
```
Meta-information
================
extype: mchoice
exsolution: `r mchoice2string(solution)`
exname: test
and here is the exams2nops call:
exams2nops("test.Rmd",
header = c("\\usepackage{tabularray}",
"\\usepackage{float}",
"\\usepackage{graphicx}",
"\\usepackage{codehigh}",
"\\usepackage[normalem]{ulem}",
"\\UseTblrLibrary{booktabs}",
"\\UseTblrLibrary{siunitx}",
"\\newcommand{\\tinytableTabularrayUnderline}[1]{\\underline{#1}}",
"\\newcommand{\\tinytableTabularrayStrikeout}[1]{\\sout{#1}}",
"\\NewTableCommand{\\tinytableDefineColor}[3]{\\definecolor{#1}{#2}{#3}}"))