alexsanjoseph / compareDF

R Tool to compare two data.frames
Other
93 stars 17 forks source link

class changed to 'data.table' not internally, but in enclosing/global environment, w/o notice. #44

Closed PatrickOStats closed 2 years ago

PatrickOStats commented 2 years ago

A simple call of compare_df() changes the class of the 2 data.frames on which it is called to data.table - not internally, but in the enclosing environment, e.g. GlobalEnv in the example below. For those of us who don't work with data.tables this can produce unwanted results. With the way R "saves" copies of the same data.frame, there's also no quick way around it with a backup copy. If I'm not mistaken, a call to a function making changes to objects in the global environment is uncommon in R. Two solutions I could think of:

Thank you. Great package, really appreciate it so much!

Minimum reproducible example

library(compareDF)
# Using a well-known data source:
data.old <- cars[1:5, ]
# make an arbitrary change: replace line 2 with a copy of line 1:
data.new <- data.old[c(1,1,3:5),]
data.old.to.compare <- data.old
data.old.backup <- data.old

# Objects are data.frames, but not data.tables:
is(data.old)

# Now a simple call to the function:
compare_df(df_new = data.new, df_old = data.old.to.compare)

# Now all 4 objects in the global environment are of class data.table:
is(data.old); is(data.old.backup); is(data.old.to.compare)
is(.GlobalEnv[["data.old.backup"]])
is(data.new)
alexsanjoseph commented 2 years ago

Thanks for the issue and kind comments @PatrickOStats - I will take a look at this and get back soon!

alexsanjoseph commented 2 years ago

Thanks again for reporting the bug @PatrickOStats - Have created a PR here https://github.com/alexsanjoseph/compareDF/pull/45, will merge and create a new release soon

alexsanjoseph commented 2 years ago

Latest version on cran should fix this issue

PatrickOStats commented 2 years ago

Latest version on cran should fix this issue

Thank you very much, @alexsanjoseph! With the latest version, I do no longer experience any issues of this sort. :)