elke
elke

Reputation: 1280

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

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?

Upvotes: 49

Views: 24083

Answers (5)

stevec
stevec

Reputation: 52588

From the mac/linux terminal, you could run:

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

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

Upvotes: 0

Paul
Paul

Reputation: 4168

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

Upvotes: 13

Naveen Kumar
Naveen Kumar

Reputation: 297

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

Upvotes: 0

Yorgos
Yorgos

Reputation: 30475

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

Upvotes: 77

Tyler R.
Tyler R.

Reputation: 461

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

Upvotes: 12

Related Questions