首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >经过处理..。通过do.call()

经过处理..。通过do.call()
EN

Stack Overflow用户
提问于 2013-08-01 04:51:35
回答 1查看 105关注 0票数 0

我正在尝试为一个S3对象编写一个方法,该方法扩展xy.coords() (),该对象本质上是一个xy.coords()列表列表。我希望能够灵活地使用...,但我需要处理其中的参数以保持美观(即xaxtyaxt参数)。下面是我编写的一个测试函数:

代码语言:javascript
复制
#test of ... expansion and modification
test = function(x, ...) {
  # x is a list of xy.coords() lists
  
  vargs = list(...)
  str(vargs)
  #vargs = as.list(substitute(list(...)))[-1L]
  # plotting defaults for aesthetics if they are not explicitly set
  if (!'xaxt' %in% names(vargs)) vargs[['xaxt']] = 'n'
  if (!'yaxt' %in% names(vargs)) vargs[['yaxt']] = 'n'
  if (!'pch' %in% names(vargs)) vargs[['pch']] = 16
  if (!'cex' %in% names(vargs)) vargs[['cex']] = 0.5
  print(vargs)
  
  pargs = c(list(x=x[[1]]$x, y=x[[1]]$y), vargs)
  str(pargs)
  
  do.call(plot, pargs) # this produces a crazy plot with a bunch of text
  
  plot(x=x[[1]], ...) # this produces the desired results
  
  invisible(NULL)
}

我发出的电话是:

代码语言:javascript
复制
test(Y, xaxt='n', yaxt='n', pch=16, cex=0.5)

其中(为了简洁而截断):

代码语言:javascript
复制
> Y[[1]]
$x
  [1]  0.001111111  0.501388889  1.001388889  1.501388889  2.001388889  2.501388889  3.001388889  3.501388889  4.001388889

$y
  [1] 0.132 0.123 0.126 0.143 0.145 0.123 0.128 0.131 0.140

正如我在test()中的注释中所指出的,将...展开为一个参数列表,并通过do.call将该列表传递给plot创建了一个图解,文本散落在各处,如下所示:

否则,直接将...传递给plot做我想做的事情:

我做错什么了吗,

  • 我对...的处理
  • 我是如何把东西传给do.call()

我是完全遗漏了一个步骤/参数,还是这是R中的一个bug?下面的会议信息,并感谢您的帮助。

代码语言:javascript
复制
> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] groan_1.0      roxygen2_2.2.2 digest_0.6.3  

loaded via a namespace (and not attached):
[1] brew_1.0-6    stringr_0.6.2 tools_3.0.0 

编辑

下面是我的问题的一个更兼容copy+paste的版本

代码语言:javascript
复制
#test of ... expansion and modification
test = function(x, method=c('do.call', 'dots'), ...) {
  # x is a list of xy.coords() lists
  
  vargs = list(...)
  str(vargs)
  #vargs = as.list(substitute(list(...)))[-1L]
  # plotting defaults for aesthetics if they are not explicitly set
  if (!'xaxt' %in% names(vargs)) vargs[['xaxt']] = 'n'
  if (!'yaxt' %in% names(vargs)) vargs[['yaxt']] = 'n'
  if (!'pch' %in% names(vargs)) vargs[['pch']] = 16
  if (!'cex' %in% names(vargs)) vargs[['cex']] = 0.5
  print(vargs)
  
  pargs = c(list(x=x[[1]]), vargs)
  str(pargs)
  
  method = match.arg(method)
  switch(method, 
         do.call={
           do.call(plot, pargs) # this produces a crazy plot with a bunch of text
         }, dots={
           plot(x=x[[1]], ...) # this produces the desired results
         })
  
  invisible(NULL)
}

Y = list(xy.coords(runif(100), runif(100)))

# this produces a crazy plot with a bunch of text
test(Y, method='do.call', xaxt='n', yaxt='n', pch=16, cex=0.5)

# this produces the desired results
test(Y, method='dots', xaxt='n', yaxt='n', pch=16, cex=0.5)

分辨率

“影子”解决了我的问题。另一种选择是将ann=FALSE指定为传递给do.call()的列表中的一个参数。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-01 06:30:27

问题是轴心标签。你实际上是在做类似的事情

小区(x=c(0.001111111、0.501388889、1.001388889、1.501388889、2.001388889、2.501388889、3.001388889、3.501388889、4.001388889、0.501388889、1.001388889、1.501388889、2.001388889、2.501388889、2.501388889、3.001388889、3.501388889、4.001388889、4.001388889、0.501388889、1.001388889、1.501388889、2.001388889、2.501388889、3.001388889、3.501388889、4.001388889))0.501388889、1.001388889、1.501388889、2.001388889、2.501388889、3.001388889、3.501388889、4.001388889、0.501388889、1.001388889、1.501388889、2.001388889、2.501388889、3.001388889、3.001388889、3.501388889、4.001388889、0.501388889、1.001388889、1.501388889、2.001388889、2.501388889、3.001388889、3.501388889、3.501388889、4.001388889、0.501388889、1.001388889、1.001388889( 1.501388889,2.001388889,2.501388889,3.001388889,3.501388889,4.001388889)

要解决这个问题,只需自己指定标签:

代码语言:javascript
复制
if (!'xlab' %in% names(vargs)) vargs[['xlab']] = names(x[[1]][2])
if (!'ylab' %in% names(vargs)) vargs[['ylab']] = names(x[[1]][1])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17985474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档