In Python you can do this:
def boring_function(x):
a = x + 1
b = x - 1
c = x / 1
return a, b, c
a, b, c = boring_function(1)
I am looking for an equivalent method to use in R. So far, I have been trying to make an R list to retrieve the variables as follows:
boring_function = function(x) {
a = x+1
b = x-1
c = x/1
l = list(a = a, b = b, c = c)
return(l)
}
result = boring_function(1)
a = result[["a"]]
b = result[["b"]]
c = result[["c"]]
However, note that this is greatly simplified example, and in reality I am making very complex functions that need to return values, lists etc separately. And in those cases, I have to return a single list that contain values, vectors, matrices, lists in lists etc. As you can imagine, the end result looks really messy and extremely hard to work with. Imagine, very often I have to do something like this in order to get my specific result from an R function:
specific_result = complex_function(x, y, z)[["some_list"]][["another_list"]][["yet_another_list"]][finally_the_result]
I find this method very confusing and I would prefer the Python approach. But I have to do my work in R. Seems like it is not possible to have the exact Python approach, but, is there anything similar in R? If not, what is the best way to do what I want to do?