Link to home
Start Free TrialLog in
Avatar of sunse
sunseFlag for United States of America

asked on

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

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)
Avatar of Kyle Santos
Kyle Santos
Flag of United States of America image

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
Avatar of sunse

ASKER

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
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. ;)
Avatar of sunse

ASKER

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

Thanks
ASKER CERTIFIED SOLUTION
Avatar of sunse
sunse
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it.  Thank you!
Avatar of sunse

ASKER

Thank you for following up on my topic.
You're very welcome.