如何将 diffObj 保存为浏览器可读的 HTML?

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

下面的代码比较两个向量并创建差异,diff_content,格式为“HTML”。

在 RStudio 的控制台中键入 diff_content 会在查看器中显示此 table:

但是,当我用 save_html 保存 diff_content 然后尝试用 RStudio(或 Chrome)打开 HTML 文件时,它显示 HTML 作为文本

<!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...

如何保存 diff_content 以便在使用网络浏览器打开文件时可以再次看到格式良好的 table?

R 脚本

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 )

我不确定 save_html 函数在这种情况下是否真的有用。您可以将 diff_content 转换为将生成 HTML 的字符,然后您可以将其写入文件。试试这个。

writeLines(as.character(diff_content), file_name_diff)