moodler-exams

Generating cloze question: one numeric and one single choice where choices are plots


I am writing an exercise in RMarkdown (usign knitr in RStudio) which includes one numeric question and one single-choice question. How can I produce the correct code that allows the activity to be displayed in Moodle?

The numeric question requires entering the correlation coefficient (whose correct answer is "corr1" in the code below), and the single-choice question requires selecting the graph that represents the data (with "grafico1" being the correct answer).

library(ggplot2)
Concentration = c(7.1,82.9)
r.sq  = 0
while (r.sq <0.85) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data$Reaction_time~data$Concentration)
summa = summary(z)
r.sq = summa$r.squared
}
corr = round(cor(data$Concentration,data$Reaction_time),2)

corr1 = corr
r.sq  = 0
while (r.sq <0.85 | abs(corr1)<abs(corr)) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data1 = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data1$Reaction_time~data1$Concentration)
corr1 =  round(cor(data1$Concentration,data1$Reaction_time),2)
summa = summary(z)
r.sq = summa$r.squared
}

corr2 = corr
r.sq  = 0
while (r.sq <0.85 | abs(corr2)<abs(corr)) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data2 = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data2$Reaction_time~data2$Concentration)
corr2 =  round(cor(data2$Concentration,data2$Reaction_time),2)
summa = summary(z)
r.sq = summa$r.squared
}

corr3 = corr
r.sq  = 0
while (r.sq <0.85 | abs(corr3)<abs(corr)) {
x = runif(33, min(Concentration),max(Concentration))
b = runif(1,-3,3)+rnorm(33,0,0.25)
a = ifelse(max(b)<0, -min(b)*max(x),0)
data3 = data.frame(Concentration = round(x,2), Reaction_time = round(a+b*x,2))
z = lm(data3$Reaction_time~data3$Concentration)
corr3 =  round(cor(data3$Concentration,data3$Reaction_time),2)
summa = summary(z)
r.sq = summa$r.squared
}

write.csv(data,'concentration_vs_rxtime_q2.csv', row.names = FALSE)

p1 = ggplot(data, aes(x=Concentration, y=Reaction_time)) + geom_point()
p2 = ggplot(data1, aes(x=Concentration, y=Reaction_time)) + geom_point()
p3 = ggplot(data2, aes(x=Concentration, y=Reaction_time)) + geom_point()
p4 = ggplot(data3, aes(x=Concentration, y=Reaction_time)) + geom_point()

png('grafico1.png', width=380, height=248, units='px', pointsize=12)
p1
dev.off()

png('grafico2.png', width=380, height=248, units='px', pointsize=12)
p2
dev.off()

png('grafico3.png', width=380, height=248, units='px', pointsize=12)
p3
dev.off()

png('grafico4.png', width=380, height=248, units='px', pointsize=12)
p4
dev.off()

This code does not work in Moodle since the chart options do not have checkboxes available for selecting the correct answer.

Determine the correlation coefficinet:
##ANSWER1##


Choose the right plot for data:
1. ![](grafico1.png)  
2. ![](grafico2.png)  
3. ![](grafico3.png)  
4. ![](grafico4.png)

Meta-information
================
extype: cloze
exclozetype: num|schoice
exsolution: `r corr1`|1000
exname: mod10_q1

Solution

  • The reason why your code does not work is that you need to declare (a) where the interaction ##ANSWER2## goes and (b) what the Answerlist for that interaction is. You can do so via:

    Question
    ========
    Determine the correlation coefficient: ##ANSWER1##
    
    Choose the right plot for data:
    
    ##ANSWER2##
    
    Answerlist
    ----------
    * ![](grafico1.png)  
    * ![](grafico2.png)  
    * ![](grafico3.png)  
    * ![](grafico4.png)
    

    However, when including plots directly in the answer list, the details whether this can be displayed correctly depend on the learning management system and the settings selected. In general, graphics cannot be included in dropdown lists which is often the default interaction for schoice elements in cloze exercises. So in many settings it is simpler to create plots with labels like "A", "B", "C", ... and then include in the Answerlist only the letters * A, ..., rather than the whole graphic.

    Having said that, in Moodle I managed to include the plots directly if I pick vertical buttons rather than dropdown menus (via MULTICHOICE_V) and using base64 embedding directly rather than Moodle's pluginfile feature:

    exams2moodle("correlation.Rmd",
      cloze = list(cloze_schoice_display = "MULTICHOICE_V"),
      pluginfile = FALSE)