简体   繁体   中英

How to save cat() function output as a value in R?

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

控制台输出 全球环境

Summarising the answers in the comments the two solutions are:

  1. Do not use 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\")"
  1. Capture the output of the console not only around session info but around the whole thing.
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM