Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rempsyc opened this issue Feb 22, 2024 · 2 comments

Comments

@rempsyc
Copy link

rempsyc commented Feb 22, 2024

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).

afex/R/nice.R

Lines 338 to 349 in 1e3ee8d

print.nice_table <- function(x, ...) {
if(!is.null(heading <- attr(x, "heading"))) {
cat(heading, sep = "\n")
}
print.data.frame(x)
if(!is.null(attr(x, "sig_symbols"))) print_legend(x)
if(!is.null(correction_method <- attr(x, "correction")) &&
correction_method != "none") {
cat("\nSphericity correction method:", correction_method, "\n")
}
invisible(x)
}


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
Copy link
Owner

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
Copy link
Author

rempsyc commented Feb 25, 2024

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.

@rempsyc rempsyc closed this as completed Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants