Search code examples
r-exams

How sum up and print points of all exercises?


I know that in r-exam I can assign points to exercises eg. via expoints in the exercise' meta-data. However, I don't know to get the sum of the points across all exercises.

As a specific usecase: Consider a test that (by formal university requirements) must consist of (say) 90 points. So, I need to track the number of points that are already included via the exercises of the test.

I'm unaware which variable tracks this score (if any).


Solution

  • You are right, this information is not directly available, however it can be extracted from the metainformation contained in the output from any exams2xyz() interface. As a simple illustration consider:

    library("exams")
    set.seed(0)
    exm <- exams2pdf(c("swisscapital.Rmd", "deriv.Rmd", "ttest.Rmd"),
      n = 1, points = c(1, 17, 2))
    

    Now exm is a list with only n = 1 exam, consisting of three exercises, each of which provides its metainform (among other details). So you can extract the points of the second exercise in the first (and only) exam via:

    exm[[1]][[2]]$metainfo$points
    ## [1] 17
    

    So to get the points from all exercises in the first exam:

    sapply(exm[[1]], function(y) y$metainfo$points)
    ## exercise1 exercise2 exercise3 
    ##         1        17         2 
    

    Of course, here it the points were explicitly set in exams2pdf() and were thus known. But the same approach can also be used if the points are set via the expoints tag in the individual exercises.