Grec001
Grec001

Reputation: 1121

how can I redirect system command and its output in r?

I am using system command to find files on ubuntu, and trying to redirect the result on screen to a txt file, as following example.

# make file
system("touch a.txt")
system("touch b.txt")
system("touch c.txt")
system("touch d.txt")

sink("t.txt")

 c("a.txt", "b.txt")%>% lapply(function(f) {
  system(sprintf("find -name %s", f)) 
}) 
sink()

but the result turns out to be a list with O's inside. Please advise how I can achieve that. Thanks.

Upvotes: 1

Views: 442

Answers (1)

Waldi
Waldi

Reputation: 41220

You could use intern = TRUE option which returns system result directly into the R environment:

c("a.txt", "b.txt" , "test.txt") %>% lapply(function(f) {
  system(sprintf("find -name %s", f), intern = T)
}) 

[[1]]
[1] "./a.txt"

[[2]]
[1] "./b.txt"

[[3]]
character(0)


Upvotes: 2

Related Questions