I've created this function to get some information from two different files
   create_distance_to_strand <- function(data, data2, nameCsv) {
      newData  <- data  %>% 
        select(seqnames, start, end, V4, seqnames, geneStart, geneEnd, geneStrand, distanceToTSS, SYMBOL, geneLength)%>%
        rename(peak = V4)
      
      joined_df <- merge(newData , closest_dist, by.x = "peak", 
                         by.y = "peak", all.x = TRUE, all.y = TRUE) %>% drop_na()
      
      write.table(joined_df , nameCsv, sep = "\t")
    
      
      return(joined_df)
    }
    
    closest_dist = read.csv("closest_distance", header = T, sep ="\t")
    annotation = read.csv("results_annotation", header = T, sep ="\t") 
    myDistanceToStrand <- create_distance_to_strand(annotation,  closest_dist, "frame.csv")
It works as expected. However, I'm trying to make it more efficient in case I'd have different "closest_dist" files, in order to apply the function to all the files.
I've tried this:
    files <- list.files(pattern = "closest*")
    proof = lapply(files, create_distance_to_strand(annotation,  closest_dist, "proof.csv"))
But does not work
Error in h(simpleError(msg, call)) : 
      error in evaluating the argument 'y' in selecting a method for function 'merge': object 'closest_dist' not found 
Any advice? Thank you