English 中文(简体)
我能否删除......(dot-dot-dot)的一个要素,并把它通过?
原标题:Can I remove an element in ... (dot-dot-dot) and pass it on?
  • 时间:2011-08-11 15:14:30
  •  标签:
  • r
  • ellipsis

是否可能从......中删除一个要素,并将......纳入其他职能? 我的前两次尝试失败了:

parent = function(...)
{

   a = list(...)
   str(a)
   a$toRemove = NULL  
   str(a)

   # attempt 1   
   child(a)   

   # attempt 2
   child( ... = a )
}


child = function(...)
{
  a = list( ... )
  str(a)
}

parent( a = 1 , toRemove = 2 )

Edit
Sorry about the confusion. I fixed child(). The intent was to have child list the contents of ...

Edit2
Here s more of a real-world example (but still fairly simple so we can have a useful conversation about it). Parent is called via recursion. Parent need to know the depth of the recursive call. Callers outside of parent should t know about "depth" nor should they set it when calling parent(). Parent calls other functions, in this case child(). Child needs values in ... Clearly child doesn t need "depth" because parent generated it for its own use.

parent = function( ... )
{

   depth = list(...)$depth      
   if ( is.null( depth ) )
   {
       depth = 1
   }  
   print( depth )

   # parent needs value of depth to perform various calculations (not shown here)

   if ( depth == 5 )
   {
       return()
   }
   else
   {
      # child doesn t need "depth" in ...
      child( ... ) 
   }

   # yikes!  now we ve added a second, third, etc. depth value to ...
   parent( depth = depth + 1 , ... )

}


child = function(...) 
{       
    # does some magic    
}
最佳回答

对这些东西进行操纵的一种方式是,在<条码>内填上<>儿童功能,并使用一个定义,将你不想通过的任何论点带上<条码>儿童<>/条码><><>><>><>>><>>>>><><>>>><>>>><>>>>>><>>>>>>>>>>。 www.un.org/Depts/DGACM/index_french.htm 例如:

parent <- function(...) {
    localChild <- function(..., toRemove) child(...)
    localChild(...)
}
child <- function(a) {
    a + 10
}

> parent(a = 1, toRemove = 10)
[1] 11

另一种方式是使用<条码>。

parent2 <- function(...) {
    a <- list(...)
    a$toRemove <- NULL
    do.call(child2, a)
}
child2 <- function(b) {
    b + 10
}
> parent2(b = 1, toRemove = 10)
[1] 11

视您的实际使用情况,do.quest(>)或许最接近您打算提出的问题。

问题回答

<>育婴功能是错误的。 Try

> child(a=1)
Error in str(a) : object  a  not found

edit : no longer applicable.

......论点只能用来将参数传递给下一个功能。 不能轻易从那里获得这些参数,除非你将其转换成一个清单。 因此,你的孩子可以:

child <- function(...)
{
  mc <- match.call()  # or mc <- list(...)
  str(mc$a)
}

什么是有意义的。 您不知道用户是否指定了<代码>a。 正确的办法是将<条码>a作为你职务的论据。 http://www.un.org/Depts/DGACM/index_french.htm

child <- function(a, ...){
    str(a,...)
}

Then you could do :

parent <- function(...){

   mc <- match.call()
   mc$toRemove <- NULL
   mc[[1L]] <- as.name("child")
   eval(mc)

}

或使用<代码>list(......)和do. calls(, 提议建造@Gavin。 <代码>match. calls(>>)的优点是,你也可以提出非争论。 允许父母为子女规定违约:

parent <- function(a=3, ...){
    ... (see above)
}

在此,我举了一个例子,说明如何使这些项目脱离......,删除一个要素,然后,我称下一个职能是这样做的。 呼吁:

parent <- function(...){
   funArgs <-  list(...)
   str(funArgs)
   ## remove the second item
   newArgs <- funArgs[-2]
   str(newArgs)
   ## if you want to call another function, use do.call
   do.call(child, newArgs)
  }

child = function(...)
{
  cat("Don t call me a child, buddy!
")
  a <- list(...)
  str(a)
}


parent(a=1, b=2, c=3)

如果你需要在你的论据中添加更多的内容,而不是删除论点,那么就应当铭记,do. calls<>/code>就像名单上的名字,因为名字是争论的名称,名单价值是争论价值。 这在求助档案中,但我与这个轨道争斗,然后才最后将其编外。

你再次得到一些好的答复,但这里只是简单地论述你的具体例子:

parent = function(...)
{

   a = list(...)
   str(a)
   a$toRemove = NULL  
   str(a)

   # attempt 1   
   child(a)   

   # attempt 2
   #child(...)
}

child = function(...)
{
    a <- as.list(...)   
    str(a)
}

parent( a = 1 , toRemove = 2 )

返回:

List of 2
 $ a       : num 1
 $ toRemove: num 2
List of 1
 $ a: num 1
List of 1
 $ a: num 1

你的原始版本正出现错误,因为an t下定义,载于child。 然后简单使用as.list(...) in child。 (与仅仅<代码>list(......))相比,似乎产生了你想要的产出。 请注意,我只在这里使用你的努力。

I don t think the listed answers solve the problem, or at least not as I read it. Suppose you wanted to pass some parameters, like say xmax and xmin , to child(...) as actual variables?
in child s environment, it wants to see variables named xmax and xmin , and the examples presented so far do not seem to make those variables available. Try inserting a line like

xmax-xmin -> xrange

into the child() function and it ll throw an error.
The whole point of the original question, I thought, was to allow passing a subset of the optional "..." variables to child() . You can tailor the solutions for simple cases, e.g. sum(...), where sum(unlist(the_modified_list)) works. In the more general case, I still can t see a solution. We probably need to elevate this problem to the R-help mailing list.

Edit: see the detailed presentation at http://ucfagls.wordpress.com/2011/07/23/passing-non-graphical-parameters-to-graphical-functions-using/





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签