什么是相当于 RStudio 的 Knit HTML 的简洁命令行?

What is a neat command line equivalent to RStudio's Knit HTML?

什么是相当于 RStudio 的 Knit 的简洁命令行 HTML?给定一个 .Rmd 文件,您可以使用 RStudio 使用 Knitr 编织 .html.docx.pdf 文件。将此过程完全转移到命令行会很棒。到目前为止我的方法:

Rscript -e "library(knitr); knit('test.Rmd')"  # This creates test.md
pandoc test.md >> test.html

这工作正常,但生成的 test.html 没有像 RStudio 中那样漂亮的改头换面。关于如何最好地通过命令行将 .Rmd 文件编织到 .html,并以漂亮的 .html?

结尾的任何建议

额外问题:.pdf.docx 的最佳命令行解决方案是什么?

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

根据已接受的答案,我起草了一个名为 "knitter" 的 bash 脚本,它将完成所需的一切,用户需要做的就是输入:./knitter file.Rmd file.html./knitter file.Rmd file.pdf.

脚本如下:

#!/bin/sh

### Test usage; if incorrect, output correct usage and exit
if [ "$#" -gt 2  -o  "$#" -lt 2 ]; then
    echo "********************************************************************"
    echo "*                        Knitter version 1.0                       *"
    echo "********************************************************************"
    echo -e "The 'knitter' script converts Rmd files into HTML or PDFs. \n"
    echo -e "usage: knitter file.Rmd file.{pdf,html} \n"
    echo -e "Spaces in the filename or directory name may cause failure. \n"
    exit
fi
# Stem and extension of file
extension1=`echo  | cut -f2 -d.`
extension2=`echo  | cut -f2 -d.`

### Test if file exist
if [[ ! -r  ]]; then
    echo -e "\n File does not exist, or option mispecified \n"
    exit
fi

### Test file extension
if [[ $extension1 != Rmd ]]; then
    echo -e "\n Invalid input file, must be a Rmd-file \n"
    exit
fi

# Create temporary script
# Use user-defined 'TMPDIR' if possible; else, use /tmp
if [[ -n $TMPDIR ]]; then
    pathy=$TMPDIR
else
    pathy=/tmp
fi
# Tempfile for the script
tempscript=`mktemp $pathy/tempscript.XXXXXX` || exit 1

if [[ $extension2 == "pdf" ]]; then
    echo "library(rmarkdown); rmarkdown::render('""', 'pdf_document')" >> $tempscript
    Rscript $tempscript
fi
if [[ $extension2 == "html" ]]; then
    echo "library(rmarkdown); rmarkdown::render('""', 'html_document')" >> $tempscript
    Rscript $tempscript
fi

我的更简单的命令行脚本,类似于 Tyler R. 的:

在您的 .profile 或类似内容中,添加:

function knit() {
    R -e "rmarkdown::render('')"
}

然后,在命令行中输入knit file.Rmd

编辑:要获得漂亮的自动完成功能,请参阅评论

我在 Rmd header 中设置了输出格式:output: github_document 或类似的

getwd()
setwd("C:Location of the RMD File")

# To make it in to PDF you can use the below code also
rmarkdown::render("Filname.Rmd")

# To make it in to PDF you can use the below code also
rmarkdown::render("Filename", "pdf_document")

我将以上内容输入 R 脚本并从 Python 命令提示符触发它并解决了我的要求:) 请注意:如果它不起作用..尝试安装乳胶并重试..一切顺利:)

从 mac/linux 终端,您可以 运行:

R -e "rmarkdown::render('README.Rmd')"

(将 README.Rmd 替换为您想要编织的任何文件)