0
votes

I have a shiny appthat works fine on my computer but when I deploy it to Shiny Apps, the following error keeps appearing:

Error in value[3L] : object of type 'closure' is not subsettable

I checked my code and didn't find any object that used 'closure' improperly. I think there's something wrong with my code since the app runs fine locally.

Some people say that maybe it is because of the dataframe's import path. I am pretty sure my csv file and app are in the same folder. I also tried to generated mock data (to avoid reading in that csv file) but the problem persists.

library(shiny)
library(ggplot2)
ui <- fluidPage(
   titlePanel("AT&T Segmentation Visualization"),
   numericInput("size", "Sample Size (Maximum Value 2559873):", 5000, min = 1, max = 2559873),
   sidebarLayout(
    sidebarPanel(width=2,
   sliderInput("recency", "Recency:",
               min = 1, max = 5,
               value = c(4,5), step=1),
   sliderInput("frequency", "Frequency:",
               min = 1, max = 5,
               value =c(4,5) , step=1),
   sliderInput("monetary", "Monetary:",
               min = 1, max = 5,
               value = c(4,5),step=1),
   #numericInput("size", "Sample Size:", 5000, min = 1, max = 2559873),
   actionButton("update", "Run")
 ),
  # Show a plot of the generated distribution

  mainPanel(
    tabsetPanel(type = "tabs",
                tabPanel("Summary", 
                         fluidRow(
                           tableOutput("values"),
                           column(4,
                                  selectInput("sseg",
                                              "Industry Segmentation:",
                                              c("All",
                                                unique(as.character(order$Segment))),selected = "All", multiple = TRUE)
                           ),
                           column(4,
                                  selectInput("smake",
                                              "Device Make:",
                                              c("All",
                                                unique(as.character(order$DeviceMake))),selected = "All",multiple = TRUE)
                           ),
                           column(4,
                                  selectInput("stype",
                                              "Device Type:",
                                              c("All",
                                                unique(as.character(order$DeviceType))),selected = "All",multiple = TRUE)
                           ),
                           column(4,
                                  selectInput("sctype",
                                              "Device Contract Type:",
                                              c("All",
                                                unique(as.character(order$DeviceContractType))),selected = "All",multiple = TRUE)
                           ),
                           column(4,
                                  selectInput("sclen",
                                              "Device Contract Length:",
                                              c("All",
                                                unique(as.character(order$DeviceContractLength))),selected = "All",multiple = TRUE)
                           ),
                           column(4,
                                  selectInput("sclot",
                                              "Line Order Type:",
                                              c("All",
                                                unique(as.character(order$LineOrderType))),selected = "All",multiple = TRUE)
                           )
                         ),
                           DT::dataTableOutput("table")),
                tabPanel("Analysis",
                           fluidRow(
                                        column(4,
                                        sliderInput("recency2", "Recency2:",
                                                    min = 1, max = 5,
                                                    value = c(1,2), step=1)),

                                        # Input: Decimal interval with step value ----
                                        column(4,
                                        sliderInput("frequency2", "Frequency2:",
                                                    min = 1, max = 5,
                                                    value =c(1,2) , step=1)),

                                        # Input: Specification of range within an interval ----
                                        column(4,
                                        sliderInput("monetary2", "Monetary2:",
                                                    min = 1, max = 5,
                                                    value = c(1,2),step=1)),

                                        actionButton("update1", "Select")
                           ),
                         plotOutput("spm"),
                         fluidRow(
                           column(4,
                                  selectInput("f1",
                                              "X-axis:",
                                              unique(as.character(colnames(order))),selected = "Segment",multiple = FALSE)
                                  ),

                           # Input: Decimal interval with step value ----
                           column(4,
                                  selectInput("f2",
                                              "Y-axis:",
                                              unique(as.character(colnames(order))),selected = "DeviceMake",multiple = FALSE)
                           ),
                           actionButton("update2", "Scartter Plot")
                         ),
                         plotOutput("barseg"),
                         plotOutput("barseg2"),
                         plotOutput("bardevicemake"),
                         plotOutput("bardevicemake2"),
                         plotOutput("type"),
                         plotOutput("type2"),
                         plotOutput("ctype"),
                         plotOutput("ctype2"),
                         plotOutput("clength"),
                         plotOutput("clength2"),
                         plotOutput("clot"),
                         plotOutput("clot2")
                         )
    )
    )
    )
    )

server <- function(input, output){
 so=read.csv(file="order.csv")
 order<-reactive({
 s=sample(1:2558773,size=input$size)
 so[s,]
 })
sliderValues <- eventReactive(input$update,{
linshi=order()[(order()$R %in%    seq(input$recency[1],input$recency[2]))&(order()$F %in%  seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]

x=data.frame(
  Name = c("FAN",
           "Order",
           "Monetary"
  ),
  Value = as.character(c(length(unique(linshi$fan)),
                         length(unique(linshi$Order_ID)),
                         sum(linshi$moneyindex)
  )),
  Total = as.character(c(87432,
                         2420196,
                         10921947
  )),
  Percentage = as.character(c(paste(round(100*(length(unique(linshi$fan))/87432), 2), "%", sep=""),
                              paste(round(100*(length(unique(linshi$Order_ID))/2420196), 2), "%", sep=""),
                              paste(round(100*(sum(linshi$moneyindex)/10921947), 2), "%", sep="")
  )),
  stringsAsFactors = FALSE)

})

output$values <- renderTable({
sliderValues()
})

spm<-eventReactive(input$update2,{
data1=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]
data2=order()[(order()$R %in% seq(input$recency2[1],input$recency2[2]))&(order()$F %in% seq(input$frequency2[1],input$frequency2[2]))&(order()$M %in% seq(input$monetary2[1],input$monetary2[2])),]
data1$cat='c1'
data2$cat='c2'
x=intersect(data1$fan,data2$fan)
data=rbind(data1[!data1$fan %in% x,],data2[!data2$fan %in% x,])
i1=input$f1
i2=input$f2
t1<-aggregate(as.formula(paste('fan~',i1, '+', i2)), data=data,function(x) length(unique(x)))
t2<-aggregate(as.formula(paste('fan~',i1, '+', i2)), data=data1,function(x) length(unique(x)))
t3<-merge(x=t1,y=t2,by=c(colnames(t2)[c(1,2)]),all.x=TRUE)
t3$n=t3[,4]/t3[,3]
(p <- ggplot(t3, aes_string(input$f1, input$f2)) + geom_tile(aes(fill = n), colour = "white") + scale_fill_gradient(low = "orange", high = "steelblue"))+theme(axis.text.x = element_text(angle = 90, hjust = 1))+labs(title=paste("Scatter Plot (",as.character(length(unique(x))),"FANs are not plotted because of duplicates )"))+scale_color_manual(values=c("blue", "orange"))
})
segplot<- eventReactive(input$update,{
rdata=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]
data=rdata[!duplicated(rdata[,c('fan','Segment')]),][,c('fan','Segment')]
count=reshape2::melt(table(data$Segment))
ggplot(data=count, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="blue")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Segment")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
 })
segplot2<-eventReactive(input$update1,{
rdata2=order()[(order()$R %in% seq(input$recency2[1],input$recency2[2]))&(order()$F %in% seq(input$frequency2[1],input$frequency2[2]))&(order()$M %in% seq(input$monetary2[1],input$monetary2[2])),]
data2=rdata2[!duplicated(rdata2[,c('fan','Segment')]),][,c('fan','Segment')]
count2=reshape2::melt(table(data2$Segment))
ggplot(data=count2, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="orange")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Segment2")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
make<- eventReactive(input$update,{
data=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]
count=reshape2::melt(table(data$DeviceMake))
ggplot(data=count, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="blue")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Make")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
make2<-eventReactive(input$update1,{
data2=order()[(order()$R %in% seq(input$recency2[1],input$recency2[2]))&(order()$F %in% seq(input$frequency2[1],input$frequency2[2]))&(order()$M %in% seq(input$monetary2[1],input$monetary2[2])),]
count2=reshape2::melt(table(data2$DeviceMake))
ggplot(data=count2, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="orange")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Make 2")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
type<- eventReactive(input$update,{
data=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]
count=reshape2::melt(table(data$DeviceType))
ggplot(data=count, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="blue")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Type")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
type2<-eventReactive(input$update1,{
data2=order()[(order()$R %in% seq(input$recency2[1],input$recency2[2]))&(order()$F %in% seq(input$frequency2[1],input$frequency2[2]))&(order()$M %in% seq(input$monetary2[1],input$monetary2[2])),]
count2=reshape2::melt(table(data2$DeviceType))
ggplot(data=count2, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="orange")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Type 02")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})

ctype<- eventReactive(input$update,{
data=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]
count=reshape2::melt(table(data$DeviceContractType))
ggplot(data=count, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="blue")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Contract Type")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
ctype2<-eventReactive(input$update1,{
data2=order()[(order()$R %in% seq(input$recency2[1],input$recency2[2]))&(order()$F %in% seq(input$frequency2[1],input$frequency2[2]))&(order()$M %in% seq(input$monetary2[1],input$monetary2[2])),]
count2=reshape2::melt(table(data2$DeviceContractType))
ggplot(data=count2, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="orange")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Contract Type 02")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
clength<- eventReactive(input$update,{
data=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]
count=reshape2::melt(table(data$DeviceContractLength))
ggplot(data=count, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="blue")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Contract Length")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
clength2<-eventReactive(input$update1,{
data2=order()[(order()$R %in% seq(input$recency2[1],input$recency2[2]))&(order()$F %in% seq(input$frequency2[1],input$frequency2[2]))&(order()$M %in% seq(input$monetary2[1],input$monetary2[2])),]
count2=reshape2::melt(table(data2$DeviceContractLength))
ggplot(data=count2, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="orange")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Device Contract Length 02")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
clot<- eventReactive(input$update,{
data=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),]
count=reshape2::melt(table(data$LineOrderType))
ggplot(data=count, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="blue")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Line Order Type")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
clot2<-eventReactive(input$update1,{
data2=order()[(order()$R %in% seq(input$recency2[1],input$recency2[2]))&(order()$F %in% seq(input$frequency2[1],input$frequency2[2]))&(order()$M %in% seq(input$monetary2[1],input$monetary2[2])),]
count2=reshape2::melt(table(data2$LineOrderType))
ggplot(data=count2, aes(x=Var1, y=value)) + geom_bar(stat="identity",color="white", fill="orange")+theme(axis.text.x = element_text(angle = 90, hjust = 1))+geom_text(aes(label=value), vjust=-0.3,size=3.5)+labs(title="Line Order Type 02")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
})
 output$table <- DT::renderDataTable(DT::datatable({
  data=order()[(order()$R %in% seq(input$recency[1],input$recency[2]))&(order()$F %in% seq(input$frequency[1],input$frequency[2]))&(order()$M %in% seq(input$monetary[1],input$monetary[2])),][,c(-1,-2,-4)]


if (input$sseg != "All") {
  data <- data[data$Segment == input$sseg,]
}
if (input$smake != "All") {
  data <- data[data$DeviceMake == input$smake,]
}
  if (input$stype != "All") {
    data <- data[data$DeviceType == input$stype,]
  }
if (input$sctype != "All") {
  data <- data[data$DeviceContractType == input$sctype,]
}
if (input$sclen != "All") {
  data <- data[data$DeviceContractLength == input$sclen,]
}
if (input$sclot != "All") {
  data <- data[data$LineOrderType == input$sclot,]
}
data
}))


output$barseg=renderPlot({
segplot()
})
output$barseg2=renderPlot({
segplot2()
})
output$bardevicemake=renderPlot({
make()
})
output$bardevicemake2=renderPlot({
make2()
})
output$type=renderPlot({
type()
})
output$type2=renderPlot({
type2()
})
output$ctype=renderPlot({
ctype()
})
output$ctype2=renderPlot({
ctype2()
})
output$clength=renderPlot({
clength()
})
output$clength2=renderPlot({
clength2()
})
output$clot=renderPlot({
clot()
})
output$clot2=renderPlot({
clot2()
})
 output$spm=renderPlot({
spm()
})
}
# Run the application 
 shinyApp(ui = ui, server = server)
1
Is there some output that can help narrow down where the issue might be?! It might be hard to review the whole code or first run the app to see what the issue is. - NelsonGon
Try running the app in a fresh R session on you local machine and see if you still don't get an error. - Shree
@Shree Thank you so much, this is a wonderful way of checking it. I did what you said, and fount out it is because of the [order] in my [ui] function. I only changed [order] to [order()] in the [server] function and forgot about [ui]. The reason why it worked locally is because I already have the data frame [order] read in my rstudio. - Ping W
@PingW I guessed so. Glad it helped. - Shree
@NelsonGon Thank you so much. I think it may not easy to narrow down where the issue is just base on the error message reported by Shinyapps.io. It's not like Rstudio that can report the error and the location of the error at the same time. - Ping W

1 Answers

0
votes

The error

Error in value[3L] : object of type 'closure' is not subsettable

typically occurs when you have a data.frame named the same as the built-in functions in R. In your case, I believe it's the use of order in your ui function. If the script is run afresh, this variable is not defined and R will instead pick the function order - which is not subsettable, hence the error message.