Support bookdown render functionality
The bookdown R-package helps in compiling multi-page HTML output resembling the gitbook formats which are absoloutely beautiful (and packed with usability features).
Currently, the supported key bindings do not account for bookdown and only cover single-page rendering of Rmarkdown documents. To clarify, bookdown is not a separate documentation format. It is simply RMarkdown syntax source files, but separated into multiple files (one per chapter) for long-form reports, books and theses.
Can you please provide suitable infrastructure to execute the bookdown::render_book function from within vim? Its calliogn syntax and default arguments are given below (extracted verbatim from the official documentation):
render_book(input, output_format = NULL, ...,
clean = TRUE, envir = parent.frame(),
clean_envir = !interactive(), output_dir = NULL,
new_session = NA, preview = FALSE,
config_file = "_bookdown.yml")
In bookdown.org build-the-book, Yihui (the package author) recommends using a makefile. With a make file, book building can be started from vim in a separate buffer with:
:term make
The use of :term make instead of :make has the advantage of opening a new split, not blocking your current editing window. This is useful if your site takes a while to render, you can continue editing and working at the R prompt while it builds.
A sample makefile is provided within the package at bookdown/inst/examples/Makefile.
You can also use
:make
Another option that doesn't require any change to Nvim-R is to define your own shortcut to run bookdown::render_book() in the current session, similar to <LocalLeader>kr for standard Rmarkdown rendering. E.g. I put the following in ".vim/ftplugin/rmd.vim",
" Bookdown rendering
map <silent> <LocalLeader>kbb :call g:SendCmdToR("bookdown::render_book('.')")<CR>
which assumes the R working directory is the root of the book project; or you might instead replace '.' with here::here(). I would also like to have a shortcut to bookdown::preview_chapter(), e.g.
map <silent> <LocalLeader>kbc :call g:SendCmdToR("bookdown::preview_chapter(???)")<CR>
but I'm not sure what to replace ??? with to get the the file path to the current vim open file to be inserted into the command.
define your own shortcut to run bookdown::render_book() in the current session
Using your current R session is not desirable when the sites takes more than a few seconds to render. I would recommend using another R session to render the site, so that you are able to continue interacting with R in the main session. Three ways to use another R session :
- Render the site in a separate Vim split with a
makefilecontaining
render:
Rscript -e "bookdown::render_book()"
- Render the site in a separate tmux pane or vim split with a bash command (you can send that command from vim with slime)
cd ~/project_repository/ && Rscript -e "bookdown::render_book()"
- Use a continuous integration tool to build the site when you push to your remote git repository. (For example here is the configuration file .gitlab-ci.yml I use to build my blog on gitlab CI)