How to view the Viewer data in Console


#1

I was using the diffFile to find the difference of two files using the below code:

filenameForA <- "C:\\Users\\gsyed\\Desktop\\1.txt"
filenameForB <- "C:\\Users\\gsyed\\Desktop\\2.txt"

library(diffobj)
diffFile(filenameForA, filenameForB)

I can see the differences in the Viewer area of R studio. Is there a way to see this output in the console or store it in any variable. Are there any other libraries i can make use of?

I am new to R, please help me here.


#2

You can use the style parameter to change the format:

diffChr("a", "b", style = StyleAnsi())
# < "a"       > "b"     
# @@ 1 @@     @@ 1 @@   
# < a         > b

help("Style-class", package = "diffobj") will open the documentation describing the different styles and how they work. If you're new, you'll probably want to skip down to the Pre-defined Classes section. This doc page is a whopper.


#3

So is there a way to save this difference to a variable?


#4

Yes, and it's done in the usual way. But the result is not just the text displayed to the screen. It's an object of the Diff class:

x <- diffChr("a", "b", style = StyleAnsi())
str(x, max.level = 2)
# Formal class 'Diff' [package "diffobj"] with 14 slots
#   ..@ target         : chr "a"
#   ..@ tar.dat        :List of 11
#   ..@ current        : chr "b"
#   ..@ cur.dat        :List of 11
#   ..@ diffs          :List of 1
#   .. ..- attr(*, "meta")=List of 3
#   ..@ trim.dat       :List of 3
#   ..@ sub.index      : int(0) 
#   ..@ sub.head       : int(0) 
#   ..@ sub.tail       : int(0) 
#   ..@ capt.mode      : chr "chr"
#   ..@ hit.diffs.max  : logi FALSE
#   ..@ diff.count.full: int -1
#   ..@ hunk.heads     :List of 1
#   ..@ etc            :Formal class 'Settings' [package "diffobj"] with 27 slots

I've never used this package before today, so I can't speak to what parts of this should or shouldn't be used in your code (the maintainer may decide to change them later).

If you just want to keep the text printed to the console, as.character works with Diff objects:

as.character(x)
# [1] "\033[31m<\033[39m \033[31m\"a\"\033[39m       \033[32m>\033[39m \033[32m\"b\"\033[39m     "                                                            
# [2] "\033[36m@@ 1 @@   \033[39m  \033[36m@@ 1 @@   \033[39m"                                                                                                
# [3] "\033[31m<\033[39m \033[90m\033[39m\033[31ma\033[39m\033[90m\033[39m         \033[32m>\033[39m \033[90m\033[39m\033[32mb\033[39m\033[90m\033[39m       "
# attr(,"len")
# [1] 3