• R

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)
sunseApplication DesignerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle SantosSoftware Test Analyst ICommented:
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 DesignerAuthor 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 ICommented:
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. ;)
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

Thanks
sunseApplication DesignerAuthor Commented:
No problem I found the attachment button.  Please let me know if you did not get it.
app.R
app.R

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle SantosSoftware Test Analyst ICommented:
Got it.  Thank you!
sunseApplication DesignerAuthor Commented:
Thank you for following up on my topic.
Kyle SantosSoftware Test Analyst ICommented:
You're very welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
R

From novice to tech pro — start learning today.