Question: calling objects from an embedded function
0
gravatar for Assa Yeroslaviz
18 months ago by
Assa Yeroslaviz1.2k
Munich
Assa Yeroslaviz1.2k wrote:

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

ADD COMMENTlink written 18 months ago by Assa Yeroslaviz1.2k

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

ADD REPLYlink written 18 months ago by Devon Ryan89k

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 REPLYlink modified 18 months ago • written 18 months ago by Assa Yeroslaviz1.2k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1181 users visited in the last hour