rmoodler-exams

Allowing Students to Read in Data Using Read.csv(URL)


I'm writing questions for an online test (held through Moodle) using R's exams package. Some of these questions include CSV files that students need to read into R and analyse.

It'd be great if students could read their data into R simply using read.csv(URL for the Moodle plug-in file), rather than having to read it in by downloading the data set by clicking the link to it within the question, saving it to a working directory, and then reading it into R from where they saved it.

Below is a MWE where I try to create a question where students could read a CSV file into R in the desired way.

#rmd file contents
```{r gen data, echo=FALSE, results='hide'}

x<-rnorm(20)
df<-data.frame(x)
write.csv(df,"dataset.csv",row.names=F)
```

Question
======

Downloading data set using URL: `read.csv("dataset.csv")`

Downloading via link: [click this link](dataset.csv)

Just adding a dummy question: Enter the number 1 ##ANSWER1##


Meta-information
================
extype: cloze
exclozetype: num
exsolution: 1

extitle: Testing dataset download using URL
exsection: Testing dataset download using URL

The screenshot below shows how the resulting question looks when exported into Moodle:

Data Set  Question MWE

When one tries to read the CSV file into R by reading directly from the plug-in file URL (e.g. by entering read.csv("https://moodlestack.otago.ac.nz/pluginfile.php/2497/question/questiontext/21614/1/129512/dataset.csv"), in this example), what's read into R is HTML code (I think the instructions for downloading the file when a link to it is clicked). I've included below the first few lines of it.

CSV HTML

What we're obviously wanting is for only the CSV file data to be read in after entering read.csv(URL). Is this going to be possible?

P.S. The reason for wanting this is that many of our 100-level students find the steps involved in reading a file into R to be difficult (most of our students have very limited quantitative and computer skills). We make them read files in the standard way in our assignments, but in the test where there's a higher grade, it'd be good if they didn't lose many the marks simply from not being able to read a file in (or being unable to fix errors that arise when doing so).


Solution

  • To access the data, you need to be logged in to Moodle. When accessing the CSV file using read.csv(URL), even if you're logged into Moodle on your browser, R fetches the data from the URL directly itself. You therefore need to provide your log in details to R's request to access the URL.
    This would be more complicated for a student than just learning how to download the file, save it to a working directory, and read it into R from there.
    Therefore, using read.csv(URL) will make it (much!) more complicated.