Dear R community, I have a problem when I use functions as default values for argumnents in other functions. When I use curly brackets { here, I can not create a package with inlinedocs. It will give me the error when using package.skeleton() in my package structure: Error in parse(text = utxt) : <text>:4:0: unexpected end of input For example: dummyfunction = function(filters = function(x) {b = 0; x > b} )) { # rest of code here return(filters) } This seems to me as a legal function declaration but creates the above mentioned error. Is this an error of inlinedocs or do I misunderstand the R language? Or is there another way of using functions in such a way as arguments? In this case I could easily define this filters argument inside the function for cases when it is not supplied as an argument but I have some more complex functions where I really need to define something sequential as an argument like: dummyfunction = function(filters = {a = 1; b > a; b}) {print('test')} I hope I could clarify my problem. Thanks a lot Jannis
Duncan Murdoch
2013-Jan-24 18:43 UTC
[R] functions as arguments to ther functions with inlinedocs
On 24/01/2013 1:32 PM, Jannis wrote:> Dear R community, > > I have a problem when I use functions as default values for argumnents > in other functions. When I use curly brackets { here, I can not create a > package with inlinedocs. It will give me the error when using > package.skeleton() in my package structure: > > Error in parse(text = utxt) : <text>:4:0: unexpected end of input > > > For example: > > dummyfunction = function(filters = function(x) {b = 0; x > b} )) > { > # rest of code here > return(filters) > } > > > This seems to me as a legal function declaration but creates the above > mentioned error. Is this an error of inlinedocs or do I misunderstand > the R language?The definition above is not syntactically correct, it has an extra closing paren on the first line. This would be correct: dummyfunction = function(filters = function(x) {b = 0; x > b} ) { # rest of code here return(filters) } If that still confuses inlinedocs, then it's a bug in that package. This might be easier for it to handle: dummyfunction = function(filters) { if (missing(filters)) filters <- function(x) {b = 0; x > b} # rest of code here return(filters) } and it should be equivalent to the original.> Or is there another way of using functions in such a way > as arguments? In this case I could easily define this filters argument > inside the function for cases when it is not supplied as an argument but > I have some more complex functions where I really need to define > something sequential as an argument like: > > dummyfunction = function(filters = {a = 1; b > a; b}) {print('test')}That's a pretty strange definition. I would have written it as dummyfunction = function(filters = b) { { a = 1 b > a b print('test') } Remember that defaults for function arguments are evaluated in the evaluation frame of the function, not in the caller's evaluation frame, so they don't need to exist when you enter the function, only when you first use the associated argument. Duncan Murdoch
Rui Barradas
2013-Jan-24 18:54 UTC
[R] functions as arguments to ther functions with inlinedocs
Hello, Your function declaration has a syntax error, one left parenthesis too much. Corrected it would be dummyfunction <- function(filters = function(x) {b = 0; x > b} ){ # rest of code here filters # this returns a function, don't need return() } x <- -5:5 f <- dummyfunction() # this creates the default function f(x) g <- dummyfunction(mean) # this creates another function g(x) As you can see, you can use default functions as arguments in your function declaration. Hope this helps, Rui Barradas Em 24-01-2013 18:32, Jannis escreveu:> Dear R community, > > I have a problem when I use functions as default values for argumnents > in other functions. When I use curly brackets { here, I can not create a > package with inlinedocs. It will give me the error when using > package.skeleton() in my package structure: > > Error in parse(text = utxt) : <text>:4:0: unexpected end of input > > > For example: > > dummyfunction = function(filters = function(x) {b = 0; x > b} )) > { > # rest of code here > return(filters) > } > > > This seems to me as a legal function declaration but creates the above > mentioned error. Is this an error of inlinedocs or do I misunderstand > the R language? Or is there another way of using functions in such a way > as arguments? In this case I could easily define this filters argument > inside the function for cases when it is not supplied as an argument but > I have some more complex functions where I really need to define > something sequential as an argument like: > > dummyfunction = function(filters = {a = 1; b > a; b}) {print('test')} > > > I hope I could clarify my problem. > > > Thanks a lot > Jannis > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Possibly Parallel Threads
- invalid strings while parsing code with inlinedocs package.skeleton.dx()
- including figures in html documentation/help
- Why do methods of "initialize" have no "srcref" attribute as other S4 mehtods?
- dump.frames, debugger and the "..." argument
- prompt () and backticks for default arguments