R the number of times the expression evaluates to a for loop

How many times list.files ('dir_path') is evaluated in a for loop? Is it equal to the number of files present in the directory? How to check it?

for (infile in list.files('dir_path')){
     #doSomething()
}

Should I create a variable first and then pass it to the loop?

For example:

selected_files = list.files('dir_path')

for (infile in selected_files){
         #doSomething()
    }

thanks

+4
source share
1 answer

list.fileswill only be evaluated once when you use it in a for loop, like the one you suggest. The easiest way to check this is to wrap the call list.filesin another function call, for example:

f <- function() { print("Calling f"); list.files() }

and use this for for loop:

for (infile in f())
{
    print(infile)
}
+6
source

Source: https://habr.com/ru/post/1608364/


All Articles