Skip to content

Update the Rserve interface. #1213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions conf/pg_config.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ specialPGEnvironmentVars:
# Size in pixels of dynamically-generated images, i.e. graphs.
onTheFlyImageSize: 400

# To enable Rserve (the R statistical server), uncomment the following two
# lines. The R server needs to be installed and running in order for this to
# work. See http://webwork.maa.org/wiki/R_in_WeBWorK for more info.
#Rserve:
# host: localhost

# Locations of CAPA resources. (Only necessary if you need to use converted CAPA problems.)
CAPA_Tools: $Contrib_dir/CAPA/macros/CAPA_Tools/
CAPA_MCTools: $Contrib_dir/Contrib/CAPA/macros/CAPA_MCTools/
Expand Down
55 changes: 18 additions & 37 deletions lib/Rserve.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,34 @@ my $rserve_loaded = eval {
sub access {
die 'Statistics::R::IO::Rserve could not be loaded. Have you installed the module?'
unless $rserve_loaded;

Statistics::R::IO::Rserve->new(@_);
return Statistics::R::IO::Rserve->new(@_);
}

## Evaluates an R expression guarding it inside an R `try` function
##
## Returns the result as a REXP if no exceptions were raised, or
## `die`s with the text of the exception message.
# Evaluates an R expression guarding it inside an R `try` function
#
# Returns the result as a REXP if no exceptions were raised, or
# `die`s with the text of the exception message.
sub try_eval {
my ($rserve, $query) = @_;

my $result = $rserve->eval("try({ $query }, silent=TRUE)");
die $result->to_pl->[0] if _inherits($result, 'try-error');
# die $result->to_pl->[0] if $result->inherits('try-error');
my $result = $rserve->eval("try({ $query }, silent = TRUE)");
die $result->to_pl->[0] if $result->inherits('try-error');

$result;
return $result;
}

## Returns a REXP's Perl representation, dereferencing it if it's an
## array reference
##
## `REXP::to_pl` returns a string scalar for Symbol, undef for Null,
## and an array reference to contents for all vector types. This
## function is a utility wrapper to make it easy to assign a Vector's
## representation to an array variable, while still working sensibly
## for non-arrays.
# Returns a REXP's Perl representation, dereferencing it if it's an
# array reference
#
# `REXP::to_pl` returns a string scalar for Symbol, undef for Null,
# and an array reference to contents for all vector types. This
# function is a utility wrapper to make it easy to assign a Vector's
# representation to an array variable, while still working sensibly
# for non-arrays.
sub unref_rexp {
my $rexp = shift;

my $rexp = shift;
my $value = $rexp->to_pl;
if (ref($value) eq ref([])) {
@{$value};
} else {
$value;
}
}

## Reimplements method C<inherits> of class L<Statistics::R::REXP>
## until I figure out why calling it directly doesn't work in the safe
## compartment
sub _inherits {
my ($rexp, $class) = @_;

my $attributes = $rexp->attributes;
return unless $attributes && $attributes->{'class'};

grep {/^$class$/} @{ $attributes->{'class'}->to_pl };
return ref($value) eq 'ARRAY' ? @$value : $value;
}

1;
36 changes: 22 additions & 14 deletions macros/core/PGbasicmacros.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2231,11 +2231,6 @@ =head2 Formatting macros
# The function should be called either with value specified (immediate reference) or
# with url specified in which case the revealed text is taken from the URL $url.
# The $display_text is always visible and is clicked to see the contents of the knowl.
htmlLink($url, $text)
# Places a reference to the URL with the specified text in the problem.
# A common usage is \{ htmlLink(alias('prob1_help.html') \}, 'for help')
# where alias finds the full address of the prob1_help.html file in the same directory
# as the problem file
iframe($url, height=>'', width=>'', id=>'', name=>'' )
# insert the web page referenced by $url in a space defined by height and width
# if the webpage contains a form then this must be inserted between
Expand Down Expand Up @@ -2362,18 +2357,31 @@ sub OL {
);
}

=head2 htmlLink

Usage: C<htmlLink($url, $text, @attributes)>

Places an HTML link to C<$url> with the specified C<$text> in the problem. The
C<@attributes> are optional. They should be provided as attribute/value pairs,
but a single text string argument can be given (although calling C<htmlLink> in
that way is deprecated and should not be done in new problems). For example,

BEGIN_PGML
Download the [@ htmlLink($url, 'dataset', download => 'dataset.csv') @]*
for this problem.

=cut

sub htmlLink {
my $url = shift;
my $text = shift;
my $options = shift;
my $sanitized_url = $url;
$sanitized_url =~ s/&/&amp;/g;
$options = "" unless defined($options);
return "$BBOLD [ the link to '$text' is broken ] $EBOLD" unless defined($url) and $url;
my ($url, $text, @options) = @_;
return "$BBOLD [ the link to '$text' is broken ] $EBOLD" unless $url;
my $attributes = @options == 1 ? $options[0] : {@options};
MODES(
TeX => "{\\bf \\underline{$text}}",
HTML => "<A HREF=\"$url\" $options>$text</A>",
PTX => "<url href=\"$sanitized_url\">$text</url>",
HTML => ref($attributes) eq 'HASH'
? tag('a', href => $url, %$attributes, $text)
: qq{<a href="$url" $attributes>$text</a>},
PTX => '<url href="' . ($url =~ s/&/&amp;/g) . qq{">$text</url>},
);
}

Expand Down
Loading