With one R markdown file, I would like to create different possible output pdf documents, where the output file name should be defined within the document. Is there any way to convince markdown to manipulate the output filename in such a way? Ideally I would like to pass the filename by an r chunk.

推荐答案

You can keep the simplicity of using the RStudio Knit button and reproducibility of a YAML header by using the undocumented knit hook to redefine what the button does (default function called is rmarkdown::render). The output_file parameter of the render function specifies the file name, so by setting it you override the standard behaviour of using the same prefix as the input filename.

e.g. to always output a file called myfile.pdf

knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding, output_file = file.path(dirname(inputFile), 'myfile.pdf')) })

The function can be an anonymous one-liner as well as imported from a package, as seen here with slidify.

You can set your own YAML headers (I don't know if this is generally advised anyway), accessible under rmarkdown::metadata$newheader but they don't seem available from within this sort of function as far as I can see.

As for passing file name in from an R chunk... if you're referring to code chunks below the YAML header, from my experience I don't think that's possible(?). Headers can contain inline R commands (single backtick-enclosed, starting with r), but seemingly not for this hook function.

Related:

R相关问答推荐

使用ggcorrplot在相关性矩阵上标注supertitle和index标签

在通过最大似然估计将ODE模型与数据匹配时,为什么要匹配实际参数的转换值?

为什么当我try 在收件箱中使用合并功能时会出现回收错误?

警告:lmdif:info = 0. nls. lm()函数的输入参数不正确

用预测NLS处理R中生物学假设之上的误差传播

使用strsplit()将向量操作为数据框

如何调整曲线图中的y轴标签?

提取第一个下划线和最后一个下划线之间的任何内容,例外情况除外

R Read.table函数无法对制表符分隔的数据正常工作

以NA为通配符的R中的FULL_JOIN以匹配其他数据中的任何值.Frame

在R中使用列表(作为tibble列)进行向量化?

变长向量的矢量化和

数据集上的R循环和存储模型系数

如何移动点以使它们的打印不重叠

R-找出存在其他变量的各种大小的所有组合

R-使用stri_trans_General()将其音译为德语字母

如何使用grepl()在数据帧列表中 Select 特定字符串?

如何为包创建自定义roxygen2标签?

如何编辑被动式数据表?

如何在R中添加标识连续日期的新列