Question: calling objects from an embedded function
gravatar for Assa Yeroslaviz
2.3 years ago by
Assa Yeroslaviz1.3k
Assa Yeroslaviz1.3k 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"))

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 2.3 years ago by Assa Yeroslaviz1.3k

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

ADD REPLYlink written 2.3 years ago by Devon Ryan94k

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 2.3 years ago • written 2.3 years ago by Assa Yeroslaviz1.3k
Please log in to add an answer.


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