74

I am executing the following command in R :

system("ls ")

I need to store the output of the above command in some R variable. Is there a way to do the same??

user1021713
  • 2,133
  • 8
  • 27
  • 40

2 Answers2

145

Use intern=TRUE:

a <- system("ls ", intern = TRUE)
johannes
  • 14,043
  • 5
  • 40
  • 51
8

why not use the corresponding R function?

a <- list.files()
b <- list.files(recursive = TRUE)

For more details

?list.files
Thierry
  • 18,049
  • 5
  • 48
  • 66
  • 1
    Because `list.files` also lists directories as items, and if you use `recursive=T` it dives into those directories. There is no simple R way to list _just the files_ in a directory. I use `file_list = system('ls -p | grep -v /')` – abalter Mar 11 '19 at 01:37