## WeBWorK Problems

### generate file with data for students to analyze?

by George Jennings -
Number of replies: 3
Hi,

Is there a way to write a statistics problem that would dynamically generate small data files that students could save to their desktops and then open with a generic spreadsheet or other application for analysis?

I have tried displaying the data as an html table but this doesn't work because most of my students use Internet Explorer, which can't seem to copy html tables into a spreadsheet without changing their structure.

Saving data in a comma-separated textfile should work if there's a way to
generate the file and display a link to it. It would be even better if there were a reliable way for students to copy data and paste them directly into a spreadsheet.

--George
In reply to George Jennings

### Re: generate file with data for students to analyze?

by L. Felipe Martins -
Would this be suitable?

This is a Flash applet, I just put it together quickly for demo purposes. The students would not need to select anything, just clicking the button will put the data in the clipboard. I don't know the specification of csv files, but I can paste the data into OpenOffice Calc, so I assume I am doing it correctly. I didn't do much testing, but tried the most common browsers (including IE), except Opera. It does not look great in my Mac, but it is OK in Windows (what about that?).

You will have to decide how to generate the data for the problem and pass it to the applet, but we can talk about that. I guess the easiest thing would be to generate the data in the PG file and pass it to the applet for display only. It is also possible to generate the data in the applet, but then a new applet has to be compiled each time a new data set is used. Also, the grading would be done completely in the PG file, that is the simplest approach, from my experience.

I'd be glad to help to create a template that could be used for generating problems like this.

It is certainly possible to do something similar in JavaScript. The easiest way to do that, I guess, would be to use a framework, such as jQuery, but I'm not an expert on that. There also is a <datagrid> tag in HTML5, but apparently it was removed from the specification, but seems to still be supported by some browsers, at least. See here:
http://www.quackit.com/html_5/tags/html_datagrid_tag.cfm
In reply to L. Felipe Martins

### Re: generate file with data for students to analyze?

by Arnold Pizer -
Hi,

I just tested this out.  Very nice.  Actually Excel uses tabs as the default delimiter so if you replace comas by tabs, you can paste this directly into an Excel spreadsheet. Importing a csv file (which might be more standard) is a little more work in Excel.

If students use Excel, you probably want at least a tab delimited option.

Arnie

In reply to L. Felipe Martins

### Re: generate file with data for students to analyze?

by George Jennings -
Thanks Felipe and Arnie,

Felipe's flash looks great but daunting to learn so I chickened out and cooked up something in javascript that seems to work pretty well. It displays a window with a tab-separated table, then students can copy and paste it into a spreadsheet. Separating with tabs seems to be the key to get it to work with
Internet Explorer. Thank, Arnie, for that suggestion.

Here is a demo that shows how it works in a very simple problem.

DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
);

## Set-up

Context("Numeric");
# intercept, slope
$b = random(2,5,.1);$m = random(3,7);
# create fake noisy linear data (x[i],y[i]), i=0,1,...
$count = 10;$sumx = 0; $sumy = 0; for($i=0;$i<$count;$i++){$x[$i] =$i+random(-.4,.5,.1);
$y[$i] = $b +$m*( $x[$i]+random(-.4,.5,.1) );
# remove extra digits possibly caused by binary arithmetic
$x[$i] = sprintf("%.1f",$x[$i]);
$y[$i] = sprintf("%.1f",$y[$i]);
# one line in the tab-separated data table
$jsDat[$i] = '"'.join('\t',$x[$i],$y[$i]).'"';
$sumx +=$x[$i];$sumy += $y[$i];
}

# stringify table for passing to javascript
$jsDatStr = join(",",@jsDat); # data for javascript # javascript for opening data window HEADER_TEXT(<<EOS); <script Xlanguage="javascript" type="text/javascript"> <!--// function openDataWindow(){ var xyRow=new Array($jsDatStr);
var i=0;
dataWindow=window.open('','','width=200,height=300');
dataWindow.document.writeln("<pre>");
dataWindow.document.writeln("x\ty");
dataWindow.document.writeln("---\t---");
for (i=0;i<$count;i++){ dataWindow.document.writeln(xyRow[i]); } dataWindow.document.writeln("</pre>"); } //--> </script> EOS # answers$meanx = Compute("$sumx/$count");
$meany = Compute("$sumy/$count"); ## Main text TEXT(beginproblem()); Context()->texStrings; BEGIN_TEXT Here are some fake data. \{ begintable(12) \} \{ row( "x", @x)\} \{ row( "y", @y)\} \{ endtable() \} Open the \{htmlLink("javascript:openDataWindow();","easy copy")\} window, copy-and-paste the data from the window into a spreadsheet or other application, then use the application to find the mean of $$x$$ and the mean of $$y$$.$PAR
$$\bar{x}$$ = \{ans_rule(10)\} $SPACE $$\bar{y}$$ = \{ans_rule(10)\} END_TEXT Context()->normalStrings; ## Answer evaluation ANS($meanx->cmp());
ANS(\$meany->cmp());

ENDDOCUMENT();

--George