Reputation: 585
When I run the following code in RStudio the output is returned in the console, but it doesn't save as a value in the global environment - instead, it returns as a NULL value. I've noticed this occurs every time I use the cat()
function. Why is this the case and how can I fix this?
test <- cat(paste0(c("hello", "\n", "goodbye",
capture.output(sessionInfo())
)))
Upvotes: 1
Views: 1458
Reputation: 5254
Summarising the answers in the comments the two solutions are:
cat()
at all since it is a function to create a console output and it is not necessary to store the data.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\")"
NOTE: for demonstration purposes I limited the session info to the first 5 elements.
Created on 2021-04-25 by the reprex package (v2.0.0)
Upvotes: 2