1

I have the following function that return just a print out with cat().

show_something <- function() {cat("foo\n")}

What I want to do is to capture the output fo that function into a variable as a string.

I tried this:

> x <- show_something()
foo
> x
NULL
>

As you can see x return NULL. How can I get x to capture foo?

littleworth
  • 4,781
  • 6
  • 42
  • 76

1 Answers1

3

cat doesn't return anything but you can use capture.output here -

show_something <- function() {cat("foo\n")}
x <- capture.output(show_something())
x
#[1] "foo"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213