Search code examples
ralteryxanomaly-detection

Anomaly Detection (Twitter package) Output in Alteryx using R Tool


While using the twitter Anomaly Detection package for R in Alteryx, I am not able to output the anomalies which I otherwise get when I run the below code in R Studio. The input is basically a data frame with the first column as time stamps and the other is score(numeric).

The error : 'There was an error in WriteYXDBStreaming'. The code runs without the write.alteryx statement though. I'm looking to get a data frame for anomalies consisting of time stamps and score (based on the original data frame).

DF <- read.Alteryx("#1",mode="data.frame")
altx.repo <- getOption("repos")
altx.repo["CRAN"] <- "http://cran.rstudio.com"  # set your primary repo if you haven't already
options(repos = altx.repo)
package_name <- "AnomalyDetection"
if(package_name %in% rownames(installed.packages()) == FALSE){
    install.packages(package_name)
    }
library("AnomalyDetection")
DF$Date <- as.POSIXct(DF$Date)
Model <- AnomalyDetectionTs(DF, max_anoms=0.49, direction='both')
Anoms <- as.data.frame(Model$anoms)
write.Alteryx(Anoms,1,TRUE)

enter image description here


Solution

  • This error is occurring in the Write package. I don't have an explanation why but cast your variables in the output dataframe to one type (character/numeric) in order to write the data back into Alteryx as below.

    data3 <- lapply(DF, as.character)
    write.Alteryx(data3, 3)