81

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??

2 Answers 2

158

Use intern=TRUE:

a <- system("ls ", intern = TRUE)
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to store the information if one makes a system call to a program?
Is there any way to get both the os program status code and the output of the program?
What if I am shelling out to an SQL command which returns 1 million rows? How can I redirect the output to a file first?
8

why not use the corresponding R function?

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

For more details

?list.files

1 Comment

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 <target dir> | grep -v /')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.