similar to: Most elegant way to use formals() in building functions

Displaying 20 results from an estimated 30000 matches similar to: "Most elegant way to use formals() in building functions"

1999 Mar 29
3
problems with formals and get (PR#151)
With 0.63.3, try test <- function(){ fn <- function(a) print("hello") print(is.function(fn)) print(names(formals("fn")))} test() if fn is not quoted in the last line of test, it works properly. test <- function(){ fn <- function() print("hello") formals(fn) <- alist(a=,b=) return(fn)} formals(test()) now try with line 3 of test changed to
2024 Feb 18
1
Capturing Function Arguments
? Sat, 17 Feb 2024 11:15:43 -0700 "Reed A. Cartwright" <racartwright at gmail.com> ?????: > I'm wrapping a function in R and I want to record all the arguments > passed to it, including default values and missing values. This is hard if not impossible to implement for the general case because the default arguments are evaluated in the environment of the function as it
2007 Jun 26
1
slight anomaly in formals<- ? (PR#9758)
Hi, The R input/output after the following paragraph is from a session with version.string R version 2.4.0 Patched (2006-11-23 r39958). The last element, x$c, of x has no value and after the assignment of x to the formals of f, x$c does not become a formal argument of f. The second assignment does the job. It seems that x$c actually becomes the body of f after the first assignment. There is no
2016 Mar 07
2
body(NULL) <- something; ditto formals() -- should not work
I'm proposing to signal an error (from R >= 3.3.0) in such examples -- which do "work" in R 3.2.x and earlier : > f <- NULL; body(f) <- quote(sin(a+1)); f function () sin(a + 1) <environment: 0x48f9798> > g <- NULL; formals(g) <- alist(x = pi, y=); g function (x = pi, y) NULL <environment: 0x4e6dfe8> > The proposal is that the underlying C
2009 Oct 01
1
inverse currying
Dear list, I have the following function, sugar = function(fun, id = "id"){ ff <- formals(fun) if( id %in% names(ff)) stop("id is part of args(fun)") formals(fun) <- c(unlist(ff), alist(id=)) fun } which one may use on a function foo, foo = function(x){ x } sugar(foo) # results in the extended closure, function (x, id) { x } Its limitation (other
2006 Sep 26
1
Bug in formals<-
I think this is new since a previous version of R: > h <- function(x, trantab) trantab[x] > w <- 6:4 > names(w) <- c('cat','dog','giraffe') > w cat dog giraffe 6 5 4 > > formals(h) <- list(x=numeric(0), trantab=w) > h function (x = numeric(0), trantab = c(6, 5, 4)) trantab[x] You can see that the names
2010 Oct 08
2
What do you call the value that represents a missing argument?
Hi all, What's the official name for the value that represents a missing argument? e.g. formals(plot)$x str(formals(plot)$x) deparse(formals(plot)$x) is.symbol(formals(plot)$x) What's the correct way to create an object like this? (for example if you are manipulating the formals of a function to add an argument with no default value, as in http://stackoverflow.com/questions/3892580/).
2013 Feb 04
2
Modifying a function programmatically
Dear list # I have a function ff <- function(a,b=2,c=4){a+b+c} # which I programmatically want to modify to a more specialized function in which a is replaced by 1 ff1 <- function(b=2,c=4){1+b+c} # I do as follows: vals <- list(a=1) (expr1 <- as.expression(body(ff))) expression({ a + b + c }) (expr2 <- do.call("substitute", list(expr1[[1]], vals))) { 1 +
2005 Oct 31
0
(PR#7495) Function Body / Formals Bug
_________________________________________________________________________= ______________ Note: This e-mail is subject to the disclaimer contained at the bottom of= =20this message. _________________________________________________________________________= ______________ Just a note on the reported bug; it seems to be related to having a NULL function body. # this works > f <-
2011 Dec 05
2
class extension and documentation
I've added a "backsolve" method to the bdsmatrix library. Per the Extending manual section 7.1 I've also added the following 3 lines along with my setMethod definitions for 2 classes. backsolve <- function(r, ...) UseMethod("backsolve") backsolve.default <- base:::backsolve formals(backsolve.default) <- c(formals(backsolve.default), alist(... = )) I've
2005 Jan 12
0
Function Body / Formals Bug (PR#7495)
I'd like to report two bugs in the R function definitions, the first slightly problematic, the second likely unimportant. Both of these are reproducible on both Windows and Linux, R 2.0.1 First Bug: > a <- new("function") > formals(a) <- alist(a=, b=, c=, d=) > a function (a, b, c) ## the d argument disappeared! > formals(a) <- alist(a=, b=, c=, d=) > a
2016 Oct 19
2
How to assign NULL value to pairlist element while keeping it a pairlist?
On Sat, Oct 15, 2016 at 2:00 AM, Martin Maechler <maechler at stat.math.ethz.ch> wrote: >>>>>> Michael Lawrence <lawrence.michael at gene.com> >>>>>> on Wed, 12 Oct 2016 15:21:13 -0700 writes: > > > Thanks, this was what I expected. There is a desire to > > eliminate the usage of pairlist from user code, which > >
2007 Aug 22
2
Evaluating f(x(2,3)) on a function f<- function(a,b){a+b}
Dear list I have a function and a vector, say f <- function(a,b){a+b} x <- c(2,3) I want to "evaluate f on x" in the sense of computing f(x[1],x[2]). I would like it to be so that I can write f(x). (I know I can write a wrapper function g <- function(x){f(x[1],x[2])}, but this is not really what I am looking for). Is there a general way doing this (programmatically)?
2010 May 23
2
possible bug in formals
Hi, I am a little bit surprised by the following output of 'formals'. Is this the intended behavior? > f <- function(a=1,b=-1) { a+b } > class(formals(f)$a) [1] "numeric" > class(formals(f)$b) [1] "call" Josef -- ----------------------------------------------------------------------------- Josef Leydold | WU (Vienna University of Economics and
2016 Mar 29
0
body(NULL) <- something; ditto formals() -- should not work
Hi, On 03/07/2016 09:26 AM, Martin Maechler wrote: > I'm proposing to signal an error (from R >= 3.3.0) in such > examples -- which do "work" in R 3.2.x and earlier : > >> f <- NULL; body(f) <- quote(sin(a+1)); f > function () > sin(a + 1) > <environment: 0x48f9798> > This works because formals() (the getter) works on things that are not a
2009 Apr 17
1
cast function in package reshape
Hello R useRs, I have a function which returns a list of functions : freq1 <- function(x) { lev <- unique(x[!is.na(x)]) nlev <- length(lev) args <- alist(x=) if (nlev == 1) { body <- c("{", "sum(!is.na(x))", "}") f <- function() {} formals(f) <- as.pairlist(args) body(f) <- parse(text = body) namef <-
2004 Sep 14
3
Getting the argument list within a function
Is there a way of getting the argument list of a function from within that function? For example, something like f <- function(x, y = 3) { fargs <- getFunctionArgList() print(fargs) ## Should be `alist(x, y = 3)' } Thanks, -roger
2012 Oct 04
2
How to build a list with missing values? What is missing, anyway?
This is tangentially related to Hadley's question. Suppose I'm building a function programmatically; I have assembled an expression for the body and I know the names of the arguments it wants to take. Suppose I have some convenience function such that writing make_function(alist(a=, b=), quote(a+b), environment()) is equivalent to writing function(a,b) a+b So how do I make the
2018 Oct 06
1
Warning when calling formals() for `[`.
Hi, Thanks for the note. How would explain the following snippet taken from `formals` doc page (the code comment is also from that doc) ? ## formals returns NULL for primitive functions. Use it in combination with ## args for this case. is.primitive(`+`) formals(`+`) formals(args(`+`)) Le sam. 6 oct. 2018 ? 13:42, Rui Barradas <ruipbarradas at sapo.pt> a ?crit :
2003 Oct 10
1
incorrect behaviour of formals (PR#4511)
Full_Name: Jörg Polzehl Version: 1.8.0 OS: Windows XP Submission from: (NULL) (62.141.176.1) I encountered a problem when playing with the mle library and specifying negative starting values for the parameters. The reason seems to be an incorrect behaviour of function formals: glike<-function(a=1,b=1,c=1) a > formals(glike) $a [1] 1 $b [1] 1 $c [1] 1 > unlist(formals(glike)) a b c