User defined fit to ggvis plot

2019-08-26 22:36发布

I have an app that I want the user to be able to define the order of fit of 2 variables in a scatterplot that a user also defines. Right now, my attempt at this is proving unsuccessful with the error Error in x - xbar : non-numeric argument to binary operator. I am at a loss as to how to fix this, and any suggestions on how to get this working are appreciated.

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)

标签: r shiny ggvis
1条回答
Lonely孤独者°
2楼-- · 2019-08-26 23:03

You can construct the formula via as.formula and paste, using the input values.

formula = as.formula(paste(input$y, "~", "poly(", input$x, ",", input$order, ")") ) 
查看更多
登录 后发表回答