parameters icon indicating copy to clipboard operation
parameters copied to clipboard

`show()` method for printing is not supported

Open krassowski opened this issue 3 years ago • 1 comments

show() is a native R method from core methods package for printing objects, for many cases just a wrapper around print(); some R interfaces rely on using methods::show() rather than print(). methods::show(object) passes useS4 = FALSE argument in:

https://github.com/wch/r-source/blob/79298c499218846d14500255efd622b5021c10ec/src/library/methods/R/show.R#L44-L46

which sometimes breaks custom print method implementations:

To reproduce:

model <- lm(Sepal.Width ~ Petal.Length * Species + Petal.Width, data = iris)
result = parameters::model_parameters(model)
show(result)

Raises:

Error in insight::export_table(formatted_table, format = "text", caption = table_caption,  : 
  unused argument (useS4 = FALSE)
    ▆
 1. ├─base::withVisible(...)
 2. ├─methods::show(result)
 3. ├─methods::show(result)
 4. │ └─methods::showDefault(object)
 5. │   ├─base::print(object, useS4 = FALSE)
 6. │   └─parameters:::print.parameters_model(object, useS4 = FALSE)
 7. │     └─base::cat(...)
 8. └─global `<fn>`()

This can be fixed by disregarding useS4 in parameters:::print.parameters_model, similarly to how it was fixed in tidygraph: https://github.com/thomasp85/tidygraph/commit/36371e368ffe8eaf07d07596c7c70f64386c6e4e

Side note: this is pretty confusing, as useS4 is not documented in R print.default documentation, even though it seems to be a part of the API (as evidenced by the methods::show code and the R C code: https://github.com/wch/r-source/commit/c87695193cbe8524983720df7644e8f310b17233#diff-fb91d6255ac59c56417d9bea66932ae7a3e287e322b767fa9b863b11a8350433R151-R222 - even though the documentation comment no longer mentions it - it was rewritten in https://github.com/wch/r-source/commit/0d4dea725dc6ceb9ecf7bfb230d465c871af0980#diff-fb91d6255ac59c56417d9bea66932ae7a3e287e322b767fa9b863b11a8350433L195-L196).

krassowski avatar Jul 20 '22 11:07 krassowski

show() is a function for S4 objects (that's what the methods package is--the home of the S4 system). It isn't intended as a drop in replacement for print() and shouldn't be used as such. S4 is a largely dead part of R, and its idiosyncrasies aren't something that is or probably should be widely supported.

You shouldn't expect it to work with non-S4 objects or use it in general. If an interface is using show() and that causes problems, the inference should fix that by using R's actual function for displaying output--print().

bwiernik avatar Jul 20 '22 13:07 bwiernik

I guess we can close this as "not a real issue" for insight? Feel free to re-open if there's anything that we can address here.

strengejacke avatar Aug 14 '22 07:08 strengejacke