61

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
  • 1,893
  • 8
  • 25
  • 39

2 Answers2

117

Use intern=TRUE:

a <- system("ls ", intern = TRUE)
johannes
  • 12,861
  • 5
  • 37
  • 49
9

why not use the corresponding R function?

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

For more details

?list.files
Thierry
  • 17,066
  • 4
  • 42
  • 65
  • 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