calling objects from an embedded function
0
0
Entering edit mode
6.5 years ago
Assa Yeroslaviz ★ 1.8k

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 • 1.2k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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) {
...
}
ADD REPLY

Login before adding your answer.

Traffic: 2153 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6