R in WeBWorK
Contents
Required setup
Starting with PG pull request #214, WeBWorK has a built-in capability to use R code in authoring problems. After you have a working WeBWorK server, there are three distinct steps that need to happen:
- set up the R server
- install the Perl module for the Perl-R bridge
- configure WeBWorK with the location of the R server
Set up the R server
Your R server (which can be on the same server as WeBWorK, but in a real-world scenario will more likely run on its own hardware/VM), is quite easy to set up:
- install R following OS-specific instructions
- install the Rserve server, which allows remote clients to execute R code on the Rserve's server and returns the execution's result in response. The easiest way is to run (as an administrative user)
Rscript -e 'install.packages("Rserve", repo="https://cran.rstudio.com")'
. Note that this package includes C source that needs to be compiled, so you have to have the basic developer tools present on the server. - run the Rserve service as appropriate to your system. The command you need to run is
R CMD Rserve
. This will start the Rserve daemon which will listen on port 6311. The daemon only accepts connections from the localhost; if you run WebWork and Rserve on separate servers, read the final section of this page for additional configuration steps for your system.
Install the Perl-R bridge
Perl module Statistics::R::IO implements Rserve's communication protocol in Perl and provides translation from R data structures to Perl's. It is available on CPAN and can be installed in the standard manner for Perl modules, e.g., by running (as an admin user) cpan Statistics::R::IO
.
Configure Webwork with the location of the R server
PG macros that communicate with R need to know the location of the R server, which is a URL . You can do this by modifying "${WW_PREFIX}/webwork2/conf/defaults.conf", but since it's a local configuration, you should put it in localOverrides.conf:
$pg{specialPGEnvironmentVars}{Rserve} = {host => "localhost"};
The value of this variable should be a reference to a hash with at least the key host
. If you are running Rserve on a non-standard port (i.e., not on 6311), you should specify it with the key port
.
Additional configuration when WeBWorK and R are on separate hosts
If you run WeBWorK and R on separate hosts, you can either set up a tunnel to forward port 6311 from WeBWorK to R's host, or do the following:
- set up Rserve to listen on all network interfaces, not just localhost by adding a line "remote enable" to file "/etc/Rserv.conf":
cat <'EOF' >> /etc/Rserv.conf remote enable EOF
- use the correct host name (instead of "localhost") in the "Rserve host" line of "localOverrides.conf". For example:
$pg{specialPGEnvironmentVars}{Rserve} = {host => "www.example.com"};
Note that in this case you will also want to set up the Rserve host's firewall to only allow connections from the WeBWorK host(s), or otherwise it will happily execute arbitrary code from any Rserve client anywhere on the internet!