Search code examples
rgooglevis

googlevis $ operator is invalid for atomic vectors


I've hit the dreaded "$ operator is invalid for atomic vectors" error. It happens when I add the gvisLineChart. Any suggestions?

library(shiny)
library(googleVis)

#this is a dput of a sql query to make the example reproducible. 
#In reality this will be an RODBC sqlQuery result
dataset <- structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
  value = c(1.68294196961579, 82.4641565111739, 83.3056274959818, 
  6.73176787846317, 5.89029689365528, 2.52441295442369, 4.20735492403948, 
  0.841470984807897, 5.04882590884738, 15.1464777265421)), 
  .Names = c("id", "value"), row.names = c(NA, 10L), class = "data.frame")

ui <- shinyUI(
     plotOutput("motionPlot")
)

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

    output[["motionPlot"]] <- renderGvis({

    Line <- gvisLineChart(dataset, xvar=c("id"), yvar=c("value"))     
    plot(Line)
   })

})

shinyApp(ui = ui, server = server)

Solution

  • googleVis plots aren't quite like regular plots in R. Regular plots produce static images but googleVis produces basically mini-web pages with HTML and javascript data as well. Therefore you should't use plotOutput, you should use htmlOutput to render them to the page. Also, you don't need to use plot() either. This will work with your example data

    ui <- shinyUI(
         htmlOutput("motionPlot")
    )
    
    server <- shinyServer(function(input, output) {
        output[["motionPlot"]] <- renderGvis({
            gvisLineChart(dataset, xvar=c("id"), yvar=c("value"))     
       })
    })
    
    shinyApp(ui = ui, server = server)
    

    I found more examples here by googling "shiny gvisLineChart". Tested with googleVis_0.6.1, shiny_0.13.2, R 3.2.5