Class: DataFrames

Inherits:
Object show all
Defined in:
app/models/r.rb

Overview

RSession

Defined Under Namespace

Modules: Examples

Instance Method Summary collapse

Constructor Details

#initialize(name, session = RSession::Default_Session) ⇒ DataFrames


58
59
60
61
# File 'app/models/r.rb', line 58

def initialize(name, session=RSession::Default_Session)
	@name=name.to_sym
	@session=session
end

Instance Method Details

#csv_import(name_order, filename = @name.to_s+'.csv', sep = ',') ⇒ Object

initialize


62
63
64
# File 'app/models/r.rb', line 62

def csv_import(name_order, filename=@name.to_s+'.csv', sep=',')
	@session.eval("#{@name}<-read.table('#{filename}',sep='#{sep}',fill=TRUE)")
end

#glm(model) ⇒ Object

pairSummary


105
106
107
# File 'app/models/r.rb', line 105

def glm(model)
	@session.eval("glm(#model}, data=#{@name}").as_doubles
end

#importRelation(tableName = @name) ⇒ Object


73
74
75
76
77
# File 'app/models/r.rb', line 73

def importRelation(tableName=@name)
# 	pv<-read.csv('/home/greg/energy/rails/energy/production.csv')
	@session.eval("#{tableName}<-read.csv(\"/tmp/#{tableName}.csv\")")

end

#pairSummary(x, y) ⇒ Object

variableSummary


100
101
102
103
104
# File 'app/models/r.rb', line 100

def pairSummary(x,y)
	variableSummary(x)
	variableSummary(y)
	show_plot(x,y)
end

#png_plot(x, y) ⇒ Object

show_plot


81
82
83
84
85
# File 'app/models/r.rb', line 81

def png_plot(x,y)
	@session.eval("png(filename = \"#{@name}.png\",width = 480, height = 480, units = \"px\", pointsize = 12, bg = \"white\",  res = NA,type = c(\"cairo\", \"Xlib\", \"cairo-png\", \"quartz\"))")
	show_plot(x,y)
	@session.eval("dev.off()")	
end

#psqlExport(tableName = @name) ⇒ Object

csv_import


65
66
67
68
69
70
71
72
# File 'app/models/r.rb', line 65

def psqlExport(tableName=@name)
	#sql="COPY #{tableName} TO '#{tableName}.csv' WITH CSV HEADER "
	sql="COPY #{tableName} TO STDOUT WITH CSV HEADER "
	puts "sql=#{sql}"	
	psql="psql --no-align --tuples-only --host localhost --dbname energy_development --command \"#{sql}\" >/tmp/#{tableName}.csv"
	puts "psql=#{psql}"	
	sysout=`#{psql}`
end

#r_class_symbol(field = :V0) ⇒ Object

r_symbol


90
91
92
93
# File 'app/models/r.rb', line 90

def r_class_symbol(field=:V0)
	var="#{@name}$#{field}"
	@session.eval("class(#{var})").as_strings[0]
end

#r_symbol(field = :V0) ⇒ Object

png_plot


87
88
89
# File 'app/models/r.rb', line 87

def r_symbol(field=:V0)
	"#{@name}$#{field}"
end

#show_plot(x, y) ⇒ Object


78
79
80
# File 'app/models/r.rb', line 78

def show_plot(x,y)
	@session.eval("plot(#{r_symbol(x)},#{r_symbol(y)})")
end

#variableSummary(var, tableName = @name) ⇒ Object

r_class_symbol


94
95
96
97
98
99
# File 'app/models/r.rb', line 94

def variableSummary(var, tableName=@name)
	ret={}
	statistics=@session.eval("summary(#{r_symbol(var)})").as_doubles
	ret[:Min], ret[:Quartile1], ret[:Median], ret[:Mean], ret[:Quartile3], ret[:Max]=statistics
	ret
end