modelsummary icon indicating copy to clipboard operation
modelsummary copied to clipboard

Flextable + Shape and bind = Subheaders on the same line.

Open astraetech opened this issue 1 year ago • 6 comments

Hi Vincent!

Came back to modelsummary after a long while. Awesome that you are still working on it! Could you look into this please?

panels <- list(
  "Outcome: mpg" = list(
    "(I)" = lm(mpg ~ 1, data = mtcars),
    "(II)" = lm(mpg ~ qsec, data = mtcars)
  ),
  "Outcome: hp" = list(
    "(I)" = lm(hp ~ 1, data = mtcars),
    "(II)" = lm(hp ~ qsec, data = mtcars)
  )
)
        
    modelsummary(panels, shape = "cbind")
    modelsummary(panels, shape = "cbind", output=
                 "flextable")

The last line gives me the following headers: Outcome: mpg.(I) Outcome: mpg.(II) Outcome: hp.(I) Outcome: hp.(II).

loaded: modelsummary * 2.1.0.1 2024-05-21 [1] Github (vincentarelbundock/modelsummary@2bbbb1d) tinytable 0.3.0.34 2024-08-12 [1] https://vincentarelbundock.r-universe.dev (R 4.4.0)

Apologis if I missed something in the vignettes.

Thank you!

astraetech avatar Aug 16 '24 01:08 astraetech

Thanks for the report. Spanning column headers for shape="cbind" are only available in the default output format: tinytable.

It would be relatively easy to implement them for other formats by modifying this file: https://github.com/vincentarelbundock/modelsummary/blob/main/R/modelsummary_cbind.R

This would actually be a good first issue, so I'm adding a label to this issue.

However, in all transparency, this is not for the default output format, so I'm unlikely to implement this myself in the near future. But I'd be very happy to merge a PR if someone is interested.

vincentarelbundock avatar Aug 16 '24 02:08 vincentarelbundock

Thank you, @vincentarelbundock!

I was wondering then for the meanwhile, what's the best way to implement spanning column headers in the modelsummary -> flextable pipeline?

Thank you

astraetech avatar Aug 16 '24 19:08 astraetech

I'm not super familiar with flextable, but I bet there's a way to rename columns and create spanning labels using functions from that package. Should be easy.

vincentarelbundock avatar Aug 16 '24 20:08 vincentarelbundock

Hi @vincentarelbundock, I am trying to use shape = "rbind" and shape = "cbind" but run into an error of Error in sanitize_shape(shape) : Assertion on 'shape' failed: Must be a formula (or 'NULL'), not character. Could you help point me to where might be the problem?

I am using version 2.1.1.

> gm <- c("r.squared", "nobs", "rmse")

panels <- list(
  list(
    lm(mpg ~ 1, data = mtcars),
    lm(mpg ~ qsec, data = mtcars)
  ),
  list(
    lm(hp ~ 1, data = mtcars),
    lm(hp ~ qsec, data = mtcars)
> 
> panels <- list(
+   list(
+     lm(mpg ~ 1, data = mtcars),
+     lm(mpg ~ qsec, data = mtcars)
+   ),
+   list(
+     lm(hp ~ 1, data = mtcars),
+     lm(hp ~ qsec, data = mtcars)
+   )
+ )

modelsummary(
  panels,
  shape = "rbind",
  gof_map = gm)
> 
> modelsummary(
+   panels,
+   shape = "rbind",
+   gof_map = gm)

Error in sanitize_shape(shape) : 
  Assertion on 'shape' failed: Must be a formula (or 'NULL'), not character.

jialul avatar Aug 22 '24 01:08 jialul

Can you make sure you are 100% using the latest version. I find the error surprising...

vincentarelbundock avatar Aug 22 '24 02:08 vincentarelbundock

Thanks for the prompt reply! I just force uninstalled the package and reinstalled it. It works now on my end. Sorry for the inconvenience.

jialul avatar Aug 22 '24 03:08 jialul

fixed in the dev version on github:

https://github.com/vincentarelbundock/modelsummary/commit/50d468939a73b11c3f7af489f6e5edd6259a9533

vincentarelbundock avatar Aug 19 '25 11:08 vincentarelbundock

fixed in the dev version on github:

50d4689

Thanks you!

raffaem avatar Aug 19 '25 14:08 raffaem