I am using the r tool to plot some data I need in a very specific way (why I am using r tool and not the graphing tool). I have the r tool running in a macro. My macro seems to be the bottle neck in slowing down my output (went from a few mins to over an hr).
Looking at my macro and watching it run it seems that the r tool has to load each and every time. Is there a better way to have r load so that the packages do not need to reload each time? Or does anyone have a resource that can show me how to process groups of data into plots in R and back out in those groups to an Alteryx workflow.
This takes 4-7 seconds each run.
Solved! Go to Solution.
Hello Jeh,
Thanks for posting your question.
The read.Alteryx.First() and read.Alteryx.Next() functions allow you to “chunk” your input data by providing an argument that specifies the number of rows you want to read in. Reading in the first chunk of data as a data frame would look something like this:
the.data <- read.Alteryx.First("#1", 5000, mode = "data.frame")
You can have a look on our full R tool documentation :
Hope this helps,
Zak
Regarding the installation, are all the packages you reference installed locally? I want to make sure you're not trying to load and install a package each time this runs.
The R tool has the ability to produce multiple graphs suing a "for loop". In the sample below, I have a Group field that I build in the workflow before going into the R tool that I use to batch on:
AlteryxGraph(1)
#Batch graph output for each unique "group by" configuration.
for (i in unique(input$Group))
{
#Set graph data for each group:
plot.data = subset(input,input$Group==i)
#Plot settings:
barplot(
height=plot.data$PCT)
)
invisible(dev.off())
gc()
Here's the problem: this example will output a record for each graph from output(1), but I've never found a way to output any other fields with it. That means your output is going to look like this and you're probably going to need a way to join the Group field back to the records. I had to use some tools to replicate the record sorting the R tool does and joined on position.
I'm calling in "library(ggplot2)" in at the start of the r code. Is there a better way to do that because without calling it I believe that it will not work. I assume I cant just install that package into alteryx and it be available.
You can install packages in the appropriate Alteryx R folder if you'd like them available. The path should go something like this:
C:\Program Files\Alteryx\R-3.4.4\library
but if you're calling "library(", then it should already be there.
Couldn't you just write to one of the outputs and then join it with the graphs?
write.Alteryx(DATASOURCELOCATION, 5)
So I am only get 1 graph (and only because I am calling the plot outside of the loop) and it is only the last graph to run. (I put a message output to see if I was getting it 3 times in the results which I am). Why am I not getting 3 graphs?
Please dont judge my code to harsh I had to piece this together as I dont know R.
library(ggplot2) cd <- read.Alteryx("#1", mode="data.frame") AlteryxGraph(1, width=1008, height=298) #Batch graph output for each unique "group by" configuration. for (i in unique(cd$USN)) { #Set graph data for each group: plot.data = subset(cd,cd$USN==i) #Plot settings: p <- ggplot(data=plot.data, aes(x=factor(Dates), y=Counts, fill=Group)) + geom_bar(stat="identity", width = .8, position=position_dodge(), colour="black") + xlab("Kroger Weeks") + ylab("Units") + scale_fill_manual(values=c("#88B4F7", "#FF9333")) + theme(legend.position="bottom", legend.title = element_blank()) + geom_text(aes(label=Counts), vjust=1.6, color="white", position = position_dodge(0.8), size=3.5) + ggtitle(plot.data$USN) + theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=16, hjust=0.5)) p + scale_x_discrete("Kroger Weeks", labels=c("11" = "Early", "12" = "P3W4", "13" = "P4W1", "14" = "P4W2", "15" = "P4W3", "16" = "P4W4", "17" = "P5W1", "18" = "P5W2", "19" = "P5W3", "20" = "P5W4", "21" = "P6W1", "22" = "P6W2", "23" = "P6W3", "24" = "P6W4", "25" = "P7W1", "26" = "P7W2", "27" = "P7W3", "28" = "P7W4", "29" = "P8W1", "30" = "P8W2", "31" = "P8W3", "32" = "P8W4")) AlteryxMessage("How Many Times", msg.consts$INFO, priority.consts$LOW) p } p #I ONLY GET AN OUTPUT IF I PUT THIS HERE (last graph) invisible(dev.off()) gc()
Nothing jumps out as incorrect, but the redundant 'p' is odd. I assume you have more than 1 unique value in the USN field you send to the R tool, yes?
Could you share a workflow/sample data?
I haven't had time to get a solution yet, but I do have a few thoughts:
- Your text input has different capitalization of "k" in the USN field.
- I can get two basic barplot( graphs to output, but not the ggplot graphs.
- I think the original version is looping correctly (your message appears twice, smart addition), but it's overwriting 'p' which is why only one record is output.
I'll let you know if I come up with anything helpful, but R isn't my strong suit.
User | Count |
---|---|
19 | |
15 | |
15 | |
9 | |
8 |