How to capture the output of system()

后端 未结 2 511
无人及你
无人及你 2021-01-17 08:47

This question was motivated by Rmarkdown not outputting results of system command to html file. For some reason, the output of system() in R (or system2()

2条回答
  •  日久生厌
    2021-01-17 09:07

    You could add a function knitr::system that masks base::system. Users could work with it like it was system::base, but the output can be captured by capture.output:

    system <- function(...) {
      stopifnot(!any(names(list(...)) %in% "intern"))
      result <- base::system(..., intern = TRUE)
      print(result)
    }
    

    I admit, that this is somewhat hacky, and to be honest, I'm not sure about possible side effects. But I think it could be worth a try.

提交回复
热议问题