45

What is a neat command line equivalent to RStudio's Knit HTML? Given an .Rmd file, you can use RStudio to knit .html, .docx and .pdf files using Knitr. It would be great to shift this process completely to the command line. My approach so far:

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

This works fine, but the resulting test.html does not come with the same pretty make over as in RStudio. Any suggestions how one should best knit .Rmd files to .html via the command line, and end up with a pretty .html?

Extra question: What would be the best command line solution for .pdf or .docx?

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
elke
  • 1,220
  • 2
  • 12
  • 24
  • 1
    have a look [here](http://stackoverflow.com/questions/26286797/automate-rstudio-processed-rmarkdown/26287127#26287127) to see what RStudio does under the covers to make their "pretty" HTML output. – hrbrmstr Aug 24 '15 at 13:48
  • `Rscript -e 'library(rmarkdown); rmarkdown::render("[path]/test.Rmd", "html_document")'` – mikey Mar 15 '22 at 15:47

5 Answers5

71
rmarkdown::render("test.Rmd", "html_document")
VFreguglia
  • 2,129
  • 4
  • 14
  • 35
gd047
  • 29,749
  • 18
  • 107
  • 146
  • 22
    Great, this works! The complete command line command is: `Rscript -e 'library(rmarkdown); rmarkdown::render("/path/to/test.Rmd", "html_document")'` – elke Aug 31 '15 at 12:54
  • 2
    Shorter shell command, based on @elke's great comment above, using the default output format and removing the library call (redundant with `::`) `Rscript -e "rmarkdown::render('test.Rmd')"` – Paul Rougieux Dec 09 '19 at 10:22
  • When clicking the RStudio icon the page opens (or the PDF in my case). Is it possible to specify in the command line to open the PDF after knitting? – ECII Dec 24 '21 at 18:24
13

My simpler command-line script, similar to Tyler R.'s:

In your .profile or similar, add:

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

Then, on command line, type knit file.Rmd

EDIT: For nice autocomplete, see comments

I set up output format in the Rmd header: output: github_document or similar

Paul
  • 3,920
  • 31
  • 29
  • This is simple and efficient. It is also useful to restrict tab-completion to `.Rmd` files to save some typing. – VFreguglia Oct 03 '19 at 17:21
  • yes, looking to future readers to add this auto-complete functionality! – Paul Oct 11 '19 at 02:05
  • 4
    @baxx I use bash, so I added stuff to my `.bashrc` instead of `.profile`. I added `complete -f -X '!*.Rmd' knit`. See as an example: https://github.com/Freguglia/dotfiles/blob/d0be8c1b4f68af0605bac438c31ec98792e4aa0f/.bashrc#L97 – VFreguglia Sep 09 '20 at 14:22
12

Following up on the accepted answer, I've drafted a bash script called "knitter" that will do everything needed, all the user needs to do is input: ./knitter file.Rmd file.html or ./knitter file.Rmd file.pdf.

The script is below:

#!/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
Tyler R.
  • 461
  • 6
  • 15
  • 3
    Great idea to have a script for that. Unfortunately your script has some bugs (e.g. the second argument is required, but is basically ignored - only the file extension is considered; the script requires `bash`, but is started with `!/bin/sh` etc.), so I created a [corrected version](https://gist.github.com/ximeg/9d1a2d543c8959725b1c9bace196efd5). Now the second argument specifies the output format. – R Kiselev Apr 03 '20 at 18:29
  • Thanks for keeping me on my toes! – Tyler R. Apr 04 '20 at 19:02
  • 1
    If you use the version of @RKiselev, the syntax would be `./knitter ` for example `./knitter input.Rmd pdf` – Masoud Ghaderi Oct 31 '22 at 13:49
0
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")

I Typed the above in to a R Script and triggered it from Python command prompt and solved my requirement:) Kindly note : if it doesn't work.. Try to install latex and try again.. All the best :)

Naveen Kumar
  • 277
  • 4
  • 5
0

From the mac/linux terminal, you could run:

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

(replacing README.Rmd with whatever file you wish to knit)

stevec
  • 41,291
  • 27
  • 223
  • 311