dfrankow
dfrankow

Reputation: 21387

How to build a bookdown book that is not in the top-level directory in RStudio?

The bookdown docs say after setting up _bookdown.yml appropriately,

Then you can click the Build Book button in the Build pane in RStudio to compile the Rmd files into a book

I see no "Build Book" button in my RStudio, even after restart.

> packageVersion('bookdown')
[1] ‘0.18’

Here is my _bookdown.yml:

rmd_files: ["one.Rmd", "two.Rmd", "three.Rmd"]
site: "bookdown::bookdown_site"
output:
  bookdown::gitbook:
    lib_dir: "book_assets"
  bookdown::pdf_book:
    keep_tex: yes
delete_merged_file: true

I wonder if it's because my _bookdown.yml and my .Rmd files are not in the top-level directory. I want to make a book a week, so I have a subdirectory for each book. I use the same top-level project, so I can share code among books.

So if I am correct about the subdirectory being the problem: how to build a bookdown book that is not in the top-level directory in RStudio?

Otherwise, is there something else I should check?

EDIT: I'm just working on a function to build the book instead, something like:

make_book <- function(subdir) {
  setwd(paste0('dir/',subdir))
  # note: input doesn't matter, because config_file will have the book
  bookdown::render_book(input='_bookdown.yml',
                        config_file='_bookdown.yml')
  setwd(here::here())
}

Upvotes: 0

Views: 224

Answers (1)

S&#233;bastien Rochette
S&#233;bastien Rochette

Reputation: 6661

I would recommend keeping this idea of the function. Use this one instead:

make_book <- function(subdir) { 

origwd <- setwd(file.path('dir', subdir))
on.exit(setwd(origwd))
 bookdown::render_book(input='_bookdown.yml', config_file='_bookdown.yml')
}

Upvotes: 1

Related Questions