Ruby4Knime is a realization of special node types for knime.org that allow to use Ruby scripts for interactive data analytic.
The main aims of this project are:
-
provide possibility to use Ruby language for interactive data analysis;
-
make the analysis more effective using clear and laconic Ruby style.
knime.rb is the simple mediator that allows to use KNIME`s classes written in Java in the Ruby programming style and also use all Java classes without any changes.
These samples illustrate how to generate output data from input data of the node.
$in_data_0.each do |row| $out_data_0 << row end
This example is applicated for Ruby Script 2x2. Note: all inputs and outputs are accessible by names like $in_data_X, $out_data_X or by number in appropriate array $output_datatable_arr, $input_datatable_arr.
(0..1).each do |i| out = $output_datatable_arr[i] $input_datatable_arr[i].each do |row| out << row end end
This example also illustrates the progress state updating.
table = $input_datatable_arr[0] count = table.length table.each_with_index do |row, i| $out_data_0 << (row << Cells.new.string('Hi!'). int(row[0].to_s.length)) setProgress "#{i*100/count}%" if i%100 != 0 end
In this example new rows creates with new uniq rowkeys. Therefore it is possible to create output table with any number of rows.
$in_data_0.each do |row| $out_data_0 << Cells.new.string( row.getCell(0).to_s.length.to_s ) end
In this example cell 0 copies without changes and adds difference between cells with indexes 1 and 2. Index in Ruby style.
$in_data_0.each do |row| $out_data_0 << Cells.new.int(row[0].to_i). double(row[1].to_f - row[2].to_f) end
Ruby snippet realizes as the body of a lambda-function in the following template:
func = ->(row) do <<SNIPPET CODE>> end snippet_runner &func
Copy all rows without changes. Code may contains only row-variable.
row
Add difference between columns:
row << Cells.new. double(row[1].to_f - row[2].to_f)
Now is realized an access to cells of the row by indexes and by names. E.g. we have a table with following columns ‘x’, ‘Y1’, ‘Y2’, ‘y1(1)’, ‘y2(1)’. We can access by indexes from 0 to 4 or from -1 (last column) to -5 (first column). For these names a special methods are generated. All names are translated in low case. All symbols except alhabetic and numbers are changed to underline symbol. Multiple undeline symbols are changed by a single. Last one is deleted. That is we have a following set of methods: #x, #y1, #y2, #y1_1, #y2_1 available for instances of BlobSupportDataRow class. Following code fragments are available:
row << Cells.new.double(row[0].to_f) double(row.y1.to_f - row.y2.to_f)
and
$in_data_0.each do |row| $out_data_0 << Cells.new.string(row.x.to_s) end
Samples that add new cells in functional style.
count = $in_data_0.length $in_data_0.each_with_index do |row,i| $out_data_0 << row.string('Hi!').int(row.getCell(0).to_s.length).append setProgress "#{i*100/count}%" if i%100 != 0 end
-
$in_data_0.each do |row| $out_data_0 << $out_data_0.createRowKey. stringCell(row.getCell(0).to_s.length.to_s).new_row end
Now it is possible to use any types of KNIME in the DataOutput table. Simply input a full qualified Java class name in the configuration dialog of the Ruby node. E.g. org.knime.core.data.def.ComplexNumberCell or org.knime.ext.textprocessing.data.DocumentCell.