Many books that are published on R are also freely available online at Bookdown.org. If authoring a book in R, then use the bookdown package.

A longer description is available on https://www.r-bloggers.com/how-to-self-publish-a-book-customizing-bookdown/

A Python alternative would be Sphinx. Here is an example on statistical physics.