The bookdown package is an R package and set of conventions for making books with Rmarkdown and RStudio. It's designed to produce both a website with one page per chapter, and a PDF for the entire book (future versions might also add epub support). The process is still rather idiosyncratic and tightly joined to my style of book writing, but it will probably get more flexible in time, and just seeing my approach (and code) might be useful to you.
@andkov, here are some tools that might help your goals
Hadley's bookdown: https://github.com/hadley/bookdown
I think his process has changed, but here's a book that he did with a predecssor approach a few years ago: Advanced R code & presentation.