Tuesday, January 13, 2009

13Jan2009

Agenda
Welcome Chris and David
erb
RubyRX
Logging

Next Week
More RubyRx

Week after
IO

Sample db table
patient trt sex race age

Make a summary table based on this dataset:

Trt1 Trt2

Sex M X X
F X X

Age
N X X
Mean X.X X.X
Median X X
Min X X
Max X X

Race
White X X
Asian X X
Black X X


Instead of an X you'd have an instance variable

@[:frequency]['Placebo']['M'] for instance
a nested hash
@sex{frequency=>{'Placebo'=>{''M'=>'17','F'='21'}}}

erb embeding ruby in HTML

require 'erb'
File.open('Template.txt',r) do t
File open ('output.html',w) do f
file puts ERB.new(t.readlines..to_s).results(o.get_binding)
end
end

o can be any object
get_binding is a method added to the object o
because they are seperate you get a lot more power

to get a new output file you just change the template

object o
An output class, it takes an array.
A bunch of ojects for accessor variables that you are interested in.

class Output
def initialize(stdm_dat)
@sdtm_data=sdtm_data
end

This is probably a fudge, you would probably use activerecord to make an object right out of the database.

def frequency_by_trtgrp(*args)
args.each do arg

instance_eval("@#{arg}=Hash.new") unless instance_variable.defined

Create instance variables for everything you are passing through

Which is the same as saying:

@sex=Hash.new

You should probably check to make sure this instance variable doesn't exist because it would overwrite and blow away what you already. Hence the unless statement.

instance_eval("2+2") will resolve the variable in the string, for instance and execute the code

@sex[:frequency]=Hash.new{h,kh[k]=Hash.new (&h.default_proc) }
Will give you as many level of nested hashes as you need.
Passing in a string to the ruby compiler at run time. Java and C you can't do this.

The disadvantage is that the innermost hash has no default value.

@sex[:frequency]['Placebo']['Sex']+=1

So Glen set everything to zero.

class Output
@Sdtm_data.each do e
instance_eval("@#{arg}[:Frequency][@trtgrp][@#(arg}]=0")
end
self
end

You add the self so the return value is the oject returned.

o=output.new
o.freq_by_trtgrp(:race,:sex).desc_stats

Glenns spoke about method missing.

You end up with a small set of methods you can use to construct a wide variety of


0.statistics_on_sex_and rave_by_trtgrp
You don't need parameters because the method name is going to give you that information.

@sex[:Frequency]['Placebo']['M']

Glen is rethinking the above approach as maybe not the best way to do it.

@sex['Placebo']=SimpleStatistics.new

@sex['Placebo'] << e.sex

The output object now knows nothing about statistics.

This changes the template:

<%=@sex['Placebo].freq('m')





No comments: