情节出现在html考试中,但未出现在pdf版本中

时间:2018-11-20 12:51:58

标签: r r-exams

我将以下Rmd与exams R软件包一起使用时遇到问题。它适用于html,但不适用于pdf,我不知道为什么。我在html中有2个面板,但是在pdf中却不见了。谢谢!

```{r data generation, echo = FALSE, results = "hide"}
## DATA GENERATION
constr = sample(c("std","centered"),size=1)


n <- sample(35:65,1)

mx <- runif(1, 40, 60)
my <- runif(1, 200, 280)
sx <- runif(1, 9, 12)
sy <- runif(1, 44, 50)
a = runif(1,2,10)
b = sample(c(-3,1.2,3),size = 1)
e = rnorm(n)

r <- round(runif(1, 0.5, 0.9), 2)
x <- rnorm(n, mx, sd = sx)
y <- (r * x/sx + rnorm(n, my/sy - r * mx/sx, sqrt(1 - r^2))) * sy
mod1 = lm(y~x)
x1 = 0
y1 = 0

if (constr == "centered") {
   x1 = x - mx
   y1 = y - my
} else if (constr == "std") {
   x1 = (x - mx) / sx
   y1 = (y - my) / sy
}
mod2 = lm(y1~x1-1)


## QUESTION/ANSWER GENERATION
nq = 2
questions <- rep(list(""), nq)
solutions <- rep(list(""), nq)
explanations <- rep(list(""), nq)
type <- rep(list("string"),nq)

questions[[1]] = "What was the treatment we applied to the data? Or answer should either be `centered` or `standardized`. "
if (constr=="centered") {
    solutions[[1]] <- "centered"
    explanations[[1]] <- "We substracted the mean of each variable"
    questions[[2]] = "Give the value of the OLS slope coefficient in the right panel rounded to 3 digits"
    solutions[[2]] = round(coef(summary(mod1))[2],3)
    explanations[[2]] = "in a centered regression without an intercept, the slope coefficient is identical to the slope of the uncentered data (regression run with intercept)"
} else if (constr == "std") {
    solutions[[1]] <- "standardized"
    explanations[[1]] <- "You can see that both variables are centered on zero and have a standard deviation of around one."
    questions[[2]] = "Give the value of correlation coefficient $r$ between $x$ and $y$ in the left panel  rounded to 3 digits"
    solutions[[2]] = round(coef(summary(mod2))[1],3)
    explanations[[2]] = "in a standardized regression without an intercept, the slope coefficient is identical to correlation coefficient"
}
type[[2]] <- "num"
```

Question
========

We have a dataset with `r n` observations on $x$ and $y$. We apply some treatment to the data and transform it to $y1$ and $x1$. The left panel below is the data before, the right panel is the data after treatment:

```{r baplot,echo=FALSE,fig.align='center',fig.width=8}
par(mfrow=c(1,2))
plot(y~x,main="before")
plot(y1~x1,main="after")
if (constr=="centered"){
    abline(mod2)
}
```

and you are given the OLS estimates of a regression `r ifelse(constr=="centered","of y on x with","of y1 on x1 without")` an intercept for the data in the `r ifelse(constr=="centered","left","right")` panel:

```{r,echo=FALSE}
if (constr=="centered"){
    coef(mod1)
} else {
    coef(mod2)
}
```

```{r questionlist, echo = FALSE, results = "asis"}
answerlist(unlist(questions), markup = "markdown")
```

Solution
========

```{r solutionlist, echo = FALSE, results = "asis"}
answerlist(unlist(solutions),paste(unlist(explanations), ".", sep = ""), markup = "markdown")
```

Meta-information
================
extype: cloze
exsolution: `r paste(solutions, collapse = "|")`
exclozetype: `r paste(type, collapse = "|")`
exname: regconstrained
extol: 0.05

1 个答案:

答案 0 :(得分:0)

TL; DR 省略MYUserManager

背景:如果没有AddScoped(),则在.Rmd练习上运行fig.align='center'会产生普通的Markdown输出:

fig.align

但是,当包含knitr::knit()时,它会产生HTML:

![plot of chunk baplot](figure/baplot-1.png)

knitr::knit()将Markdown转换为HTML时,这具有所需的效果,因为它仅保留HTML。但是当<img src="figure/baplot-1.png" title="plot of chunk baplot" alt="plot of chunk baplot" style="display: block; margin: auto;" /> 通过LaTeX转换为PDF时,它不知道如何在Markdown中处理HTML。

因此,pandoc的模块化设计在这里不起作用,因为这样的格式设置细节无法在标准Markdown中表示。有些Markdown方言具有此类扩展名,但并非所有Markdown处理器都支持这些扩展名。这就是为什么通常的建议是将Markdown与所需的输出标记(通常是HTML)混合使用。

因此,对于R /考试,我通常建议保持格式尽可能简单,以使练习在许多可能的输出格式下均能正常运行。在这里居中并不是很重要。