singmann / afex

Analysis of Factorial EXperiments (R package)
119 stars 32 forks source link

Printing method for `nice` collides with `rempsyc::nice_table()` of class `nice_table` #126

Closed rempsyc closed 7 months ago

rempsyc commented 7 months ago

Hello, I do not use package afex, but a user (see rempsyc/rempsyc#26 for reprexes) has reported an issue wherein the printing method for the function nice, which outputs an object of class nice_table, collides with rempsyc::nice_table(), also of class nice_table, simply when afex is loaded (and remains after being detached).

The specific issue is that afex:::print.nice_table() uses print.data.frame(x) on the flextable object, but then does not return the flextable, or rather, return it invisibly, with invisible(x).

https://github.com/singmann/afex/blob/1e3ee8d438795ad60129735ae09cc8458864c7dc/R/nice.R#L338-L349


How would you suggest addressing the issue?

A possibility is to update afex::nice() to make it more robust by adding a check that the object does not have additional classes (e.g., rempsyc::nice_table() also has a flextable class).


data <- mtcars[1:3, ]
table <- flextable::flextable(data)
class(table) <- c("nice_table", class(table))
table

suppressPackageStartupMessages(library(afex))

print(table)
#> [1] header     body       footer     col_keys   caption    blanks     properties
#> <0 rows> (or 0-length row.names)

Created on 2024-02-22 with reprex v2.0.2

singmann commented 7 months ago

Thanks for alerting me of this. However, I am not sure if I can do much about this. The afex print method follows the general structure of methods for the print generic. It returns objects via invisible(x) without changing the objects, see: https://stat.ethz.ch/R-manual/R-devel/library/base/html/print.html I cannot see a corresponding print.nice_table method in rempsyc that could handle the object instead.

Maybe I am missing something, but on my brief look at your code, I cannot see any method for nice_table objects. Thus, maybe the easiest solution would be to remove the class at your end. If you do not define methods for it, is there any reason to add the class to the object in the first place?

rempsyc commented 7 months ago

Good point, thanks! Yes, there are no methods for nice_table objects yet, so I think you are right, I will remove it for now. Thank you for the suggestion! I think the reason was until now to leave a "signature trail" of how the table was made, as I had seen in other code.

FWIW, the workaround I was thinking for afex (now outdated) was in the printing method to check for the exact classes afex::nice produces, and return the object as is if it doesn't match.