简体   繁体   中英

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. 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

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

that then does the knitting with

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).

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
  2. how to render .Rmd to html with custom filename output

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 . You might also use R -e to execute a line or few of R code hardcoded as a string in your python script.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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