函数获取RMarkdown文档的YAML标头中没有完整路径的文件名

编程入门 行业动态 更新时间:2024-10-22 13:32:52
本文介绍了函数获取RMarkdown文档的YAML标头中没有完整路径的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图通过更改RStudio编织按钮的行为,更改其写入编织Rmd文件的输出的目录.我从此答案开始,但是我不想使用固定字符串给出的文件名,而是使用根据Rmd文件名输出文件名.但是,变量inputFile包含Rmd文件的完整路径.有没有办法只获取文件名而没有路径?

I am trying to modify the behavior of RStudio's knit button, by changing the directory to which it writes the output of knitting the Rmd file. I have started with this answer, but instead of having the filename given by a fixed string, I'd like to have the output filename based on the Rmd filename. However, the variable inputFile includes the full path to the Rmd file. Is there a way to get only the filename without the path?

我正在使用的标头会生成完整的路径+文件名,而我只需要文件名(test2是我在当前工作目录中创建的目录):

The header I am working with that produces the full path+filename where I'd like just the filename (test2 is a directory that I created in the current working directory):

--- knit: (function(inputFile, encoding) {rmarkdown::render(inputFile,encoding=encoding, output_file=file.path(dirname(inputFile), "test2", paste0(substr(inputFile,1,nchar(inputFile-4),".html"))) }) output: html_document ---

推荐答案

如今,knitr附带了一个函数current_input(),该函数为您提供Rmd文件的名称作为字符串.然后tools::file_path_sans_ext()将删除扩展名.

Nowadays, knitr comes with a function current_input() that gives you the name of the Rmd-file as a string. And tools::file_path_sans_ext() will remove the extension.

但是要解决OP的确切问题,今天可能有更好的选择,例如,编织器选项,软件包 ezknitr ,RStudio"编织目录" 按钮或 here::here() .

But to solve the exact problem of the OP, there are probably better options today, for example, knitr options, the package ezknitr, RStudio "Knit Directory" button, or here::here().

更多推荐

函数获取RMarkdown文档的YAML标头中没有完整路径的文件名

本文发布于:2023-10-31 18:57:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件名   路径   函数   完整   文档

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!