【问题标题】:Shiny Deployment Error:Error in value[[3L]](cond) : object of type 'closure' is not subsettable闪亮的部署错误:值 [[3L]](cond) 中的错误:“闭包”类型的对象不是子集
【发布时间】:2019-12-30 22:23:01
【问题描述】:

我有一个shiny 应用程序在我的计算机上运行良好,但是当我将它部署到Shiny Apps 时,仍然出现以下错误:

值[3L] 中的错误:“闭包”类型的对象不是子集

我检查了我的代码,没有发现任何不正确地使用“闭包”的对象。我认为我的代码有问题,因为应用程序在本地运行良好。

有人说可能是因为数据框的导入路径。我很确定我的 csv 文件和应用程序在同一个文件夹中。我还尝试生成模拟数据(以避免读取该 csv 文件),但问题仍然存在。

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)

【问题讨论】:

  • 是否有一些输出可以帮助缩小问题的可能范围?!可能很难查看整个代码或首先运行应用程序以查看问题所在。
  • 尝试在本地机器上的新 R 会话中运行应用程序,看看是否仍然没有出现错误。
  • @Shree 非常感谢,这是检查它的好方法。我按照你说的做了,发现这是因为我的 [ui] 函数中的 [order]。我只在 [server] 函数中将 [order] 更改为 [order()] 并忘记了 [ui]。它在本地工作的原因是因为我已经在我的 rstudio 中读取了数据框 [order]。
  • @PingW 我猜是的。很高兴它有帮助。
  • @NelsonGon 非常感谢。我认为仅根据 Shinyapps.io 报告的错误消息来缩小问题所在可能并不容易。不像Rstudio可以同时报错和报错位置。

标签: r shiny


【解决方案1】:

错误

值[3L] 中的错误:“闭包”类型的对象不是子集

通常在您有一个与 R 中的内置函数命名相同的 data.frame 时发生。在您的情况下,我相信这是在您的 ui 函数中使用 order 。 如果脚本重新运行,则此变量未定义,R 将选择函数 order - 它不是子集,因此会出现错误消息。

【讨论】:

  • 非常感谢。这正是我的代码的问题。
  • 很高兴它解决了您的问题。如果您发现答案有用,请考虑投票和/或将其标记为正确答案。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
相关资源
最近更新 更多