用户定义适合 ggvis 图

User defined fit to ggvis plot

我有一个应用程序,我希望用户能够在用户也定义的散点图中定义 2 个变量的拟合顺序。现在,我的尝试被证明是不成功的错误 Error in x - xbar : non-numeric argument to binary operator。我对如何解决这个问题一头雾水,如有任何关于如何使它正常工作的建议,我们将不胜感激。

ui <- fluidPage(
  selectInput(inputId = "x", label="Select x-axis Variable:", choices=as.character(names(mtcars)),selected='mpg', multiple = FALSE),
  selectInput(inputId = "y", label="Select y-axis Variable:", choices=as.character(names(mtcars)),selected='hp', multiple = FALSE),
  numericInput(inputId = "order", label = "Select Order of fit line", value = 2, min = 1, max = 6, step = 1),
  ggvisOutput("plot1") 
)

server<-function(input,output,session)
{

  vis <- reactive({

  xvar <- prop("x", as.symbol(input$x))
  yvar <- prop("y", as.symbol(input$y))

  mtcars %>%
    ggvis(x = xvar, y = yvar) %>%
    layer_points() %>%
    layer_model_predictions(model = "lm", formula=yvar ~ poly(xvar,input$order))
  })
    vis %>% bind_shiny("plot1")
}

shinyApp(ui = ui, server = server)

您可以使用 input 值通过 as.formulapaste 构造公式。

formula = as.formula(paste(input$y, "~", "poly(", input$x, ",", input$order, ")") )