【问题标题】:What is a neat command line equivalent to RStudio's Knit HTML?什么是等同于 RStudio 的 Knit HTML 的简洁命令行?
【发布时间】:2015-11-17 22:30:06
【问题描述】:

什么是等同于 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 的最佳命令行解决方案是什么?

【问题讨论】:

  • 看看here,看看 RStudio 在幕后做了什么来制作“漂亮”的 HTML 输出。

标签: r rstudio knitr r-markdown


【解决方案1】:
rmarkdown::render("test.Rmd", "html_document")

【讨论】:

  • 太好了,这行得通!完整的命令行命令为:Rscript -e 'library(rmarkdown); rmarkdown::render("/path/to/test.Rmd", "html_document")'
  • 更短的shell命令,基于@elke上面的精彩评论,使用默认输出格式并删除库调用(与::冗余)Rscript -e "rmarkdown::render('test.Rmd')"
  • 当点击 RStudio 图标时,页面会打开(或者在我的情况下是 PDF)。是否可以在命令行中指定编织后打开PDF?
【解决方案2】:

根据接受的答案,我起草了一个名为“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 $1 | cut -f2 -d.`
extension2=`echo $2 | cut -f2 -d.`

### Test if file exist
if [[ ! -r $1 ]]; 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('"${1}"', 'pdf_document')" >> $tempscript
    Rscript $tempscript
fi
if [[ $extension2 == "html" ]]; then
    echo "library(rmarkdown); rmarkdown::render('"${1}"', 'html_document')" >> $tempscript
    Rscript $tempscript
fi

【讨论】:

  • 为此编写一个脚本是个好主意。不幸的是你的脚本有一些错误(例如第二个参数是必需的,但基本上被忽略了 - 只考虑文件扩展名;脚本需要bash,但以!/bin/sh等开头),所以我创建了一个@987654321 @。现在第二个参数指定输出格式。
  • 感谢您让我保持警惕!
【解决方案3】:

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

在您的.profile 或类似名称中,添加:

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

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

编辑:对于漂亮的自动完成,请参阅 cmets

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

【讨论】:

  • 这样简单高效。将制表符补全限制为 .Rmd 文件也很有用,以节省一些输入。
  • 是的,期待未来的读者添加此自动完成功能!
  • @baxx 我使用 bash,所以我在 .bashrc 中添加了一些东西,而不是 .profile。我添加了complete -f -X '!*.Rmd' knit。参见示例:github.com/Freguglia/dotfiles/blob/…
【解决方案4】:
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 命令提示符触发它并解决了我的要求:) 请注意:如果它不起作用..尝试安装乳胶并重试..一切顺利:)

【讨论】:

    猜你喜欢
    • 2015-03-08
    • 1970-01-01
    • 2017-02-02
    • 2022-08-02
    • 2019-10-17
    • 2019-01-01
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多