R: “For Loop” is Creating a Single File Instead of Two Files

I am using the R programming language. I have a "data frame" called "a" that has 2 rows and 1 column (the column is called "file.list"):

>head(a)
                                               file.list
1 C:/Users/Documents/files_i_want/1_a.pdf
2 C:/Users/Documents/files_i_want/a_1.pdf

I am trying to write a "for loop" that performs an operation using each "element" from this data frame:

library(tesseract)
library(pdftools)

for (i in 1:2)
 { 

file_i = tesseract::ocr(a[i, "file.list"]) 

}

The above code successfully runs:

Converting page 1 to 1_a_1.png... done!
Converting page 1 to a_1_1.png... done!

Problem: The above loop is only creating a single file instead of two files. Does anyone know why this is happening? Can someone please show me how to fix this problem?

Thanks

Two iterations of for loop write to same variable: file_i, and NOT file_1 and file_2. So, the second iteration overwrites results of first variation.

One can use assign for these purposes.

1 Like

thank you! I ended up solving this problem - would you like to see the answer?

Yes, please show the solution.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.