# Idiots guide to using R in KNIME

Dear Users.

I am trying to get KNIME to work with R.

Is there a simple to follow guide that will at least get me to run a R model in KNIME?

regards

Hi,

Please send me any questions that you have and I'll help out.

We're currently writing a guide with KNIME Press - stay tuned in !

regards,

Mike M

Hey Mike,

When can we expect the guide to be out? Could you please post the link to the guide here?

Thanks

Vittesh

Hi Vittesh,

When the book is released there will be links to it on the KNIME site, as per the first book "Beginner's Luck".

regards,

Mike

Hello Mike,

I am a bit confused with the R snippet as well. I would like to run the code I've pasted below and would also like to know if there is a tutorial, link or Help page with the R snippet, learner and predictor.

1200 1.375
4000 0.464
1333.33 0.148
444.44 0.047
148.148 0.014
49.383 0.005
16.461 0.004"),

with(z,plot(X,Y1))
with(z[-1,],plot(X,Y1))
tmpf <- function(X,a,b,cc,d) d + ((a-d)/(1+(X/cc)^b))

## fix a and d to reasonable values, fit b and cc
n1 <- nls(Y1~tmpf(X,a=0.7,b,cc,d=0),
data=z[-1,],
start=list(cc=1000,b=-1))

## now use b and cc as starting values, fit a and d
n2 <- nls(Y1~tmpf(X,a,b=-1.721,cc=2738,d),
data=z[-1,],
start=list(a=0.7,d=0))

## now fit all simultaneously
n3 <- nls(Y1~tmpf(X,a,b,cc,d),
data=z[-1,],
start=as.list(c(coef(n1),coef(n2))))
coef(n3)

Thank you

Varun

Dear all,

I use R in Knime a lot as it is a really nice way to package R functions into small "lego bricks" for re-use.

The two nodes I use the most are  (Local) R Snippet and R View. Make sure you have a working copy of R on your machine and you have set up the path in the KNIME preferences <File/Preferences/KNIME/R>

For the R Snippet (Local) you need to always return data to the R object R<- <something> also the input to the node is called R also, so here is an example:

#================
# Simple "R Snippet" I
#================
data_subset<-R[,7:34]  #remove first 6 rows
R<-data_subset             #return the subset to out-port

You can also use individual variables from the in-port by double clicking the column list in the Configure dialog: (here we select Var10 and Var22)

#================
# Simple "R Snippet" II
#================
selected_variables<-cbind(R\$Var10, R\$Var22)  #select columns 10 and 22
R<-selected_variables                                       #return the subset to out-port

I quite often strip the text feilds out of a datframe, do some maths then re-bind the text fields like so:

identifiers<-R[,1:6]
data_subset<-R[,7:34]
<do stuff on data_subset>
R<-cbind(identifiers,data_subset)

One frustrating feature is that you can't pass two inputs to the R Snippet so you always have to pass the data back as columns or rows to the R output and then pull this data back out in subsequent nodes. For instance with PCA I add the scores and loadings alongside the original data and split them out again later.

The R View (local) is where you put R plots. Its pretty easy to use and the R plot will be seen by right clicking the node and selecting View: RView. Here is a simple example:

#================
# Simple "R View" III
#================
R<-as.matrix(R)
plot(R[1,], type="l", main="Spectrum No 1", xlab="Wavelength", ylab="Intensity")

Note with R View there is no need to return the result to R<-

One other thing to be aware of is that if you need a special library in R you need to specify it with

Require('library_name')

where 'library_name' is the name of the library i.e. PCAmethods or whatever, AND you have to have installed it in your R environment previously.

I've dabbled a bit with the R Learner and R Predictor but there seems to be a bug in the predictions always being done on the training set see my other post:(http://tech.knime.org/forum/r-statistics-nodes-and-integration/serious-bug-in-r-predictor-always-predicts-training-set-not)

I hope this helps you get started, It was confusing for me at first but now I have a huge library of little data analysis functions I can use as "Lego bricks" to design new workflows. I love the self contained nature of each node and it forces you into good programming practices!

Cheers,

Mark

Mark,

thx for the help.I got the point: always send what is computed to knime col with the "R<-foo_just_computed" instruction. However, what I do not get is how should I handle to send two foo_1 and foo_2. Each one of them is rightly computed, I did not find a way to send the two the last writtent one is always the one that get out.

ex:

R<-R

X <-R\$"column_X"
Y<-R\$"column_Y"
Z<-X+Y

how go I return X,Y,Z as three separates cols in knime?

thx.

bruno

Hi Bruno,

Try: R<-cbind(x,y,z) which will combine columns,

Or if you are using the labs nodes: knime.out <-as.data.frame(cbind(x,y,z))

Regards,

Aaron

Aaron,

thx for the tip, and thx for the ref url.
works just the way I needed.

best.

bruno

Hi,

I am currently using Knime 3.1 version and i want to run R integration example from example server which is already available in Knime. I am using R 3.2.3 version for 64bit windows OS. When i downloaded that workflow and when i try to configure R source node it is showing that "R home invalid". How can i solve these error?

Hi M.R.H.Prasanna!

This error means that you configured your RHome incorrectly (or did not at all). The KNIME Analytics Platform requires a installation of R to function correctly. In your case, you are using a custom installation (in comparision to using the plugin which provides a R installation). You just need to tell KNIME where to find it:

1. Open the KNIME Analytics Platform

2. Select File > Preferences from the top menu bar

3. Locate the "R" preference page under "KNIME"

4. For the "Path to R Home" select the R-3.2.3 directory of your R installation.

You might then need to restart the Application.

Hope this helps,

Jonathan.

Hi Jonathan,

I did in the same way as you said and it works for me. Thank you