KNIME spirits, at your service!

Armin Ghassemi Rudd
Act of Intelligence Accretion
6 min readMar 17, 2022

--

Do you believe in supernatural beings? No?! So here we may make you believe in some type of them called “Flow Variables”.

Flow variables (short form of “workflow variables”) are external parameters to be used in KNIME workflows[1]. These KNIME spirits can be created (from scratch), unleashed (from a data table) or even better, revived (to become data table values)!

Bringing a flow variable into existence is the easiest part. There are so many options to choose when you want to create a new flow variable. All nodes can do such a thing even those which their job is to extract variables and input them as data table values. Have you noticed the text boxes beside the list options in “Flow Variables” tab of the nodes’ configuration window? Those simple boxes are one of the potential birthplaces of flow variables. We can input a new name for our desired variable into those text boxes, save the configuration settings, execute the node and guess what; a new flow variable is born!

In the following image, we have input “my_variable” as the name for our desired variable in the configuration window of the “ Table Creator “ node.

The name of the first column that we have produced by using this node will be the value of the variable after executing the node.

Let’s check the output after executing this “Table Creator” node:

And as we navigate to the “Flow Variables” tab, we can see a new variable named “my_variable” which its value is “column1”.

The “Flow Variables” tab in nodes’ configuration window will become your favorite feature in KNIME as soon as you get familiar with. You can also modify all the node’s configurations by using flow variables in this tab. Let’s have another example in which we create a new flow variable by using the “ String Input “ node which can be found among the nodes in “Quickforms” subcategory under the “Workflow Control” category of the “Node Repository” panel.

In the configuration window of the node, we input “new variable” as the “Variable Name” and “product name” as the “Default Value”:

Now execute the node and connect the output port (which is a variable port) to the input variable port of our previously used “Table Creator” node. Ah! Cannot find the variable ports of the “Table Creator” node? No problem, right click on the node and select “Show Flow Variable Ports”.

So now we can use the flow variable created by “String Input” node to name our columns in the “Table Creator” node by assigning the variable to the “ColumnName” option of an already produced column in the “Flow Variables” tab.

Now if we recheck the output, we will notice that the column is renamed regarding our “new variable” value.

And also the value of our “my_variable” is changed regarding the new column name:

But now we have two variables with the same values. How about modifying one of them and use it to name a new column? Let’s use a “ String Manipulation (Variable) “ node and this expression to modify “my_variable”:

String("price")

The output of the “String Manipulation (Variable)” node:

And now let’s create a new column by using “ Constant Value Column “ and name it “price” by using our flow variable. We must input some value into “Append” option to be able to assign a variable to this parameter (this value will be ignored as soon as we assign a flow variable to the parameter). We set the column type to “Number (integer)” and the value to 0.

And this should be the output:

Now we want to change the product names to start from “prod10”, not “prod1”. How can we change the names? Manually? Or by using a “Rule Engine” node and defining five rules? What if we had thousands of products? So let’s find a better solution. Also, we want to add new prices based on product names which will be 100$ for the first product and then it will be increased by 50$ for the next product and so on. So let’s pick the nodes we need and do the job.

After the “Constant Value Column” node we use a “ Chunk Loop Start” node and set the value for “Rows per chunk” to 1, so we work on one row in each iteration of the loop. Now we have to convert the data from Row into variables. How? Just pick the “ Table Row To Variable” node and see how nicely this node does the job. Now that we have the product name and the price as flow variables, we can modify them using the “String Manipulation (Variable)” and the “ Math Formula (Variable) “ nodes.

In “String Manipulation (Variable)” node we apply this expression and append new variable named “new product name”:

join("prod", string($${IcurrentIteration}$$ + 10))

This expression joins “prod” and the current iteration number (which is also a flow variable produced by “Chunk Loop Start” node) plus 10.

Then in the “Math Formula (Variable)” node we apply this formula and replace the current “price” variable:

100 + $${IcurrentIteration}$$ * 50

As you see this formula has a base of 100 and adds 50 multiplied by current iteration number to it.

Now we have the desired product name and the price so let’s put them into our data table. Pick a “ Column Expressions “ node and define two expressions, one for the product names and the other one for the prices. Both of the expressions replace the corresponding columns.

Expression 1 (product names):

variable("new product name")

Expression 2 (prices):

variable("price")

These expressions replace the value of the “product name” and the “price” columns with the values form the “new product name” and the “price” variables.

And finally, the “ Loop End “ node to close the loop. So the final output should be like this:

Our introduction to “Flow Variables” ends here but you can learn more by yourself. There are so many nodes in KNIME for you to work exclusively with flow variables (keep in mind that all the nodes in KNIME can deal with flow variables) other than the nodes we mentioned, like: “Table Row To Variable”, “Table Row To Variable Loop Start”, “Table Column To Variable”, “Variable To Table Row”, “Variable To Table Column”, “Rule Engine Variable”, most of the nodes in “Quickforms” subcategory and more.

Now, you can build much more complex workflows with no fear as you know how to summon the KNIME spirits to serve you!

Originally published at https://blog.statinfer.com.

--

--