当从命令行与 RStudio 运行 时,差异文件丢失 HTML 格式

Diff file loses HTML formatting when run from command line vs RStudio

我有两个文件,正在使用 R 的 diffobj 在它们之间创建一个 HTML 差异文件。

当我 运行 RStudio 中的 RScript 一切正常时,我得到一个 diff HTML 文件,如:

当我从命令行 运行 脚本时,HTML diff 文件如下所示:

如何从命令行 运行 R 脚本并获得漂亮的 HTML 格式?

R 脚本和文本文件

原始文本文件 - file_name_original

Hello there I am a file
I have three lines
And no fourth line

更改的文本文件 - file_name_changed

Hello there I am a file
I have three lines but I am a little longer than usual
And no fourth line

R 脚本

library("diffobj")

file_name_diff <- "diff.html"

file_name_original <- # Path to original file
file_name_changed <- # Path to changed file

# Compare files
diff_content <- diffFile(current = file_name_original, 
                         target = file_name_changed, 
                         mode = "sidebyside",
                         format = "html")

writeLines(as.character(diff_content), file_name_diff)

默认情况下 diffFile() 的行为不同,具体取决于 R 是否处于交互模式,因此您需要使用参数 interactive = TRUE 来获得与从控制台获得相同的结果。

使用文档中的函数示例:

library("diffobj")

file_name_diff <- "C:\Path\to\file\diff.html"

url.base <- "https://raw.githubusercontent.com/wch/r-source"
f1 <- file.path(url.base, "29f013d1570e1df5dc047fb7ee304ff57c99ea68/README")
f2 <- file.path(url.base, "daf0b5f6c728bd3dbcd0a3c976a7be9beee731d9/README")

res <- diffFile(f1,
                f2,
                mode = "sidebyside",
                format = "html",
                interactive = TRUE)

writeLines(as.character(res), file_name_diff)