0

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)
dataphile
  • 344
  • 1
  • 5
  • 13
  • It's very difficult to help without a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Sounds like `dataset` isn't a data.frame but we can't run your code to test it. – MrFlick Sep 16 '16 at 15:53
  • I've used the msdb database in this example so that anyone with a mssql db can run it. Any query against any db will reproduce the result. Unfortunately it is a db related question so 'reproducible' would include a reference to a database. Thanks for your interest, any help is appreciated. – dataphile Sep 18 '16 at 05:47
  • 1
    I've dput the sqlQuery result to make it reproducible. – dataphile Sep 19 '16 at 08:17

1 Answers1

1

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295