我当时想同时显示输出并将保存到文件中。具体来说,我正在尝试同时打印sessionInfo()
的结果并将其保存到文件中。 capture.output()
并没有达到预期的效果。
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 18
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1 packrat_0.5.0
> y = capture.output(sessionInfo())
> print(y)
[1] "R version 3.5.1 (2018-07-02)"
[2] "Platform: x86_64-pc-linux-gnu (64-bit)"
[3] "Running under: Linux Mint 18"
[4] ""
[5] "Matrix products: default"
[6] "BLAS: /usr/lib/openblas-base/libblas.so.3"
[7] "LAPACK: /usr/lib/libopenblasp-r0.2.18.so"
[8] ""
[9] "locale:"
[10] " [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 "
[11] " [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C "
[12] " [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C "
[13] ""
[14] "attached base packages:"
[15] "[1] stats graphics grDevices utils datasets methods base "
[16] ""
[17] "loaded via a namespace (and not attached):"
[18] "[1] compiler_3.5.1 tools_3.5.1 packrat_0.5.0 "
> cat(y)
R version 3.5.1 (2018-07-02) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Linux Mint 18 Matrix products: default BLAS: /usr/lib/openblas-base/libblas.so.3 LAPACK: /usr/lib/libopenblasp-r0.2.18.so locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_3.5.1 tools_3.5.1 packrat_0.5.0
问题在于,使用print()
会导致打印计数器[1]
等,而使用cat()
会导致换行符丢失。有没有办法在捕获它的同时完全照常显示它?
答案 0 :(得分:2)
答案很简单。只需添加回行符并使用cat()
,即可:
cat(paste(y, collapse = "\n"))
一个人可以使用此便利功能来捕获并传递输出节目:
capture_save = function(expr) {
y = capture.output(eval(expr, envir = parent.frame()))
cat(paste(y, collapse = "\n"))
y
}
示例:
> y = capture_save(sessionInfo())
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 18
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1 packrat_0.5.0
> y
[1] "R version 3.5.1 (2018-07-02)"
[2] "Platform: x86_64-pc-linux-gnu (64-bit)"
[3] "Running under: Linux Mint 18"
[4] ""
[5] "Matrix products: default"
[6] "BLAS: /usr/lib/openblas-base/libblas.so.3"
[7] "LAPACK: /usr/lib/libopenblasp-r0.2.18.so"
[8] ""
[9] "locale:"
[10] " [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 "
[11] " [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C "
[12] " [9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C "
[13] ""
[14] "attached base packages:"
[15] "[1] stats graphics grDevices utils datasets methods base "
[16] ""
[17] "loaded via a namespace (and not attached):"
[18] "[1] compiler_3.5.1 tools_3.5.1 packrat_0.5.0 "