【问题标题】:Use MySQL database query results to plot R graph using RApache and Brew使用 MySQL 数据库查询结果使用 RApache 和 Brew 绘制 R 图
【发布时间】:2014-06-12 10:01:21
【问题描述】:

我正在尝试使用 R 绘制由 MySQL 查询结果填充的图形。我有以下代码:

rs = dbSendQuery(con, "SELECT BuildingCode, AccessTime from access")
data = fetch(rs, n=-1)
x = data[,1]
y = data[,2]
cat(colnames(data),x,y)

这给了我一个输出:

BuildingCode AccessTime TEST-0 TEST-1 TEST-2 TEST-3 TEST-4 14:40:59 07:05:00 20:10:59 08:40:00 07:30:59

但这就是我卡住的地方。我知道如何将“猫”数据传递到 R 图中。我花了几个小时在网上搜索,我遇到的大多数 R 图示例都使用read.tables(text="")。这对我来说是不可行的,因为数据必须来自数据库而不是硬编码。我还发现了一些关于将输出保存为 CSV 但 MySQL 无法覆盖现有文件,因此一旦我无法执行代码后再次执行此操作,因为文件已存在。

我的问题是,我如何使用“猫”数据(或者如果有更好的方法,可以使用其他方法)使用非硬编码的数据绘制图表?

注意:我使用 RApache 作为我的 Web 服务器,并且我已经安装了 Brew 包。

【问题讨论】:

  • 为什么不能直接使用plot(x=data[,1],y=data[,2],xlab=colnames(data)[1],ylab=colnames(data)[2])
  • 我现在就试试。我是 R 新手,所以我仍在努力了解它是如何工作的。
  • 我刚刚尝试在 RStudio 中运行它并收到以下消息:Error in plot.window(...) : need finite 'xlim' values In addition: Warning messages: 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 3: In min(x) : no non-missing arguments to min; returning Inf 4: In max(x) : no non-missing arguments to max; returning -Inf 5: In min(x) : no non-missing arguments to min; returning Inf 6: In max(x) : no non-missing arguments to max; returning -Inf
  • 您的数据似乎有一些缺失值或空值...您想对空值做什么?
  • 我使用的数据只是测试数据,据我所知(当我创建它时)没有空值?但是让我们说有,我将如何解决这个问题?

标签: mysql r rapache r-brew


【解决方案1】:

使用 R 进行绘图,然后将文件的路径传回 cat

<%
## Your other code to get the data, assuming it gets a data.frame called data

## Plot code
library(Cairo)
myplotfilename <- "/path/to/dir/myplot.png"
CairoPNG(filename = myplotfilename, width = 480, height = 480)
plot(x=data[,1],y=data[,2])
tmp <- dev.off()
cat(myplotfilename)
%>

【讨论】:

  • 我已尝试添加此内容,但出现错误“没有名为 'Cairo' 的包”...我已使用“install.packages('Cairo')”从 R 安装它,我正如你所说,我正在调用“图书馆(开罗)”
  • 正如我在上面的评论中提到的,我已经安装了 Cairo,但仍然出现错误。
  • 以root身份安装并重启Apache
猜你喜欢
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
相关资源
最近更新 更多