WeBWorK and R
WeBWorK is an interactive online homework system for mathematics and science courses. Pre-existing homework questions can be selected from an extensive library, or customized questions can be written using WeBWorK’s Perl-based scripting language (various tutorials can be found here).
An important addition to the WeBWorK platform is the ability to interface with R code; this is especially useful for writing homework questions that employ graphing or data generation techniques beyond the usual capabilities of WeBWorK itself. LMU’s WeBWorK installation has already been configured to interface with R via special commands within your WeBWorK problem definition file (.pg file).
Below are some useful examples. Some of these are adapted from the MAA’s R in WebWork page.
Loading Rserve macro
At the top of your .pg file, you will need to include the “RserveClient.pl” macro
loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
"RserveClient.pl" # <--- R integration
);
Returning computed numerical values
The function rserve_eval will send any string (enclosed in single quotes) to the R server and execute it as though it had been typed in the R terminal. The general format is
rserve_eval('<commandstring>');
If the R command returns a value, you can assign it to a WeBWorK variable. Here is a simple numerical computation:
$myval = rserve_eval('(pi + 7)/2');
You can also call other R functions within rserve_eval, such as
$myval = rserve_eval('mean(c(4,6,7))');
Returning a figure
Generating a figure requires two additional lines to tell R to capture the graphic device output:
$img = rserve_start_plot('png');
rserve_eval('curve(dnorm(x, mean=3.5), xlim=c(-4, 4))');
$image_path = rserve_finish_plot($img);
To display the figure in the BEGIN_TEXT section of your .pg file, use $image_path as its location:
BEGIN_TEXT
...image($image_path, width=>300, height=>300) \}:
\{
... END_TEXT
Passing WebWork variables to an R command
You may want to define a variable within your .pg file and then pass its value to R as part of a command. The clearest way to accomplish this is to construct the entire command string containing your desired variable, and then pass the string to R, as in this example where we modify the figure example from above to include a random value for the mean:
$mean = random(-2, 2, .5);
$commandstring = 'curve(dnorm(x, mean=' . $mean . '), xlim=c(-4, 4))';
$img = rserve_start_plot('png');
rserve_eval($commandstring);
$image_path = rserve_finish_plot($img);
Note that the dot operator “.” is used for concatenating strings and variables together.
Multi-line scripts
More complicated sets of commands requiring multiple lines can be issued line by line; the R session’s memory is persistent for the entirety of the .pg file. If you want to return a value as the result of some computation, remember to assign it to a WeBWorK variable. Example:
rserve_eval('x <- 5');
rserve_eval('y <- 8');
$myval = rserve_eval('x + y');
Alternatively, you can run an entire script by making the script file accessible over the internet via a regular URL, and then sourceing the script in a single rserve_eval command as in the following example:
$myval = rserve_eval('source("http://rovero.org/R/webwork_script_example.R")');
Be sure to enclose the URL in double quotes, and the entire command string in single quotes.
For complete versions of some of the above examples, see https://webwork.maa.org/wiki/R_in_WeBWorK