如何将 cat() 函数输出保存为 R 中的值?
How to save cat() function output as a value in R?
当我在 RStudio 中 运行 以下代码时,输出在控制台中返回,但它不会在全局环境中保存为值 - 相反,它 returns 作为 NULL价值。我注意到每次使用 cat()
函数时都会发生这种情况。为什么会这样,我该如何解决?
test <- cat(paste0(c("hello", "\n", "goodbye",
capture.output(sessionInfo())
)))
总结评论中的答案,两种解决方案是:
- 根本不要使用
cat()
,因为它是创建控制台输出的函数,不需要存储数据。
test <- paste0(c("hello", "\n", "goodbye",
sessionInfo()[1:5]))
test # or cat(test)
#> [1] "hello"
#> [2] "\n"
#> [3] "goodbye"
#> [4] "list(platform = \"x86_64-w64-mingw32\", arch = \"x86_64\", os = \"mingw32\", system = \"x86_64, mingw32\", status = \"\", major = \"4\", minor = \"0.3\", year = \"2020\", month = \"10\", day = \"10\", `svn rev` = \"79318\", language = \"R\", version.string = \"R version 4.0.3 (2020-10-10)\", nickname = \"Bunny-Wunnies Freak Out\")"
#> [5] "x86_64-w64-mingw32/x64 (64-bit)"
#> [6] "LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252"
#> [7] "Windows 10 x64 (build 19041)"
#> [8] "c(\"Mersenne-Twister\", \"Inversion\", \"Rejection\")"
- 捕获控制台的输出,不仅围绕会话信息,而且围绕整个事情。
test <- capture.output(cat(
paste0(c("hello", "\n", "goodbye", sessionInfo()[1:5]))
))
test
#> [1] "hello "
#> [2] " goodbye list(platform = \"x86_64-w64-mingw32\", arch = \"x86_64\", os = \"mingw32\", system = \"x86_64, mingw32\", status = \"\", major = \"4\", minor = \"0.3\", year = \"2020\", month = \"10\", day = \"10\", `svn rev` = \"79318\", language = \"R\", version.string = \"R version 4.0.3 (2020-10-10)\", nickname = \"Bunny-Wunnies Freak Out\") x86_64-w64-mingw32/x64 (64-bit) LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252 Windows 10 x64 (build 19041) c(\"Mersenne-Twister\", \"Inversion\", \"Rejection\")"
注意:出于演示目的,我将会话信息限制为前 5 个元素。
由 reprex package (v2.0.0)
于 2021-04-25 创建
当我在 RStudio 中 运行 以下代码时,输出在控制台中返回,但它不会在全局环境中保存为值 - 相反,它 returns 作为 NULL价值。我注意到每次使用 cat()
函数时都会发生这种情况。为什么会这样,我该如何解决?
test <- cat(paste0(c("hello", "\n", "goodbye",
capture.output(sessionInfo())
)))
总结评论中的答案,两种解决方案是:
- 根本不要使用
cat()
,因为它是创建控制台输出的函数,不需要存储数据。
test <- paste0(c("hello", "\n", "goodbye",
sessionInfo()[1:5]))
test # or cat(test)
#> [1] "hello"
#> [2] "\n"
#> [3] "goodbye"
#> [4] "list(platform = \"x86_64-w64-mingw32\", arch = \"x86_64\", os = \"mingw32\", system = \"x86_64, mingw32\", status = \"\", major = \"4\", minor = \"0.3\", year = \"2020\", month = \"10\", day = \"10\", `svn rev` = \"79318\", language = \"R\", version.string = \"R version 4.0.3 (2020-10-10)\", nickname = \"Bunny-Wunnies Freak Out\")"
#> [5] "x86_64-w64-mingw32/x64 (64-bit)"
#> [6] "LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252"
#> [7] "Windows 10 x64 (build 19041)"
#> [8] "c(\"Mersenne-Twister\", \"Inversion\", \"Rejection\")"
- 捕获控制台的输出,不仅围绕会话信息,而且围绕整个事情。
test <- capture.output(cat(
paste0(c("hello", "\n", "goodbye", sessionInfo()[1:5]))
))
test
#> [1] "hello "
#> [2] " goodbye list(platform = \"x86_64-w64-mingw32\", arch = \"x86_64\", os = \"mingw32\", system = \"x86_64, mingw32\", status = \"\", major = \"4\", minor = \"0.3\", year = \"2020\", month = \"10\", day = \"10\", `svn rev` = \"79318\", language = \"R\", version.string = \"R version 4.0.3 (2020-10-10)\", nickname = \"Bunny-Wunnies Freak Out\") x86_64-w64-mingw32/x64 (64-bit) LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252 Windows 10 x64 (build 19041) c(\"Mersenne-Twister\", \"Inversion\", \"Rejection\")"
注意:出于演示目的,我将会话信息限制为前 5 个元素。
由 reprex package (v2.0.0)
于 2021-04-25 创建