1

I'm trying to run an R file from the command line (from Terminal -- on macOS 10.13.3).

Following the directions in this helpful Q & A, I added #! [path/to/file.R] as the first line of the R file.

When I then type Rscript [path/to/file.R] in Terminal, a part of the file to test that it worked seems to run (I added a print() statement), however, the next line:

rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8')

Leads to this error:

Error in rmarkdown::render_site(output_format = "bookdown::pdf_book",  : 
  No site generator found.

Why does the print() statement appear to work fine, but this line - to render a bookdown book - does not?

The file I'm trying to run is on GitHub here.

Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73

2 Answers2

3

For Markdown I have had the following script in the examples directory of littler for about 1 1/2 years, and rendered hundreds of times with it.

Note it calls rmarkdown::render() yet you want a bookdown format, so methinks you may need to write a very similar script calling bookdown::render_book() or a similar function.

You can of course do exactly the same as a one-liner from Makefiles, and many people do. Here is one which will map all Rmd file to pdf documents (which is my standard workflow, I don't do much html):

sources :=              $(wildcard *.Rmd)
slides :=               $(sources:.Rmd=.pdf)

all:                    ${slides}

%.pdf:                  %.Rmd
                        Rscript -e "rmarkdown::render(\"$<\", clean=TRUE)"

Because my editor has a shortcut for invoking make this is particularly handy.

Lastly, for Rscript (available everyhere) you'd do #!/usr/bin/env Rscript or possibly the direct path to Rscript.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
1

You could use a simple script like this that you save to compile.book.sh for example.

#!/bin/sh

## Get the path to the book
BOOK_PATH=$1

## Get the current path
CURRENT_PATH=$(pwd)

## Get to the right path
cd ${BOOK_PATH}

## Compile the book
R -e 'rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8')'

## Get back to the previous path
cd ${CURRENT_PATH}

You can then execute the compilation as follows from wherever in your machine (from the terminal):

sh compile.book.sh path/to/Rfolder

With path/to/Rfolder being the folder where your index.Rmd is.

Thomas Guillerme
  • 1,747
  • 4
  • 16
  • 23