R ggvis and shiny: layer_model_predictions (model = "lm") Error ... attempt to replicate an object of type "close"

I tried using ggvis with brilliant, but got some error with my test sample. The codes are at the end of this message.

I got the following error message only when I tried to run ggvis with brilliance:

Guessing formula = mpg ~ wt
Error in rep(col, length.out = nrow(data)) : 
   attempt to replicate an object of type 'closure'

If I comment out the line on the .R server

layer_model_predictions(model = "lm") %>%

I will not get an error message, and the brilliant application works fine, except that I do not get the fitting line lines. Some ggvis codes also work great when not used with shiny ones. I guess this has something to do with reactive data set to brilliant, but I don't know how to quickly find a solution.

Any suggestion?

Thank you very much for your help!

Ying

ui.R

library(ggvis)
shinyUI(pageWithSidebar(

  div(),

  sidebarPanel(
    sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
                value = 10, step = 1)
  ),

  mainPanel(
    ggvisOutput("plot"),
    tableOutput("mtc_table")
  )

))

server.R

library(shiny)
library(ggvis)
library(dplyr)

shinyServer(function(input, output, session) {

  mtc <- reactive({

            mtc <- mtcars[1:input$n, ]
            mutate(mtc, carname=rownames(mtc), id = 1:nrow(mtc))
  })


  all_values <- function(x) {

      if(is.null(x)) return(NULL)
      row <- mtc()[mtc()$id == x$id, ]
      row$carname

  }

  mtc %>%
         ggvis(x= ~wt, y= ~mpg, key := ~id, fill = ~factor(cyl)) %>%
         layer_points() %>%
         group_by(cyl) %>%
         add_tooltip(all_values,"hover") %>%
         layer_model_predictions(model = "lm") %>%
         bind_shiny("plot")

  output$mtc_table <- renderTable({
    mtc()[, c("carname", "wt", "mpg", "cyl")]
  })
})
+4
source share

All Articles