Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

R tool error - prasing and evaluation

martin22
5 - Atom

This is my first time using the R tool in Alteryx, so I'm hoping my error is trivial, if embarrassing...

 

I'm trying to use R to parse and evaluate an expression in a data frame.  When I write it purely as code, it works.  When I try to use an input it does not, I just get an output of 1.  I have no idea why.

 

The code only version which works is:

#create a basic data frame df
df <- data.frame(matrix(ncol = 3, nrow = 2))
x <- c("age", "formula", "evaluation")
colnames(df) <- x
df$age[1]<-54
df$age[2]<-20
df$formula[1]<-"df$age*5"
df$formula[2]<-"df$age*5"

 

#parse, evaluate and output the expression
df$evaluation<-eval(parse(text=df$formula))
write.Alteryx(df, 2)

 

So i get:

ageformulaevaluation
54df$age*5270
20df$age*5100

 

However if instead of using the R code to generate the dataframe and have an input table, the exact same code results in the evaluations both being 1, instead of 270 and 100.

 

I've attached the workflow, which shows where it works and where it doesn't.  I'd be very grateful for any support.

 

I have a large table, ~100k rows which each have a formula / expression in the formula field which need evaluating and I'm trying to use R to do it but can't get past this stumbling block.  Also in the future I'd likely want to use R for a few other things.

2 REPLIES 2
BrandonB
Alteryx
Alteryx

I was able to get this to work. For some reason just reading in the formula as a string doesn't allow the R code to parse and eval. I just manually set df$formula as.character.

 

df<-read.Alteryx("#1", mode="data.frame")
df$formula<-as.character(df$formula)
df$evaluation<-eval(parse(text=df$formula))
write.Alteryx(df , 1)

martin22
5 - Atom

Super, thanks.

Labels