R Shiny ggplot2支持向量机趋势线

时间:2017-04-05 17:39:04

标签: r ggplot2 shiny svm

我多年来一直对此感到沮丧;我无法看到趋势线:

library(shiny)
ui = pageWithSidebar(
  headerPanel("Twitter Engagement Correlations"),
    sidebarPanel(
      selectInput("c", label = "Variable:",
                     choices = colnames(dataset))
      ),
  mainPanel(
    plotOutput('plot1')
  )
)
server = function(input, output) {

  reactive({
    library(e1071)
    regressor = svm(Engagements ~ Character.count, 
                    data = dataset,
                    type = 'eps-regression')
  })

  output$plot1 = renderPlot({
    print(ggplot() +
      geom_point(aes(x = dataset[,input$c], y = dataset$Engagements),
                 colour = 'red') +
        geom_line(aes(x = dataset$Character.count, y = predict(regressor, newdata = dataset)),
                  colour = 'blue') +
      ggtitle('SVR Regression Model') +
      xlab(input$c) +
      ylab('Engagements'))
    })
}
    shinyApp(ui = ui, server = server)

错误代码' object' regressor'找不到'出现了,我不知道如何解决这个问题? 提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您的regressor变量未在plot1范围内定义。您需要为reactive

命名
getRegressor <- reactive({
   library(e1071)
   svm(Engagements ~ Character.count, 
               data = dataset,
               type = 'eps-regression')
 })

现在您可以调用它来定义 regressor内的变量plot1

output$plot1 <- renderPlot({
  regressor <- getRegressor()
  ...
})