Help in using flow variables in R snippet

Hi guys,

I'm using the R snippet node present in the KNIME Labs – R (Interactive). As I'm new using flow variables, I need your help to do something that is easy in theory: use the value of a flow variable in the R snippet as a variable name.

I will explain it better. I have the following working R snippet code:

lm.fit=lm(housecost~.,data=knime.in)

which builds a linear model using the variable “housecost” as response,and which is present in the knime.in data. Now I want to generalize it and I want to pass to the lm.fit model the response variable name (in this case “housecost”) using a flow variable called “response” which value is “housecost”.

I tried the following:

lm.fit=lm(knime.flow.in[["response"]]~.,data=knime.in)

but it gave me the error:

Error in model.frame.default(formula = knime.flow.in[["response"]] ~ ., :

variable lengths differ (found for 'X')

 

 

I also tried:

response<-"FLOWVAR(response)"

lm.fit=lm("response"~.,data=knime.in)

But it gave me a similar error.

I'm sure I'm missing something obvious, but I cannot understand from basic documentation. Please, can you help me to understand?

Gio

My guess (!) is that the problem might stem from the formula mode in lm() - AFAIR, it would expect the variable name without "". Maybe there is an alternative way of formulating lm()? e.g. as.formula() allows you to construct a formula expression with quoted strings, which would then look like lm(as.formula(paste0(knime.flow.in[["response"]], " ~ .")), data = knime.in). Not sure whether that works...

Another possibility is to use the flow variable in Knime to change the name of the responsevar to a generic name just before handing the data set to the R node.