0

I currently have a basic script written in R, which has two functions embedded within another:

FunctionA <- Function() {
  results_from_B <- FunctionB()
  results_from_C <- FunctionC()
}

Function B generates some data which is then analysed in Function C.

If I stop the code within function A, I can see the structure of results_from_C - this appears under 'values' and I can refer to different elements using the syntax results_from_C$column_name1.

I achieved this within Function C by specifying the returned values using:

return(list(column_name_1 = value1, column_name_2 = value2)

However, I cannot work out how I can return these same values (in the same structure) from Function A - everything I try returns a list which is formatted as 'Data' rather than 'Values' and cannot be indexed using the syntax results_from_A$column_name1.

Can anyone help me to understand what I need to do in order to extract results from Function C outside of Function A?

Thanks in advance

Texas
  • 903
  • 1
  • 11
  • 31
  • 1
    It would be easier to help you if you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. Show exactly what you tried and so it's easier to see what you were trying to accomplish. If you provide something we can test with we can verify any possible solutions. – MrFlick Aug 17 '17 at 18:33
  • results_from_C should be the output of your function, unless in your real case you print something afterwards, or are actually assigning to a subset of results_from_C – moodymudskipper Aug 17 '17 at 18:42

1 Answers1

1

I don't understand what you mean by formatted as 'Data' rather than 'Values'. There's nothing wrong with the setup you describe, I every now and then use functions inside functions, it's perfectly OK.

(Note that R is case sensitive, it's function not Function.)

FunctionA <- function() {
  FunctionB <- function() 1:2*pi
  FunctionC <- function(x) 
      list(column_name_1 = x[1], column_name_2 = x[2])

  results_from_B <- FunctionB()
  results_from_C <- FunctionC(results_from_B)
  results_from_C
}

result <- FunctionA()
result
$column_name_1
[1] 3.141593

$column_name_2
[1] 6.283185

result$column_name_1
[1] 3.141593

Is this it? If not, please clarify your question.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thanks, this does address what I was struggling with, its also helped me to work out what's causing my issues. If I add one more line to your code above: `result1 <- replicate(1, FunctionA())` and look at the Environment viewer in R - `result` appears under the 'Values' section and `result1` appears under 'Data' Are you aware of how I could use the replicate function (I'll increase the value from 1 in reality) and still be able to refer to columns using the syntax `result1$column_name_1`? – Texas Aug 17 '17 at 19:02
  • @Texas `replicate` returns an object of class `matrix` so you'll need to access its elements with a different syntax. For instance, `result1["column_name_1", 1]`. A standard practice is to transpose the result, `result1 <- t(replicate(...))`. And then use the more intuitive `result1[1, "column_name_1"]` – Rui Barradas Aug 17 '17 at 19:13
  • @Texas I forgot, `replicate(..., simplify = FALSE)` will return a `list`. You can then access the values with `result1[[1]]$column_name_1`. – Rui Barradas Aug 17 '17 at 19:28
  • Just been trying out your first suggestion and seems to work perfectly, the second is really useful to be aware of too. Thank you very much for the help! – Texas Aug 17 '17 at 19:31