在Shiny反应式表达式中返回cspade()对象

时间:2016-11-22 10:23:19

标签: r shiny arules reactive

我需要从Shiny应用程序中的反应式表达式返回一个cspade()对象,通过另一个反应式表达式使用它并生成一个ruleInduction()对象。代码如下:

## server.R
library(shiny)
library(data.table)
library(arulesSequences)

# Read Data
x <- fread(input = "Data/ITCOPAQuantity.csv"
           ,colClasses = c("character","integer","character","integer"))
setnames(x, colnames(x), c("cid","date","item","size"))
x[,size:=NULL]
x[,cid:=as.integer(substr(cid,nchar(cid)-6,nchar(cid)))]
x <- unique(x)
setorder(x, cid, date)
x[,TID:=.GRP, keyby=.(cid, date)]

x <- data.frame(x)
x <- x[with(x, order(cid, date)), ]

d <- as(split(x$item, x$TID), "transactions")

# Add keys
transactionInfo(d)$sequenceID <- sapply(split(x$cid, x$TID), min)
transactionInfo(d)$eventID    <- as.integer(sapply(split(x$date, x$TID), unique))

shinyServer(function(input, output) {

# Generating a CSPADE object based on input values
makeRuleDT <- reactive({
xcs <- cspade(d
              ,parameter = list(support=input$support
                                ,maxsize=input$maxsize
                                ,mingap=input$mingap
                                ,maxgap=input$maxgap)
              ,control = list(tidLists=TRUE))
xcs
})

# Use CSPADE object from reactive expression above as input
# to generate rules and output result in data.frame()     
makeFinDT <- reactive({
  xcs <- makeRuleDT()
  rule <- ruleInduction(xcs
                        ,confidence=input$confidence
                        ,control=list(tidLists=TRUE))

xfin <- data.table(as(subset(rule, lhs(rule) %ain% c("CRP")), "data.frame"))
if(input$exact==TRUE){
  xfin <- subset(xfin, grepl("?<\\{CRP\\},\\{UA\\}?>",rule))
}
as.data.frame(xfin)
})

output$ruleDT <- renderDataTable({
  makeFinDT()
})

})

当我运行应用程序时,我收到以下错误消息: 错误:未找到对象'规则'

我怀疑反应函数以某种方式以错误的方式返回对象或根本不返回。但我不知道如何解决它。

非常感谢任何帮助!

0 个答案:

没有答案