Nvim-R icon indicating copy to clipboard operation
Nvim-R copied to clipboard

Support bookdown render functionality

Open krishnakumarg1984 opened this issue 5 years ago • 4 comments

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")

krishnakumarg1984 avatar Jun 26 '20 23:06 krishnakumarg1984

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.

paulrougieux avatar Jul 20 '20 10:07 paulrougieux

You can also use

:make

jranke avatar Jul 20 '20 10:07 jranke

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.

mikemc avatar Apr 07 '21 13:04 mikemc

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 :

  1. Render the site in a separate Vim split with a makefile containing
    render:
         Rscript -e "bookdown::render_book()"
  1. 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()"
  1. 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)

paulrougieux avatar Apr 07 '21 14:04 paulrougieux