How does RStudio generate html outout for the viewer pane?

rstudio
html
viewer

#1

Esteemed RStudio community,
This:

library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:6]
dt %>%
  kable() %>%
  kable_styling()

Writes to my hardrive the following files and directory which are needed for proper rendering of the html page:
/tmp/.../index.html
/tmp/.../lib/libraryX/some_files
/tmp/.../lib/libraryY/more_files

Sometimes I'm not able to use RStudio to develop kableExtra tables and must use another editor like Emacs. I can capture the contents of index.html e.g. using writeLines(), but would also like to output the /tmp.../lib directories and files that RStudio automagically does in order have the fully rendered html output as it would appear using RStudio.

How can i get this? Is it practically possible or are packages like kableExtra really dependent on RStudio?

Apologies in advance if the topic belongs elsewhere or has already been raised previously - please point me there! < first post :slight_smile: >


#2

Since lots of people get started with R Markdown in RStudio, and RStudio provides some really great integrated tools for working with R Markdown, I think your question is a common point of confusion. The great news is that the RStudio knit button is just running rmarkdown::render() on the Rmd file. All the work is being done by rmarkdown / knitr / pandoc, and works just as well outside of RStudio.

The How It Works section of the R Markdown website explains in more detail:
https://rmarkdown.rstudio.com/lesson-2.html

(If you have more questions about how all this works, the best category is probably #R-Markdown)


#3

@jcblum Thank you! Yes, rmarkdown / pandoc / htmltools are the magic which allow RStudio to write to R's temporary directory the /tmp/lib/... files I was seeking to obtain independently of RStudio. For the sake of completeness, KabelExtra's documentation details the exact function I needed, namely, save_kable. Thus, this snippet is now complete for the (thankfully rare) code writen without RStudio:

library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:6]
dt %>%
  kable() %>%
  kable_styling() %>%
  save_kable(file = "table1.html", self_contained = T)  ## saves to working directory
browseURL("table1.html")                           ## opens table.html in system browser

#4

Glad you worked it out, and thanks especially for providing your final snippet — I expect that will be very helpful to future seekers! :grin: