I'm trying to have output from a function that prints output not print to the console.
The capture.output
function is recommended in some answers, but it's not clear to me how to capture the output but still return the function's output.
E.g., if I have function f()
and want "printed output!"
to not print to the console but to have "value"
be returned:
f <- function() {
print("printed output!")
return("value")
}
# printed output is returned - want to capture the output
f()
#> [1] "printed output!"
#> [1] "value"
# also doesn't work, as captured output and function's output is returned
capture.output(f())
#> [1] "[1] \"printed output!\"" "[1] \"value\""
I think the solution may involve using sink (and con()
), but the answer that uses them does not use a function (and so I'm having difficulty applying the approach).