How can I write 3 blocks of text to the correspondind files when both the blocks of code and the file names are in separate lists

sunse
sunse used Ask the Experts™
on
Hello,

I have  a list of lists.  The lists in the list of lists are file names.  I use lapply to read and merge the contents of each list in the list of lists (3 merged contents in this case  which will be the content of 3 files).  Then, I  have to change the name of the 3 resulting files and finally I have to write the contents of the files to each file.

 lc <- list("test.txt", "test.txt", "test.txt", "test.txt")
 lc1 <- list("test.txt", "test.txt", "test.txt")
 lc2 <- list("test.txt", "test.txt")
#list of lists.  The lists contain file names
 lc <- list(lc, lc1, lc2)
#new names for the three lists in the list of lists
 new_dataFns <- list("name1", "name2", "name3")
 file_paths <- NULL
 new_path <- NULL
#add the file names to the path and read and merge the contents of each list in the list of lists
 lapply(
    lc,
    function(lc) {
     filenames <- file.path(dataFnsDir, lc)
     dataList= lapply(filenames, function (x) read.table(file=x, header=TRUE))
     Reduce(function(x,y) merge(x,y), dataList)
     #   print(dataList)

    }
  )  

#add the new name of the file to the path total will be 3 paths/fille_newname.tsv.  
 lapply(new_path, function(new_path){new_path <- file.path(getwd(), new_dataFns)

The statements above work because lc and  new_dataFns are global and I can pass them to the lapply function

#Finally, I need to write the merged contents to the corresponding file (path/name.tsv).  I tried the following statement, but this does not work.  How can I write the content to each file? I was trying to use list <- cbind(dataList, new_path) so that afterwards I can get the merged contents and the file_name from the list and that way write each merged content to the corresponding file, but it seems that the dataList and the newPath are not global and the cbind() function does not work.
 

Thanks


This program works because I made the varibles inisde lapply global by using the <<- operator.  However, it does not work with the real files in the real program. These are .tsv files whith named columns.  The answer I get when I run the real program is: Error: (converted from warning) Error in : (converted from warning) Error in <Anonymous>: arguments imply differing number of rows: 3455, 4319.  What might be causing this?

    lc <- list("test.txt", "test.txt", "test.txt", "test.txt")
    lc1 <- list("test.txt", "test.txt", "test.txt")
    lc2 <- list("test.txt", "test.txt")
    #list of lists.  The lists contain file names
    lc <- list(lc, lc1, lc2)
    #new names for the three lists in the list of lists
    new_dataFns <- list("name1", "name2", "name3")
    file_paths <- NULL
    new_path <- NULL
    #add the file names to the path and read and merge the contents of each list in the list of lists
    lapply(
      lc,
      function(lc) {
        filenames <- file.path(getwd(), lc)
        dataList <<- lapply(filenames, function (lc) read.table(file=lc, header=TRUE))
        dataList <<- lapply(dataList, function(dataList) {merge(as.data.frame(dataList),as.data.frame(dataList))})
       
      }
    )  
   
    #add the new name of the file to the path total will be 3 paths/fille_newname.tsv.  
    lapply(new_dataFns, function(new_dataFns) {new_path <<- file.path(getwd(), new_dataFns)})
   
    print(new_path)
    print(dataList)
   
    finalFiles <- merge(as.data.frame(dataList), as.data.frame(new_path))
    print(finalFiles)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle SantosSoftware Test Analyst I at Dassault Systemes

Commented:
Hi,

I am here to help you with your open question.  Do you still need help?  I have the ability to alert more experts if you still need help.

If you solved the problem on your own, would you please post the solution here in case others have the same problem?

If you need me to delete this question just say "Delete."

Thank you for using Experts Exchange.

Regards,

Kyle Santos
Customer Relations
sunseApplication Designer

Author

Commented:
Hello Kyle,

i solved the problem.  Please see attached file.  The function parameters are passed to the function by a shiny app.  I there anyone who could help me with a shiny problem?  i will create a new question.

Thanks,

Giuseppa
Kyle SantosSoftware Test Analyst I at Dassault Systemes

Commented:
HI Sunse,

Thank you for letting me know.  I do not see the attached file.  Can you try to resend?  Make sure to hit 'upload' after you attach. ;)
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

sunseApplication Designer

Author

Commented:
I cannot see the button to attach the file.  Could you show me where is it?

Thanks
Application Designer
Commented:
No problem I found the attachment button.  Please let me know if you did not get it.
app.R
app.R
Kyle SantosSoftware Test Analyst I at Dassault Systemes

Commented:
Got it.  Thank you!
sunseApplication Designer

Author

Commented:
Thank you for following up on my topic.
Kyle SantosSoftware Test Analyst I at Dassault Systemes

Commented:
You're very welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial