[Rd] How do you construct a function from a list? (PR#743)

dmurdoch@pair.com dmurdoch@pair.com
Wed, 22 Nov 2000 13:56:56 +0100 (MET)


On Wed, 22 Nov 2000 13:08:26 +0100, Kurt Hornik wrote:
.
>>>>> Duncan Murdoch writes:
>> I'm trying to get a data.restore function to work on functions.  One
>> thing I can't figure out:  how do you construct a function from it's
>> component parts?
 ...
>> forloop <-
>> as.call(list(as.name('for'),as.name('i'),1,as.call(list(as.name('junk')))))
.
>
>I think the easiest approach is to do
>
>	foo <- function() {}
>	body(foo) <- forloop

That one gives me the same as "as.function(list(forloop))", i.e. 

 function () 
 list(for (i in 1) junk())

I'm running this in 1.1.1 on Windows, but I tried a Nov 14 development
snapshot, and got the same results.

I think this is a bug in as.function.default, because of these two
examples:

For a function with arguments, as.function undoes as.list:

 > options(keep.source=F)
 > withargs <- function(x) for (i in 1) junk()
 > withargs
 function (x) 
 for (i in 1) junk()
 > as.function(as.list(withargs))
 function (x) 
 for (i in 1) junk()

For a function with no arguments, it doesn't:
 
 > noargs <- function() for (i in 1) junk()
 > noargs
 function () 
 for (i in 1) junk()
 > as.function(as.list(noargs))
 function () 
 list(for (i in 1) junk())

I looked at the source (do_asfunction in main/coerce.c), but couldn't
understand what was going on well enough to figure out the bug.

Duncan Murdoch


--please do not edit the information below--

Version:
 platform = Windows
 arch = x86
 os = Win32
 system = x86, Win32
 status = 
 major = 1
 minor = 1.1
 year = 2000
 month = August
 day = 15
 language = R

Windows 9x 4.90 (build 3000)  

Search Path:
 .GlobalEnv, Autoloads, package:base


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._