[英]Pipe R markdown through Knitr?
I found out from this question that you can call Knitr from a script like this: 我从这个问题中发现,您可以从如下脚本中调用Knitr :
Rscript -e "library(knitr); knit('myfile.Rmd')
But is there a way to get it to use stdin and stdout instead of files? 但是有没有办法让它使用stdin和stdout而不是文件?
I tried several variations on 我尝试了几种变体
Rscript -e 'library(knitr); knit2html(text=readLines(), output=stdout())'
but none have worked so far. 但到目前为止,都没有成功。
EDIT: I suppose worst case, I could write a wrapper script that writes stdin to a temp file, calls knitr on it, echoes the output file, and deletes them. 编辑:我想最坏的情况是,我可以编写一个包装器脚本,将stdin写入临时文件,对其调用knitr,回显输出文件,然后将其删除。 But that's kind of ugly :( 但这有点丑陋:(
OK I tried it again today, and here's a working script: 好的,我今天再次尝试了,这是一个工作脚本:
#!/bin/bash
# Hacky version of what I thought should be doable like so:
# Rscript -e 'library(knitr); knit2html(text=readLines(), output=stdout())'
# Turns out you need tempfiles for some reason?
input="/tmp/input.Rmd"
output="/tmp/output.html"
rcode="library(knitr); knit2html(input=\"$input\", output=\"$output\")"
cat /dev/stdin > "$input"
Rscript -e "$rcode" &> /dev/null
cat "$output"
You pipe R markdown in and HTML comes out. 您通过管道R降价,HTML出来了。 Ignores any errors. 忽略任何错误。
Here is one version: 这是一个版本:
#!/bin/Rscript
library(knitr)
input <- readLines('stdin')
invisible(knit(text=input, output=stdout(), quiet=TRUE))
Then (assuming script is knit.R
, rmd file is test.R
and md to html converted is multimarkdown
: 然后(假设脚本是knit.R
,RMD文件是test.R
和MD到HTML转换的multimarkdown
:
knit.R < test.Rmd | multimarkdown > test.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.