I am trying to use the two packages, afex (easy anovas), and rempsyc (easy APA formatting for tables) in my code. Independently, both work nicely. I use rempsyc's function nice_table to create a table in APA formatting that can be exported to Word but also seen in the RStudio Viewer, like this:

Once I load afex though, this function from rempsyc doesn't show me a table anymore and instead gives me the following output:
[1] header body footer col_keys caption blanks properties
<0 rows> (or 0-length row.names)
To reproduce this issue, here´s a basic example based on mtcars that is also used for demonstration of rempsyc.
library(rempsyc)
nice_table(
mtcars[1:3, ],
title = c("Table 1", "Motor Trend Car Road Tests"),
note = c(
"The data was extracted from the 1974 Motor Trend US magazine.",
"* p < .05, ** p < .01, *** p < .001"
)
)
#this one works
library(afex)
nice_table(
mtcars[1:3, ],
title = c("Table 1", "Motor Trend Car Road Tests"),
note = c(
"The data was extracted from the 1974 Motor Trend US magazine.",
"* p < .05, ** p < .01, *** p < .001"
)
)
#this one gives the error above and no table is created
detach("package:afex", unload = TRUE)
nice_table(
mtcars[1:3, ],
title = c("Table 1", "Motor Trend Car Road Tests"),
note = c(
"The data was extracted from the 1974 Motor Trend US magazine.",
"* p < .05, ** p < .01, *** p < .001"
)
)
#still doesn´t work
As you can see, my first solution was to unload afex to make this work again, but the same error remains. Specifying the package (rempsyc::nice_table) also doesn't work. The only way I can get the function nice_table to work again is to close R/RStudio completely and restart from the top.
I think the problem is within the nice function from afex that outputs a dataframe of the class nice_table (as the manual states). This somehow seems to overwrite some default method that allows the function nice_table to work.
I am basically just asking if anyone knows a way that these two packages might still work together in one script. Maybe I have missed something.
Edit: In the development version of rempsyc, I have now removed the nice_table class completely, so you should be able to install the new version from the r-Universe without having to use pkgload::unload in your script. The new version will find its way to CRAN eventually.
install.packages("rempsyc", repos = c(
rempsyc = "https://rempsyc.r-universe.dev",
CRAN = "https://cloud.r-project.org"))
I am the rempsyc package maintainer. Thank you for reporting this issue. I have opened an issue within the afex repository to find a solution to this colliding namespace. This will hopefully be resolved in a future version of afex.
In the meanwhile, I provide an explanation of this issue below, as well as a workaround using pkgload::unload instead of detach, which you can use within your script for the time being.
data <- mtcars[1:3, ]
table <- flextable::flextable(data)
class(table) <- c("nice_table", class(table))
suppressPackageStartupMessages(library(afex))
print(table)
#> [1] header body footer col_keys caption blanks properties
#> <0 rows> (or 0-length row.names)
pkgload::unload("afex")
table

Created on 2024-02-22 with reprex v2.0.2
The afex package has a printing method for objects of class nice_table (for the afex::nice() function), and, of course, rempsyc::nice_table() also produces an object of class nice_table.
The specific issue is that afex:::print.nice_table() uses print.data.frame(x) on the nice_table object, but then does not return the flextable, or rather, return it invisibly, with invisible(x) (as seen on these code lines).
I think there is nothing I can do from my side (besides giving up on the nice_table class) because afex overwrites the printing method for this class without much robustness checks to avoid this type of conflict. Hopefully we will find a solution soon.
The workaround of pkgload::unload works because, unlike detach, it detaches the whole namespace. You can just load afex again later in the script if you need to and repeat these steps as needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With