Search code examples
rdiffobj

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


I have two files and am using R's diffobj to create an HTML difference file between them.

When I run the RScript in RStudio all is well and I get a diff HTML file like:

enter image description here

When I run the script from the command line, the HTML diff file looks like: enter image description here

How do I run the R Script from the command line and get the nice HTML formatting?

R Script and Text Files

Original Text File - file_name_original

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

Changed Text File - 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 Script

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)

Solution

  • By default diffFile() behaves differently depending on if R is in interactive mode or not so you need to use the argument interactive = TRUE to get the same result as you would from the console.

    Using the function example from the documentation:

    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)