Reputation: 427
I am building an interface using RShiny that displays the results of ARM rules created through ARULES package. Its a simple display app that lets the user choose the metric (Confidence or Support or Lift) and the number of rows to display (5 or 10 or 15 or 20). Front end looks like this:
The R code running behind just sorts by the metric given and displays the number of rows as chosen in second radion button. The issue I face is , when I choose "Support" in first radiobutton, the results go blank on the RShiny screen though the records are displayed in RStudio. This happens occasionally as app works works perfectly sometimes and at times, it doesnt show the results though the results will be visible on my rStudio. What could be the reason for it?
RShiny Code below:
ui <- fluidPage(
titlePanel("Movie Recommendation - ARM"),
sidebarLayout(
sidebarPanel(
helpText("Sort rules based on metric fed in. Include how many rules are needed to be displayed"),
radioButtons("var",
label = "Choose a metric",
choices = c("Confidence","Lift","Support"),
selected = "Confidence"),
radioButtons("range",
label = "Choose Number of rules to display",
choices = c(5,10,15,20),
selected = 5)
),
#mainPanel(tableOutput("rule"))
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Rules",DT::dataTableOutput("rule"))
)
)
)
)
server <- function(input, output) {
output$rule <- DT::renderDataTable({
#output$rule <- reactive({
# browser()
sw <- switch(input$var,
"Confidence" = "confidence",
"Lift" = "lift",
"Support" = "supp")
range1 = input$range
a=sort_rules(sw,range1)
})
}
# Run app ----
shinyApp(ui, server)
sort_rules (Function) below
sort_rules = function(metric1,k)
{
#browser()
rules <- readRDS("rules.RDa")
print(paste0("Number of rules read:",length(rules),sep = " "))
k = as.numeric(k)
rules_sort <- sort (rules, by=metric1, decreasing=TRUE)
print(length(rules_sort))
pl = inspect(head(rules_sort,k))
return(pl)
}
I checked using "browser()" and it appears that the 'pl' in the above function comes out as blank sometimes. But the results are displayed in RStudio and not on RShiny App.
Additional information, following function creates the rules.RDa:
readTran = function(sup,con)
{
sup = as.numeric(sup)
con = as.numeric(con)
data = readRDS("Rules_transaction.RDa")
rules = arules::apriori(data, parameter = list(supp = sup, conf = con))
subsetRules <- which(colSums(is.subset(rules, rules)) > 1) # get subset rules in vector
length(subsetRules) #> 3913
rules <- rules[-subsetRules] # remove subset rules.
saveRDS(rules,file = "rules.RDa")
}
sup and con are Support and Confidence respectively. I used 0.1 and 0.7 while creating rules.RDa from Rules_transactions.RDa.
Rules_transactions.RDa data is uploaded on below given github link: https://github.com/vjkadekar/madeup_data_ARM (this is purely a made up data and no client information is used here)
and its csv version is uploaded as my_data.csv on same link.
Upvotes: 0
Views: 134
Reputation: 427
I am answering my own question here. The problem was with pl = inspect(head(rules_sort,k))
Turns out that I cannot capture output of an inspect statement and it can only be viewed on RStudio panel.
Changed the code to this:
sort_rules = function(metric1,k)
{
rules <- readRDS("rules.RDa")
k = as.numeric(k)
rules_sort <- sort (rules, by=metric1, decreasing=TRUE)
# Removed Inspect in below line and just stored head of the rules as dataframe.
pl = as(head(rules_sort,k),"data.frame")
return(pl)
}
This still doesn't explain why sometimes the display was visible on RShiny, but that could be a question for some other day.
Upvotes: 0