vincentarelbundock / modelsummary

Beautiful and customizable model summaries in R.
http://modelsummary.com
Other
908 stars 75 forks source link

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

Open astraetech opened 4 weeks ago

astraetech commented 4 weeks ago

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!

vincentarelbundock commented 4 weeks ago

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.

astraetech commented 4 weeks ago

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

vincentarelbundock commented 4 weeks ago

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.

jialul commented 3 weeks ago

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.
vincentarelbundock commented 3 weeks ago

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

jialul commented 3 weeks ago

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