0

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?

scamander
  • 3,755
  • 4
  • 31
  • 59

1 Answers1

2

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
  • 355,584
  • 18
  • 123
  • 178