“警告:eval中的错误:无法将n转换为标量整数”

时间:2016-07-15 03:00:49

标签: r ggplot2 shiny dplyr

我正在尝试创建一个闪亮的应用程序,可以让您选择不同的统计方法来分析一些保险数据,然后以某种方式绘制结果。我希望能够在界面中选择一些值和方法的名称,然后有一个按钮来触发模型的创建,然后是另一个按钮来触发绘图创建。

我对Shiny App很新,我收到的错误消息是我无法弄清楚的。

“警告:eval中的错误:无法将n转换为标量整数”

这是我的代码:

library(shiny)
library(insuranceData)
library(caret)
library(randomForest)
library(dplyr)
library(ggplot2)
library(pls)
data(AutoBi)

ui <- fluidPage(
  # Application title
  titlePanel("Ratemaking Tool"),
  #fileInput("file", "Upload file"),
  actionButton("do", "Make Model"),
  actionButton("Go", "Make Graph"),
  selectInput("Model", "Model", c("lm", "glm", "pls")),
  sidebarLayout(
    sidebarPanel(
     sliderInput("percentiles",
              "Percentile Splits:",
              min = 1,
              max = 20,
              value = 5,round = TRUE),
  sliderInput("cv_splits",
              "Folds for K-fold validation:",
              min = 1,
              max = 10,
              value = 5,round = TRUE)),mainPanel(plotOutput("distPlot")
        )
    )
)


server <- function(input, output){
values <- reactiveValues(df_data = NULL)


observeEvent(input$do, {
    dataInput <- isolate(reactive({
     t <- AutoBi %>% na.omit() %>% mutate(log.age = log(CLMAGE + 1),log.loss      =     log(LOSS + 1))
  train = slice(t,1:700)
  test = slice(t,701:nrow(t)) %>% mutate(log.age = log(CLMAGE + 1),log.loss =     log(LOSS + 1))
  t1 <- select(train,-c(log.loss,LOSS,CLMAGE))

  ctrl <- isolate(trainControl(method = "cv",number = input$cv_splits))
  model <- train(x = t1[,c(2,3,4,5,6,7)],y = train$LOSS,trControl = ctrl,
                 method = input$Model)
  preds <- predict(model,test[,c(2,3,4,5,6,9)])
  test$pred <- preds

  ordered <- test %>% arrange(pred) %>% mutate(quantile = ntile(pred,input$percentiles)) %>% group_by(quantile) %>%
    summarize(avg_loss = mean(LOSS),avg_prd= mean(pred)) %>% mutate(flag = as.factor(ifelse(avg_loss - avg_prd > 0,0,1)))
  values$df_data = ordered
  return(ordered)
}))
values$df_data = dataInput()
})

 observeEvent(input$Go,{
 output$distPlot <- isolate(renderPlot({
 ggplot(values$df_data)+ aes(quantile,avg_loss,size = 1) + geom_point() +     geom_point(aes(y = avg_prd,color = "avg_pred")) + ggtitle(paste("Statistical Learning Technique:", input$Model)) + 
    geom_linerange(aes(ymin = avg_prd,ymax = avg_loss,size = .1,color =   flag)) + guides(size=FALSE)
  }))
    })
 }

   shinyApp(ui = ui, server = server)

0 个答案:

没有答案