简体   繁体   English

如何从 Python 脚本调用 R Markdown knit 函数

[英]How to call R Markdown knit function from Python script

I have a Python script that creates multiple .Rmd files, and I wanted a way to automatically turn them into .html's without having to manually knit each within RStudio.我有一个创建多个 .Rmd 文件的 Python 脚本,我想要一种方法来自动将它们转换为 .html 文件,而无需在 RStudio 中手动编织每个文件。 I've probably spent around 4 hours researching and trying different options, and although I've managed to make it work by calling a .R script with我可能花了大约 4 个小时研究和尝试不同的选项,尽管我已经设法通过调用 .R 脚本来使其工作

subprocess.Popen(['Rscript', '--vanilla', 'rmd2html.R'], shell=False)

that then does the knitting with然后编织

rmarkdown::render("dicionarioNew.Rmd", "html_document") , rmarkdown::render("dicionarioNew.Rmd", "html_document") ,

this for some reason does not use UTF-8 (which I need) and doesn't easily allow me to store the number of times the program has been run (necessary for giving a different name to each html file).出于某种原因,这不使用 UTF-8(我需要)并且不容易让我存储程序运行的次数(为每个 html 文件提供不同的名称所必需的)。

Your title question is answered in your question's body but you have more specific needs not met by your current implmentation:您的标题问题在您的问题正文中得到了回答,但您当前的实现无法满足您的更具体需求:

  1. how to render .Rmd using UTF-8如何使用 UTF-8 渲染 .Rmd
  2. how to render .Rmd to html with custom filename output如何使用自定义文件名输出将 .Rmd 渲染为 html

I suggest you try seeking out the answers to these individually.我建议您尝试单独寻找这些问题的答案。

As a general answer to this question however, I suggest you consider using the Rscript command to run a custom R script which does what you want based on the source of rmd2html.R .但是,作为对这个问题的一般答案,我建议您考虑使用Rscript命令运行自定义 R 脚本,该脚本根据rmd2html.R 的源执行您想要的操作 You might also use R -e to execute a line or few of R code hardcoded as a string in your python script.您还可以使用R -e来执行一行或几行硬编码为 Python 脚本中的字符串的 R 代码。

If you want to break things out further in python, there are many options for rendering at the chunk or file level individually using the sweave, rmarkdown, stationary, and other R packages.如果你想在 python 中进一步分解,有很多选项可以使用 sweave、rmarkdown、steady 和其他 R 包在块或文件级别单独渲染。 Given a more specific example of what you are trying to accomplish someone may be able to help point you towards which of these many options would be right for your use case.给出一个更具体的示例,说明您要完成的任务,某人可能会帮助您指出这些选项中的哪一个适合您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM