Iterating Properly with Custom Function in R

you have quite a few options really

  1. you can use the magic_for library

init

magicfor::magic_for(print, progress=T, silent=T) # initialisation with print
for(i in 1:length(input_files)){
  df0 <- fxml_importXMLFlat(input_files[i])
  df1 <- fxml_toDataFrame(df0, siblings.of=1, elem.or.attr="elem",col.attr = elem.)
  print(df1) # important for magic_for as expressed ininitialisation
}
magic_result # or magic_result_as_dataframe() or magic_result_as_vector
  1. you can make a global variable to append your df to (it seems you try to do it only inside the loop with df1<-c(df0, ) but as you don’t save it to a global variable it gets overwritten

    df<-data.frame() # global variable
         for(i in 1:length(input_files)){
           df0 <- fxml_importXMLFlat(input_files[i])
           df1 <- fxml_toDataFrame(df0, siblings.of=1, elem.or.attr="elem",col.attr = elem.)
         df<-c(df, df1)
         }
    
  2. you could use lapply/sapply by simply putting

    result<-lapply(input_files, fxml_importXMLFlat)  # i used lapply for lists - sapply does the same for data.frames
    

i am sure there are even more ways to do it but of the top of my head i would suggest option 3

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top