Skip to content

Instantly share code, notes, and snippets.

@djun-kim
Created March 10, 2013 19:51
Show Gist options
  • Save djun-kim/5130142 to your computer and use it in GitHub Desktop.
Save djun-kim/5130142 to your computer and use it in GitHub Desktop.
WeBWorK Rserve Integration sample problem
##DESCRIPTION
## Test rserve client
##ENDDESCRIPTION
##KEYWORDS('rserve')
## DBsubject('Statistics')
## DBchapter('Descriptive Statistics')
## DBsection('Mean')
## Date('13/11/2012')
## Author('Djun Kim')
## Institution('UBC')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
########################################################################
DOCUMENT();
loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
"RserveClient.pl",
);
# Print problem number and point value (weight) for the problem
TEXT(beginproblem());
# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;
##############################################################
#
# Setup
#
#
Context("Numeric");
$m = random(25,30,1);
$sd = random(2, 4, 0.1);
@rnorm = rserve_query(<<END_RCODE);
data1=rnorm(15,mean=$m,sd=$sd)
round(data1,4)
END_RCODE
$rnorm = join ", ", @rnorm;
@mean = rserve_query(<<END_RCODE);
round(mean($rnorm),4)
END_RCODE
$mean = join " ", @mean;
$rnorm = join ", ", @rnorm;
$ans = Real($mean);
##############################################################
#
# Text
#
#
Context()->normalStrings;
BEGIN_TEXT
Rnorm generated $rnorm; $BR
What is the mean of the above vector? \{ ans_rule(6) \} $BR
$PAR
Mean of \{ join ", ", @rnorm\} is $mean.$BR
END_TEXT
Context()->normalStrings;
##############################################################
#
# Answers
#
#
ANS( $ans->cmp());
ENDDOCUMENT();
##DESCRIPTION
## This problem illustrates the ability to dynamically generate
## graphics from the R statistical platform and display them
## in WeBWorK.
##ENDDESCRIPTION
##KEYWORDS('rserve', 'graphics', 'plotting')
## DBsubject('Statistics')
## DBchapter('Descriptive Statistics')
## DBsection('Mean')
## Date('13/11/2012')
## Author('Djun Kim')
## Institution('UBC')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
########################################################################
DOCUMENT();
loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
"RserveClient.pl",
);
# Print problem number and point value (weight) for the problem
TEXT(beginproblem());
# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;
##############################################################
#
# Setup
#
#
Context("Numeric");
rserve_start();
## create a plot
$file = rserve_start_plot('png');
rserve_eval("x <- rnorm(100,0,1)");
rserve_eval("hist(x, col='light blue')");
$filepath = rserve_finish_plot($file);
rserve_finish();
##############################################################
#
# Text
#
#
Context()->normalStrings;
BEGIN_TEXT
This problem illustrates the ability to dynamically generate
graphics from the R statistical platform and display them
in WeBWorK.
$PAR
tempDirectory is $tempDirectory.$BR
filepath is $filepath$BR
$PAR
The following image is generated by the R command $BR$BR
x <- rnorm(100,0,1) $BR
hist(x, col='light blue') $BR
$PAR
\{image($filepath, width=>400, height=>400)\}
END_TEXT
Context()->normalStrings;
##############################################################
#
# Answers
#
#
ENDDOCUMENT();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment