Alteryx Designer Desktop Discussions

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

Read R output as is

nabegh
7 - Meteor

Hi,

 

If I have the following code, for example

 

shapiroNormality <- shapiro.test(data$var1)
write.Alteryx(shapiroNormality, 1)

and I use a Browse tool to read the output, I get the results in a table. 

 

Is there a way to read R output as is?

 

Thanks.

6 REPLIES 6
JohnJPS
15 - Aurora

I'm curious what you were looking for other than the values in the table.  I ran a shapiro.test and the output from it is the same as the table (data frame, or vector) that we see in Alteryx.

nabegh
7 - Meteor

Yes John. My question is actually about the format not the content. Can I read R's output as if it was written to the console? 

DylanB
Alteryx Alumni (Retired)

Based on the documentation here https://stat.ethz.ch/R-manual/R-devel/library/stats/html/shapiro.test.html, the output of the shapiro.test function is a list with elements 'statistic', 'pvalue', 'method', and 'dataname'. Alteryx's only way to read this in stream is as a dataframe type of object. So there's no super nice way to see the R output exactly as it pops up in your RStudio console. You can use the print method in R and look in the messages in Alteryx; I think that's the closest you can probably get.

nabegh
7 - Meteor

This is failing

 

wilcoxPoints <- wilcox.test(data_all$points~data_all$source, data=data_all) 
write.Alteryx(wilcoxPoints, 1)

R (16) Error in data.frame(statistic = 570563.5, parameter = NULL, p.value = 0.646636541214278, :
R (16) arguments imply differing number of rows: 1, 0
R (16) Calls: write.Alteryx ... as.data.frame -> as.data.frame.list -> eval -> eval -> data.frame

JohnJPS
15 - Aurora

@nabegh, I believe you can use capture.output in order to gather output from R to a variable, but you still need to massage it a little.

 

Fortunately Alteryx is great at massaging data: I've attached a workflow that supplies the output as seen on the console as 4 rows of character data.  Hope that helps!

DylanB
Alteryx Alumni (Retired)

Okay. It looks like the error you are getting is stemming from the fact that R is providing a list which is being coerced into a dataframe for Alteryx to use, and it is converting param = NULL into an empty list instead of param = c(NULL).

 

Here's what I came up with (obviously, change the value of w to be the result of the wilcox test):

 

w <- wilcox.test(rnorm(100, mean = 5, sd = 3))

convertForAlteryx <- function(name) {
	if(is.null(w[[name]])) {
		c(NA*1)
	} else {
		w[[name]]
	}
}

wdf <- as.data.frame(lapply(names(w), FUN = convertForAlteryx))
names(wdf) <- names(w) write.Alteryx(wdf)

 

Or if you prefer this vertically,

 

w <- wilcox.test(rnorm(100, mean = 5, sd = 3))

convertForAlteryx <- function(name) {
  if(is.null(w[[name]])) {
    NA*1
  } else {
    w[[name]]
  }
}

ids <- names(w)

vals <- unlist(lapply(names(w), FUN = convertForAlteryx))

wdf <- data.frame(ids, vals)

write.Alteryx(wdf)
Labels