如何直接编织到R对象? [英] How to knit directly to R object?

查看:79
本文介绍了如何直接编织到R对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将knit() ted文档直接作为R对象作为字符向量存储在R中.

I'd like to store a knit()ted document directly in R as an R object, as a character vector.

我知道我可以通过knit()设置为tempfile()然后导入结果来做到这一点,就像这样:

I know I can do this by knit()ing to a tempfile() and then import the result, like so:

library(knitr)
library(readr)
ex_file <- tempfile(fileext = ".tex")
knitr::knit(text = "foo", output = ex_file)
knitted_obj <- readr::read_file(ex_file)
knitted_obj

返回

# [1] "foo\n"

按预期.

是否可以使用tempfile()而不通过直接将结果配管"到向量上来完成 的操作?

Is there a way to do this without using a tempfile() and by directly "piping" the result to a vector?

为什么,我到底要这个吗?

  • *.tex字符串将以编程方式保存到光盘,并在以后呈现为PDF.在下游功能中从光盘读取渲染的*.tex会使代码更复杂.
  • 缓存非常容易,并且移动到另一台计算机上.
  • 我真的很害怕一般的副作用,尤其是跨机器/OS的文件系统的恶作剧.我想将这些功能隔离到尽可能少的(print()save()plot())函数.
  • *.tex string will be programmatically saved to disc, and rendered to PDF later. Reading rendered *.tex from disc in downstream functions would make code more complicated.
  • Caching is just a whole lot easier, and moving this cache to a different machine.
  • I am just really scared of side effects in general and file system shenanigans across machines/OSes in particular. I want to isolate those to as few (print(), save(), plot()) functions as possible.

这会使我成为一个糟糕的(或仅仅是OCD的)R开发人员吗?

Does that make me a bad (or just OCD) R developer?

推荐答案

它应该像这样的一行一样简单:

It should be as straightforward as a single line like this:

knitted_obj = knitr::knit(text = "foo")

您可能想再次阅读帮助页面?knitr::knit,以了解返回的信息.

You may want to read the help page ?knitr::knit again to know what it returns.

这篇关于如何直接编织到R对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆