rhtmltoolsdiffobj

How to save diffObj as HTML that's readable by a browser?


The code below compares two vectors and creates the difference, diff_content, formatted as "HTML".

Typing diff_content in RStudio's Console brings up this table in the Viewer:

enter image description here

However, when I save diff_content with save_html and then try to open the HTML file with RStudio (or Chrome) it displays the HTML as text

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style type='text/css'> /* Structural CSS ------------------------------------------------------------*/ /* * TBD whether we want a more fully table like structure; some of the visual * cues provided by the current set-up are useful (line wraps, etc.) */ DIV.diffobj-container PRE.diffobj-content { white-space: pre-wrap; margin: 0; } DIV.diffobj-container DIV.diffobj-row { width: 100%; font-family: monospace; display: table; table-layout: fixed; } DIV.diffobj-container DIV.diffobj-line { width: auto; display: table-cell; overflow: hidden; } DIV.diffobj-container DIV.diffobj-line>DIV { width: 100%; display: table; table-layout: auto; } DIV.diffobj-container DIV.diffobj-line.banner>DIV { display: table; table-layout: auto; /* set to fixed in JS */ } DIV.diffobj-container DIV.diffobj-text { display: table-cell; width: 100%; } DIV.diffobj-container DIV.diffobj-gutter { display: table-cell; padding: 0 0.2em; } DIV.diffobj-container DIV.diffobj-gutter DIV { display: table-cell; } #diffobj_content_meta DIV.diffobj-container DIV.diffobj-row { width: auto; } #diffobj_banner_meta DIV.diffobj-container DIV.diffobj-line.banner>DIV { table-layout: auto; } #diffobj_outer { overflow: hidden; } /* Summary -------------------------------------------------------------------*/ DIV.diffobj-container...

How can I save diff_content so that I can see the nicely formatted table again when opening the file with a web browser?

R Script

library(diffobj)
library(htmltools)

file_old <- 1:10
file_new <- c(1:8, 23, 24)

file_name_diff <- "cc_page_diffs/test_diff.html"

diff_content <- diffPrint(current = file_old, target = file_new, format="html")
save_html(diff_content, file = file_name_diff )

Solution

  • I'm not sure the save_html function is really helpful in this case. You can convert the diff_content to character which will produce the HTML which you can then write to a file. Try this instead.

    writeLines(as.character(diff_content), file_name_diff)