开发者

Create "missing objects" (aka: "empty symbols" , "empty objects") / needed for formals manipulation/

How to create an "empty object" in R? [edit: I don't know how to rightly call this "thing" so I am calling it "empty object", others: "empty symbol", "zero length symbol", "missing object" might also be used]

[edit2: finally I tend to settle down on the "missing symbol object" for the name of the "thing". It also appears that J.Chambers is using this terminology in his 2008 book, see comments for @mbq's answer. According to Chambers, the "missing symbol" has a zero-length string as it's contents. Thus, as.symbol("") should create such an object, which it doesn't in current version of R(2.11.1) ]

The simplest way I could find is

x <- alist(a=)$a

[Clarification]

Note that "empty object" is not a NULL object or a vector of length 0. "Empty object" x in my above example could be used in the function's formals manipulation, which is what I need it for.

Here is an example:

> al <- alist(a = 323, b = , c = 434)  
> al  
$a  
[1] 323  
$b  
$c  
[1] 434  

>   
> al[["c"]] <- numeric()  
> al  
$a  
[1] 323  
$b  
$c                                开发者_运维百科   #not empty  
numeric(0)  

>   
> al[["c"]] <- list()              
> al  
$a  
[1] 323  
$b  
$c                                   #not empty  
list()  

>   
>   
> al[["c"]] <- NULL                    #object removed  
> al  
$a  
[1] 323  
$b  


>   
> al[["c"]] <- alist(a = )$a  
> al  
$a  
[1] 323  
$b  
$c                                      #empty  

So, I am just looking for a way to create empty objects for use in function's formals manipulations. I am pretty sure there must be a way in base R.

Here is an example:

> foo <- function(a = 3232, b = 234){b+a}  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

> formals(foo)$c <- alist(a = )$a  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

$c  


> foo <- function(a = 3232, b = 234){b+a}  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

> formals(foo)$c <- alist(a = )$a  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

$c  

Thanks.


Try substitute():

foo <- function(a = 3232, b = 234) {}
formals(foo)$c <- substitute()
foo
# function (a = 3232, b = 234, c)


What have you done with alist(a=)$a is not an empty object, it is empty symbol/name ('' compiled as symbol name). I'm pretty sure it can't be replicated in any other way (as.symbol('') raises error).


As others have said, you're not creating an empty object. x holds a value, which is an unevaluated expression.

?alist says:

‘alist’ handles its arguments as if they described function arguments. So the values are not evaluated, and tagged arguments with no value are allowed whereas ‘list’ simply ignores them. ‘alist’ is most often used in conjunction with ‘formals’.

This is the easiest way I can determine to add an argument with no value to a function:

> foo <- function(a = 3232, b = 234){b+a}
> formals(foo) <- c(formals(foo),alist(c=))
> formals(foo)
$a
[1] 3232

$b
[1] 234

$c


Depending on the type :

x <- list()
# empty vectors
y <- numeric(0)
z <- logical(0)
...

Now you have to think whether you really want to create an empty object.

edit : You can indeed use the NULL option as well. The main difference is that the NULL object doesn't have a type. See also ?"NULL"


There is no 'empty object'. There's NULL, NA (in various flavours), zero-length lists and so on. Where does your concept of 'empty object' come from, and what are the properties of an 'empty object'?

What you have created with alist is a monster:

> x<-alist(a=)$a
> x
Error: argument "x" is missing, with no default

[its some kind of unevaluated function argument expression thing. Not an 'empty object' in any sense of the word I know]


Also d <- c()


Also d <- ""

(padding padding padding)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜