I'd like to store a
ex_file <- tempfile(fileext = ".tex")
knitr::knit(text = "foo", output = ex_file)
knitted_obj <- readr::read_file(ex_file)
#  "foo\n"
You can use
con <- textConnection("varname", "w") to create a connection that writes its output to variable
varname, and use
output=con in the call to
knit(). For example:
library(knitr) con <- textConnection("knitted_obj", "w") knit(text="foo", output = con) close(con) knitted_obj
returns the same as your tempfile approach. Multiple lines will
show up as different elements of
knitted_obj. I haven't timed it, but text connections have a reputation for being slow, so this isn't necessarily as fast as writing to the file system.