【问题标题】:how can I redirect system command and its output in r?如何在 r 中重定向系统命令及其输出?
【发布时间】:2023-03-18 04:16:01
【问题描述】:

我正在使用system 命令在ubuntu 上查找文件,并尝试将屏幕上的结果重定向到一个txt 文件,如下例所示。

# 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()

但结果是一个包含 O 的列表。 请告知我如何实现这一目标。谢谢。

【问题讨论】:

标签: r


【解决方案1】:

您可以使用intern = TRUE 选项将system 结果直接返回到R 环境中:

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)


【讨论】:

    猜你喜欢
    • 2015-04-10
    • 2019-01-05
    • 2014-01-27
    • 2014-12-11
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多