关于 Plyr 错误的问题:as.double(y) 中的错误:无法强制类型“S4”;到“double”类型的向量

发布于 2024-10-04 19:43:26 字数 616 浏览 1 评论 0 原文

我正在升级我之前工作的一个项目。这段代码几个月前就有效了,同时我升级了 R 和 plyr。我想我以前使用的是 R1.10,现在使用的是 R1.35,我不确定之前运行的 plyr 版本是什么,但我当前安装的版本是 1.2。

这是我正在尝试运行的内容:

library(plyr)
library(twitteR)

tw <- head(ldply(searchTwitter("rstats", session=getCurlHandle(), n=10), function(x) data.frame(text=text(x), favorited=favorited(x), created=created(x), truncated=truncated(x), id=id(x), statusSource=statusSource(x), screenName=screenName(x))))

我现在总是收到相同的错误消息。

Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

任何建议将不胜感激。

谢谢,

杰森

I am upgrading a project I had working previously. This code worked a few months ago, in the meantime I have upgraded R and plyr. I think I was on R1.10 and now I am on R1.35, I am not sure what version of plyr I was running before but my current installed version is 1.2.

Here is what I am trying to run:

library(plyr)
library(twitteR)

tw <- head(ldply(searchTwitter("rstats", session=getCurlHandle(), n=10), function(x) data.frame(text=text(x), favorited=favorited(x), created=created(x), truncated=truncated(x), id=id(x), statusSource=statusSource(x), screenName=screenName(x))))

I always get the same error message now.

Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'

Any advice would be appreciated.

Thanks,

Jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡離愁欲言轉身 2024-10-11 19:43:26

在您对正在运行的版本感到困惑的过程中(没有 R 版本 1.35!!),存在几个问题。 (要了解您正在运行的 R 版本和软件包,请尝试 sessionInfo()。)

首先,您收到的错误来自于您对 text() 的使用。它应该是statusText()

其次,似乎某些函数/方法没有在包 NAMESPACE 中导出。您可以通过在调用函数时指定正确的命名空间来使其工作,如下例所示,但您应该向包维护者发送电子邮件(Jeff Gentry - CRAN)。您可以使用 ::: 运算符引用未导出的函数。 ::: 在左侧采用包/命名空间名称,在右侧采用函数名称,例如:

twitteR:::statusSource(x)

这是示例的完整工作版本:

library(plyr)
library(twitteR)
## simplify the call to see what is going on - function first
fooFun <- function(x) {
    data.frame(text = statusText(x), favorited=favorited(x),
               created=created(x), truncated=twitteR:::truncated(x),
               id=id(x), statusSource=twitteR:::statusSource(x),
               screenName=screenName(x))
}
## now ldply it
out <- ldply(searchTwitter("rstats", session = getCurlHandle(), n = 10), fooFun)
## show some of it:
head(out)

In amongst your confusion about what versions you are running (there wasn't an R version 1.35!!), there are several issues. (To find out what versions of R and packages you are running, try sessionInfo().)

First, the error you are getting comes from your use of text(). It should be statusText().

Second, it seems like some of the functions/methods are not being exported in the package NAMESPACE. You can make it work by specifying the correct namespace when calling the function, as per the example below, but you should email the package maintainer (Jeff Gentry - contact details on CRAN). You can refer to unexported functions using the ::: operator. ::: takes the package/namespace name on the left-hand side, with the function name on the right hand side, e.g.:

twitteR:::statusSource(x)

Here is a full working version of your example:

library(plyr)
library(twitteR)
## simplify the call to see what is going on - function first
fooFun <- function(x) {
    data.frame(text = statusText(x), favorited=favorited(x),
               created=created(x), truncated=twitteR:::truncated(x),
               id=id(x), statusSource=twitteR:::statusSource(x),
               screenName=screenName(x))
}
## now ldply it
out <- ldply(searchTwitter("rstats", session = getCurlHandle(), n = 10), fooFun)
## show some of it:
head(out)
笑看君怀她人 2024-10-11 19:43:26

这适用于当前版本的 R (2.12.0) 和 twitteR 版本 0.91:

tw <- ldply(searchTwitter("rstats", session=getCurlHandle(), n=10), 
              function(x)  c(text=x@text, favorited=x@favorited, created=x@created, 
                             truncated=x@truncated, id=x@id, statusSource=x@statusSource, 
                             screenName=x@screenName )
                          )

我遇到了与您相同的错误,直到我切换到“@”运算符来访问槽值。

This works in the current version of R (2.12.0) and version 0.91 of twitteR:

tw <- ldply(searchTwitter("rstats", session=getCurlHandle(), n=10), 
              function(x)  c(text=x@text, favorited=x@favorited, created=x@created, 
                             truncated=x@truncated, id=x@id, statusSource=x@statusSource, 
                             screenName=x@screenName )
                          )

I was getting the same error as you were until I switched over to the "@" operator for accessing the slot values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文