用户定义适合ggvis图

时间:2017-07-12 22:05:17

标签: r shiny ggvis

我有一个应用程序,我希望用户能够在用户也定义的散点图中定义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)

1 个答案:

答案 0 :(得分:1)

您可以使用as.formula值通过pasteinput构建公式。

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