Christoph
Christoph

Reputation: 7063

List all files in a folder without subfolders in R

I use li <- list.files(path, full.names = F, include.dirs = F). To my surprise li also contains folders if there are any. I thought I can switch that off using include.dirs = F. Am I wrong?
I am using R version 3.2.2 (2015-08-14) on Windows.

Upvotes: 2

Views: 2773

Answers (2)

Kunal Puri
Kunal Puri

Reputation: 3427

@Christoph, If you refer to the help provided by R, the statement written is

include.dirs: logical. Should subdirectory names be included in recursive listings? (They always are in non-recursive ones)..

I want to highlight the term: (They always are in non-recursive ones).

As rightly said by @RichardTelford, by default, the value of recursive = FALSE.

If you really want to do the task, you can give a try to this code:

setdiff(list.files(path,full.names=T),list.dirs(path,recursive=F))

Upvotes: 6

user2007598
user2007598

Reputation: 163

The related answers are quite relevant here as they are related: :-)

However, if you are looking for R scripts in a folder and want to leave out the other things: {subfolders, rds files, etc}.

This is one way to do it:

list.files(pattern = ".R$")

$ in regex expressions looks at the end of a string

^ similarly looks for expressions at the start of a string

Upvotes: 3

Related Questions