I think this is a simple question.

As for many languages, you need to provide the return type before you write a function. however I didn't find solution for R. the only way I can do right now is to make a call, and using str(),mode(),class() to check the returned value.

but if the function takes long time, I can't use this way Is there a simple way to know the return type of the function even before I call it?

by the way, I could find some of return type by typing ?function_name, but many helps didn't mention the return type of the function.

Okey, this is the senario why known this info could be so useful:

1.I need the return type to see, after I got the return value, how should I deal with the return, for simple case, if I don't know the return type is list or dataframe, I don't actually have the info to decide which function to use next~Sometimes you don't know you got S3 or S4 object, which made you don't know you should use @ or $ to deal with it

2.suppose there are two functions in two package do the same thing, one return me a connection and another return me a html object, if I knew the return type then I can easily pick which function I should use based on my case. Sometimes you only have limit times to connect to some places, then you will waste several of your chances to check out the return type

7

2 Answers

In short no...

R is a dynamically typed language and many of it's function return different types depending on the parameters passed, being this in my opinion one of the strong points of R, many functions accept many types and return many types. A quick example:

mode(sapply(vector(mode="list", 10) ,function (x) return ('a'))) [1] "character" mode(sapply(vector(mode="list", 10) ,function (x) return (1))) [1] "numeric" 

Here sapply returns a "character" type or a "numeric" type depending on the function passed to apply on each of the elements. Overall you just have to get used to the language and if nothing works do what you are doing on small tasks first.

2

In R, there are a number of ways to handle multiple return types from a function.

Let's say you have a function f that calls g, which can return various types. Your first option is with an explicit type check:

f <- function(x) { ... y <- g(x) if(is.data.frame(y)) { # process result as a data frame } else if(is.list(y)) { # process y as a list # this must go after is.data.frame, because a list is also a data frame } ... } 

Now f will check the result of g when it returns, and then call the appropriate code to handle it.

This is fine if you only have a small number of possible types to choose from. Once the number of types becomes large, it's a better option to use something more systematic. That option would be to use R's object framework(s). The simplest framework is S3, so let's look at that.

f <- function(x) { y <- g(x) f_result(y) } # this is the f_result _generic_: it dispatches individual methods based on the class of y f_result <- function(y) { UseMethod("f_result") } # f_result _method_ for data frames f_result.data.frame <- function(y) { # process result as a data frame } # f_result method for lists f_result.list <- function(y) { # process result as a list } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.