【问题标题】:googlevis $ operator is invalid for atomic vectorsgooglevis $ 运算符对原子向量无效
【发布时间】:2016-09-19 21:51:57
【问题描述】:

我遇到了可怕的“$ 运算符对原子向量无效”错误。当我添加 gvisLineChart 时会发生这种情况。有什么建议吗?

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)

【问题讨论】:

  • 没有reproducible example 很难提供帮助。听起来 dataset 不是 data.frame,但我们无法运行您的代码来测试它。
  • 我在这个例子中使用了 msdb 数据库,因此任何拥有 mssql db 的人都可以运行它。针对任何数据库的任何查询都将重现结果。不幸的是,这是一个与数据库相关的问题,因此“可重现”将包括对数据库的引用。感谢您的关注,感谢您的帮助。
  • 我已经输入了 sqlQuery 结果以使其可重现。

标签: r googlevis


【解决方案1】:

googleVis 绘图与 R 中的常规绘图不太一样。常规绘图生成静态图像,但 googleVis 生成基本上带有 HTML 和 javascript 数据的迷你网页。因此,您不应使用plotOutput,而应使用htmlOutput 将它们呈现到页面上。此外,您也不需要使用plot()。这将适用于您的示例数据

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)

我通过谷歌搜索“shiny gvisLineChart”找到了更多示例here。使用googleVis_0.6.1shiny_0.13.2R 3.2.5 测试

【讨论】:

  • 太棒了!谢谢弗利克先生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2013-07-14
  • 2019-04-29
  • 2015-10-02
  • 1970-01-01
相关资源
最近更新 更多