Call Refenrence Class function by string [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Is there a easy way to call a function of object of a reference class by string like a do.call("...",...) for standard functions in R?

Here's a class and instance
A <- setRefClass("A",
fields=list(x="numeric"),
methods=list(value=function() x))
a <- A(x=10)
A funky way of invoking the value method is
> a[["value"]]
Class method definition for method value()
function ()
x
<environment: 0x123190d0>
suggesting that we could do
> do.call("[[", list(a, "value"))()
[1] 10
This has some pretty weird semantics -- the function returned by do.call seems to be independent of the instance, but actually is defined in the instance' environment
> fun = do.call("[[", list(a, "value"))
> fun
Class method definition for method value()
function ()
x
<environment: 0x1c7064c8>
> a$x=20
> fun()
[1] 20
Also, functions are instantiated in a 'lazy' way, so a[["value"]] only returns a function if it has already been called via a$value(). As discussed on ?setRefClass, I think one can force definition of the method at the time of object initialization with
A <- setRefClass("A",
fields=list(x="numeric"),
methods=list(
initialize=function(...) {
usingMethods("value")
callSuper(...)
},
value=function() x))

Related

Missing 1 required positional argument, Recursion - Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
def times(n,k):
if k >= 1:
return times(k-1) + n
times("140",3)
Traceback (most recent call last):
File "C:\Users\insane 18\Desktop\mat.py", line 21, in <module>
times("140",3)
File "C:\Users\insane 18\Desktop\mat.py", line 19, in times
return times(k-1) + n
TypeError: times() missing 1 required positional argument: 'k'
[Finished in 0.1s with exit code 1]
i keep getting this error even though i am putting the value of k but still getting this. please help me
Your times function takes two arguments as per your definition, namely k and n. However, when yyou recursively call the function times you're just supplying one argument(k) and not the other (n).
If you change your code to the following, it works.
def times(k, n):
if k<=1:
return times(k-1,n) # n has been moved inside the parentheses.
As a general guideline: Python doesn't have Tail Call Optimization and it is advised to avoid recursive calls.
The error is caused because you gave only one argument when you called times in line return times(k-1) + n

Recursive default argument reference [duplicate]

This question already has an answer here:
Unexpected behaviour with argument defaults
(1 answer)
Closed 5 years ago.
Can anyone explain me what is wrong in this code below. What I thought I am doing here is
a declaration of a global variable a=5
a definition of a function fun which takes one argument which defaults to the aforementioned global variable a
And when I call fun() without any parameters the local variable a becomes a copy of the global variable a and at any point in the function code it takes precedence over the global a (unless I specifically use get("a", envir=parent.frame))
But I must be wrong. Why isn't it allowed?
> a = 5
> fun = function(a=a) { a + 1 }
> fun(4)
[1] 5
> fun()
Error in fun() :
promise already under evaluation: recursive default argument reference or earlier problems?
And when I call fun() without any parameters the local variable a becomes a copy of the global variable a
No: default arguments are evaluated inside the scope of the function. Your code is similar to the following code:
fun = function(a) {
if (missing(a)) a = a
a + 1
}
This makes the scoping clearer and explains why your code doesn’t work.
Note that this is only true for default arguments; arguments that are explicitly passed are (of course) evaluated in the scope of the caller.

Define a S3 function using UseMethod [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to the programming R. I defined a function called liw.mstreeClass and I defined as below, but when I run the program I am keep getting the following errors:
# define method: lcosts(generic dispatch)
liw.mstreeClass <- function(nb, data, method, p) UseMethod("nbcosts"){
Error: unexpected '{' in "liw.mstreeClass <- function(nb, data, method, p) UseMethod("nbcosts"){"
if(method=="penrose") { liw <- mat2listw(penroseDis.mstreeClass((scale(data))))
return(liw)}
Error: object 'method' not found
}
Error: unexpected '}' in " }"
# liw.mstreeClass <- function(nb, data, method, p) UseMethod("nbcosts"){
# Error: unexpected '{' in "liw.mstreeClass <- function(nb, data, method, p)
Well, to start with, you've got a syntax error here. You can group several expressions with curly brackets but not start curly brackets after an expression.
Compare...
mean(1)
... with ...
mean(1){
# error!!
Secondly, in S3 you define methods for already existing generic functions. So if you have a function "liw" that could be applied to several classes, then liw.mstreeClass would define the way to do the "liw" for a class called "mstreeClass". So you first have to define liw as a generic function:
liw<-function(x,...){
UseMethod("liw")
}
Notice that you must have "liw" as an argument to UseMethod, not some random crap. (Take a look at the manual to understand why.) You would rarely have a lot of code besides the call to UseMethod in a generic function's body.
And having done that, you can define an mstreeClass method for liw. For example,
liw.mstreeClass<-function(x, y, z){
paste("liw equals ", x + y + z)
}
Note that as method dispatch in S3 is based on the first argument, your x must have class "mstreeClass" - only in that case, liw(x) will be directed to liw.mstreeClass(x). And I think if your generic has x as the first argument then the first argument of all methods must be called x too.
UseMethod("nbcosts"){"
if(method=="penrose") { liw <- mat2listw(penroseDis.mstreeClass((scale(data))))
return(liw)}
Error: object 'method' not found
}
Error: unexpected '}' in " }"
Umm.. sorry, these lines don't make a lot of sense. See above or the manuals on how to use UseMethod.

How to catch an error/exception in R? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Exception handling in R
Does anyone have idea on how to catch an error or an exception in R?
Like Joshua said: use tryCatch. Include an error argument, which should be a function accepting one parameter (the error, typically called e).
tryCatch(
stop("you threw an error"),
error = function(e)
{
print(e$message) # or whatever error handling code you want
}
)
It really depends on what you mean by "catch". Look at tryCatch and withCallingHandlers.
Have you looked into stop?
This will allow you to catch exceptions that you define.

Exception handling in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Does anyone have examples/tutorials of exception handling in R? The official documentation is very terse.
Basically you want to use the tryCatch() function. Look at help("tryCatch") for more details.
Here's a trivial example (keep in mind that you can do whatever you want with an error):
vari <- 1
tryCatch(print("passes"), error = function(e) print(vari), finally=print("finished"))
tryCatch(stop("fails"), error = function(e) print(vari), finally=print("finished"))
Have a look at these related questions:
Equivalent of "throw" in R
catching an error and then branching logic
https://stackoverflow.com/search?q=[r]+trycatch
Besides Shane's answer pointing you to other StackOverflow discussions, you could try a code search feature. This original answer pointed to Google's Code Search has since been discontinued, but you can try
Github search as e.g. in this query for tryCatch in language=R;
Ohloh/Blackduck Code search eg this query for tryCatch in R files
the Debian code search engine on top of the whole Debian archive
Just for the record, there is also try but tryCatch may be preferable. I tried a quick count at Google Code Search but try gets too many false positives for the verb itself -- yet it seems tryCatch is more widely used.
This result from a related google search helped me: http://biocodenv.com/wordpress/?p=15.
for(i in 1:16){
result <- try(nonlinear_modeling(i));
if(class(result) == "try-error") next;
}
The function trycatch() is fairly straight forward, and there are plenty of good tutorials on that. A excellent explanation of error handling in R can be found in Hadley Wickham's book Advanced-R, and what follows is a very basic intro to withCallingHandlers() and withRestarts() in as few words as possible:
Lets say a low level programmer writes a function to calculate the absolute
value. He isn't sure how to calculate it, but knows how to construct an
error and
diligently conveys his naiveté:
low_level_ABS <- function(x){
if(x<0){
#construct an error
negative_value_error <- structure(
# with class `negative_value`
class = c("negative_value","error", "condition"),
list(message = "Not Sure what to with a negative value",
call = sys.call(),
# and include the offending parameter in the error object
x=x))
# raise the error
stop(negative_value_error)
}
cat("Returning from low_level_ABS()\n")
return(x)
}
A mid-level programmer also writes a function to calculate the absolute value, making use of the woefully incomplete low_level_ABS function. He knows that the low level code throws a negative_value
error when the value of x is negative and suggests an solution to the problem, by
establishing a restart which allows users of mid_level_ABS to control the
way in which mid_level_ABS recovers (or doesn't) from a negative_value error.
mid_level_ABS <- function(y){
abs_y <- withRestarts(low_level_ABS(y),
# establish a restart called 'negative_value'
# which returns the negative of it's argument
negative_value_restart=function(z){-z})
cat("Returning from mid_level_ABS()\n")
return(abs_y)
}
Finally, a high level programmer uses the mid_level_ABS function to calculate
the absolute value, and establishes a condition handler which tells the
mid_level_ABS to recover from a negative_value error by using the restart
handler.
high_level_ABS <- function(z){
abs_z <- withCallingHandlers(
# call this function
mid_level_ABS(z) ,
# and if an `error` occurres
error = function(err){
# and the `error` is a `negative_value` error
if(inherits(err,"negative_value")){
# invoke the restart called 'negative_value_restart'
invokeRestart('negative_value_restart',
# and invoke it with this parameter
err$x)
}else{
# otherwise re-raise the error
stop(err)
}
})
cat("Returning from high_level_ABS()\n")
return(abs_z)
}
The point of all this is that by using withRestarts() and withCallingHandlers(), the function
high_level_ABS was able to tell mid_level_ABS how to recover from errors
raised by low_level_ABS error without stopping the execution of
mid_level_ABS, which is something you can't do with tryCatch():
> high_level_ABS(3)
Returning from low_level_ABS()
Returning from mid_level_ABS()
Returning from high_level_ABS()
[1] 3
> high_level_ABS(-3)
Returning from mid_level_ABS()
Returning from high_level_ABS()
[1] 3
In practice, low_level_ABS represents a function that mid_level_ABS calls a
lot (maybe even millions of times), for which the correct method of error
handling may vary by situation, and choice of how to handle specific errors is
left to higher level functions (high_level_ABS).
The restart function is very important in R inherited from Lisp. It is useful if you want to call some function in the loop body and you just want the program to continue if the function call collapses. Try this code:
for (i in 1:20) withRestarts(tryCatch(
if((a <- runif(1))>0.5) print(a) else stop(a),
finally = print("loop body finished!")),
abort = function(){})

Resources