Search code examples
paster-exams

using paste0 in file name in exams2moodle


I am trying to create a loop to automatize exams generation using the examspackage....

I have created a series of exercices like this

gr1 <- c("ae1_IntroEst_1.Rmd","ae1_IntroEst_2.Rmd","ae1_IntroEst_3.Rmd","ae1_IntroEst_4.Rmd")

gr2 <- c("ae1_IntroProcEst_1.Rmd","ae1_IntroProcEst_2.Rmd","ae1_IntroProcEst_3.Rmd","ae1_IntroProcEst_4.Rmd")

...etc... 

Now, I am creating a loop to export all the exercices to moodle xml:

for (i in 1:2){
  grupo <- paste0("gr",i)
  exams2moodle(grupo, name = paste0("mt1_",i, "_M"), dir = "nops_moodle", encoding = "UTF-8", schoice = list(answernumbering = "none", eval = ee))
}

But I am getting this error:

Error in xexams(file, n = n, nsamp = nsamp, driver = list(sweave = list(quiet = quiet, : The following files cannot be found: gr11.

If I replace "grupo" by "gr1" then it works... (but I am generating 20 exercices). I can't figure it out...

Any ideas?

Thanks!


Solution

  • Because grupo is a string: "gr1". The exams2moodle's first parameter is a string (in your case) and not the list of files (as you want).

    If you want use a variable which name is in a string variable, you should use get (get: Return the Value of a Named Object)

    Check the sample code:

    > x <- 'foo'
    > foo <- 'bar'
    > x
    [1] "foo"
    > get(x)
    [1] "bar"
    > 
    

    In your case:

    for (i in 1:2){
      grupo <- paste0("gr",i)
      exams2moodle(get(grupo), name = paste0("mt1_",i, "_M"), dir = "nops_moodle", encoding = "UTF-8", schoice = list(answernumbering = "none", eval = ee))
    }