小编Ash*_*Ash的帖子

all.equal.POSIXct的意外输出

我从R中的all.equal方法获得了意外的输出,特别是POSIXct的实现,all.equal.POSIXct.

t <- Sys.time()
isTRUE(all.equal(t, t+1))
Run Code Online (Sandbox Code Playgroud)

返回TRUE,然后

isTRUE(all.equal(t, t+1, scale = 1))
Run Code Online (Sandbox Code Playgroud)

返回FALSE.

但是,如果查看all.equal.POSIXct的定义,可以看到scale参数的默认值为1:

> all.equal.POSIXct
function (target, current, ..., scale = 1) 
{
    check_tzones(target, current)
    NextMethod("all.equal")
}
<bytecode: 0x22eac90>
<environment: namespace:base>
Run Code Online (Sandbox Code Playgroud)

如果显式调用all.equal.POSIXct而不是all.equal,则会得到相同的结果.

为什么在第一次调用all.equal.POSIXct时没有选择默认参数scale = 1?我做错了什么,或者我从根本上误解了什么,或者这是一个错误?

在此先感谢您的帮助.

r posixct

9
推荐指数
2
解决办法
179
查看次数

R并行S4类簇错误

我在使用R中的并行包获取一些代码时遇到问题.我正在使用R 2.15.

这是一个简化的例子......我有一个'animal.R'文件,其中包含以下内容:

# animal.R
setClass("Animal", representation(species = "character", legs = "numeric"))

##Define some Animal methods
setGeneric("count",function(x) standardGeneric("count"))
setMethod("count", "Animal", function(x) { x@legs})

setGeneric("countAfterChopping",function(x) standardGeneric("countAfterChopping"))
setMethod("countAfterChopping", "Animal", function(x) { x@legs <- x@legs-1; x@legs})
Run Code Online (Sandbox Code Playgroud)

然后,在我的R终端中,我运行:

library(parallel)
source('animal.R')
Run Code Online (Sandbox Code Playgroud)

启动两个节点的本地群集:

cl <- makeCluster(rep('localhost', 2))
Run Code Online (Sandbox Code Playgroud)

告诉群集节点有关Animal类的信息:

clusterEvalQ(cl, parse('animal.R'))
Run Code Online (Sandbox Code Playgroud)

然后在集群上运行一些代码:

# This works
parSapply(cl, list(daisy, fred), count)

# This doesn't...
parSapply(cl, list(daisy, fred), countAfterChopping)
Run Code Online (Sandbox Code Playgroud)

停止群集:

stopCluster(cl)
Run Code Online (Sandbox Code Playgroud)

对parSapply的第一次调用按预期工作,但第二次调用会产生此错误:

Error in checkForRemoteErrors(val) : 
  2 nodes produced errors; first error: "Animal" is not a defined class
Run Code Online (Sandbox Code Playgroud)

有什么想法发生了什么?为什么第二次调用parSapply不起作用?

parallel-processing r s4

5
推荐指数
1
解决办法
5646
查看次数

在R包中定义组通用函数

我在编写R包中定义一个通用组时遇到了麻烦.

这是一个相当小的例子:

setGroupGeneric('FooBarFunctions', function(x, y) NULL)

setGeneric('foo', group = 'FooBarFunctions', function(x, y) standardGeneric('foo'))
setGeneric('bar', group = 'FooBarFunctions', function(x, y) standardGeneric('bar'))

setMethod('foo', signature(x = 'ANY', y = 'ANY'),
function(x, y)
  cat(sprintf('foo,ANY (%s),ANY (%s)\n', x, y)))

setMethod('bar', signature(x = 'ANY', y = 'ANY'),
function(x, y)
  cat(sprintf('bar,ANY (%s),ANY (%s)\n', x, y)))

setMethod('FooBarFunctions', signature(x = 'character', y = 'ANY'),
function(x, y)
  cat(sprintf('FooBarFunctions,character (%s),ANY (%s)\n', x, y)))
Run Code Online (Sandbox Code Playgroud)

如果我将此代码粘贴到R终端,那么一切都按预期工作:

> foo(1, 2)
foo,ANY (1),ANY (2)
> bar(1, 2)
bar,ANY (1),ANY (2)
> foo('a', 2)
FooBarFunctions,character (a),ANY …
Run Code Online (Sandbox Code Playgroud)

r s4

4
推荐指数
1
解决办法
652
查看次数

标签 统计

r ×3

s4 ×2

parallel-processing ×1

posixct ×1