I tried to use ggvis with shiny, 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 shiny:
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 in server.R
layer_model_predictions(model = "lm") %>%
I will not get the error message and shiny app runs fine except that I do not get the linear fitting lines. The ggvis part of the codes also run fine when not used with shiny. I guess it has something to do with the reactive data set by shiny, but I have no clue to find a quick fix.
Any suggestion?
Thanks a lot for the 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")]
})
})