ROBDC SqlSave:如何将数据导出到 Excel 工作表中?
我正在尝试将数据从 R 导出到一个 Excel 文件(到不同的工作表):
library(plyr)
library(RODBC)
g <- lapply(iris, function(x) as.data.frame(table(x)))
save2excel <- function(x) sqlSave(xlsFile,
x, tablename = x[1], rownames = FALSE)
xlsFile <- odbcConnectExcel("C:/Temp/iris.xls", readOnly = FALSE)
l_ply(g, save2excel)
odbcCloseAll()
这会生成错误:
Error in sqlColumns(channel, tablename) :
‘1:35’: table not found on channel
问题在于 tablename = x[1]
,如何将列表名称转换为工作表名称?
I'm trying to export data from R into one Excel file (into different sheets):
library(plyr)
library(RODBC)
g <- lapply(iris, function(x) as.data.frame(table(x)))
save2excel <- function(x) sqlSave(xlsFile,
x, tablename = x[1], rownames = FALSE)
xlsFile <- odbcConnectExcel("C:/Temp/iris.xls", readOnly = FALSE)
l_ply(g, save2excel)
odbcCloseAll()
This generates on error:
Error in sqlColumns(channel, tablename) :
‘1:35’: table not found on channel
The problem lies in tablename = x[1]
, how to get list names into sheet names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须以某种方式向您的函数提供
names(g)
。最简单的解决方案类似于mapply
。此外,无论出于何种原因,Excel ODBC 驱动程序似乎不喜欢工作表名称中的点,即使 Excel 本身可以处理它们。因此,您必须将名称更改为“Sepal.Length”至“Sepal_Length”等。
完整:
You'll have to supply
names(g)
to your function somehow. The simplest solution looks likemapply
.Also, for whatever reason the Excel ODBC driver doesn't seem to like dots in sheet names, even though Excel itself can handle them. So you'll have to change your names like "Sepal.Length" to "Sepal_Length" or the like.
In full: