This is a test version of Biostars. For the public version, visit https://www.biostars.org.
calling objects from an embedded function

I am trying to call for an object from a nested function. The object itself was created in the first (top) function.

the function structure i am having is similar to this:

top_function(input = input_file) {
# hier I am reading in the data, converting it and modify the structure
 sample_file = read.delim2(...)
 sample_mat <- as.matrix(sample_file)
# setting output directory
  outputdir <- paste0(sample_name, "_Output/")
  nested_function (mat= sample_mat) {
    # here I run some more calculations and than I would like the results (such as table/plots  ) to be saved in the output folder created in the top part. 
  saveRDS(object = cl, file = paste0(outputdir, sample_name,"_cl.RDS"))
  pdf(paste0(outputdir, sample_name,"_Clustering_iterations.pdf"))
  ...
  dev.off()
  }  
}

But when I am running the function I get the error -

Error in paste0(outputdir, sample_name, "_cl.RDS") : 
  object 'outputdir' not found

What i am missing here? How can make the function recognize the object I created in the top function?

thanks Assa

function nested-function object

Have your googled "R programming scope"? You're just being confused by variable scope.

Yes. I did, but it didn't help much. I have tried to understand how to correct it, but it seems, that everything I try keep briniging me to the same error message. What Am I doing wrong here?

Do I need to define each time all the arguments needed in the nested function. meaning Should the call to the nested functioned be something like that:

nested_function (mat= sample_mat, outputdir = outputdir, sample_name = sample_name) {
...
}

0 answers

No answers yet.

Log in to answer this question.