rjsonhttrxapi

GET xAPI statements from a LRS


Have a look at the code below.

My objective is to GET xAPI statements from a Learning Record Store (LRS).

The code is able to GET the first 100 xAPI statements from the LRS - 100 is the maximum amount of statements displayed per page - but not the ones after that.

To solve said problem, I tried to create a loop where I make use of a counter to obtain the next 100 statements and so on. However, I ran into trouble. The current code does not save the next 100 xAPI statements. It simply saves the first 100 over and over. How do I fix this?

If you need any more information, let me know.

output <- data.frame()
counter <- 001L

base <- "https://lrs.com/servlet/ekp/xAPI/statements?from="
url <- paste0(base,counter)

while (TRUE) {
  res <- tryCatch({
    dat <- GET(url, authenticate(username, password, type = "basic"))
    dat <- content(dat, "text", encoding = "UTF-8")
    fromJSON(dat, flatten = TRUE)
  },error = function(e) NULL)
  if (length(res$statements) == 0) break
    output <- bind_rows(output, res$statements)
    counter <- counter + 100L
    print(counter)
}

output <- do.call(rbind, output)

Solution

  • The url does not change, it should be included inside the while loop.

    while (TRUE) {
      url <- paste0(base,counter)
      res <- tryCatch({
        dat <- GET(url, authenticate(username, password, type = "basic"))
        dat <- content(dat, "text", encoding = "UTF-8")
        fromJSON(dat, flatten = TRUE)
      },error = function(e) NULL)
      if (length(res$statements) == 0) break
      output <- bind_rows(output, res$statements)
      counter <- counter + 100L
      print(counter)
    }